Setup and context — About This Article Series
This series extracts insights from Gemini Lab premium content and presents them in practical form for both free and premium users.
Part 1 Purpose: Master fundamental skills for integrating Gemini API into real projects Part 2 Purpose: Build production-grade systems, voice AI, monetization pipelines
This guide assumes Python 3.10+, but REST API concepts apply across languages.
Gemini API Design Patterns
Basic Text Generation Structure
Let's start with minimal implementation:
import google.generativeai as genai
import os
# Configure API key
api_key = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=api_key)
# Initialize model
model = genai.GenerativeModel("gemini-2.5-pro")
# Simple text generation
response = model.generate_content("Explain Japan's economic growth rate in 3 sentences")
print(response.text)Key Points:
GenerativeModelspecifies the model ("gemini-2.5-pro", "gemini-2.5-flash", etc.)generate_content()accepts the prompt- Response available as
response.text
More Detailed Configuration
Real projects require parameter control:
from google.generativeai.types import GenerationConfig, SafetySetting, HarmCategory, HarmBlockThreshold
response = model.generate_content(
contents="What are best practices for designing Python Web APIs?",
generation_config=GenerationConfig(
temperature=0.7, # Creativity (0.0-2.0)
top_p=0.95, # Diversity control
top_k=40, # Narrow candidate pool
max_output_tokens=1024, # Max response tokens
stop_sequences=["---"], # Output terminator
),
safety_settings=[
SafetySetting(
category=HarmCategory.HARM_CATEGORY_UNSPECIFIED,
threshold=HarmBlockThreshold.BLOCK_NONE,
)
]
)
print(response.text)Parameter Explanation:
- temperature: Lower = deterministic, higher = random. Usually 0.5-0.9
- top_p: Nucleus sampling. Narrows to high-probability choices. Usually 0.8-0.95
- max_output_tokens: Set within API limits. 1024-100000
Streaming Responses
For long responses, return partial results to improve user experience:
model = genai.GenerativeModel("gemini-2.5-pro")
# Enable streaming
response = model.generate_content(
contents="Explain AI history in 1000 words",
stream=True
)
# Output text progressively
for chunk in response:
if chunk.text:
print(chunk.text, end="", flush=True)
print() # NewlineUse Case: Stream responses to clients via Server-Sent Events (SSE). Users see AI "thinking" in real-time.
Multi-turn Conversation Management
Manage multiple questions and answers as one conversation:
model = genai.GenerativeModel("gemini-2.5-pro")
# Initialize chat history
chat = model.start_chat(history=[])
# Turn 1
user_input_1 = "Teach me about async programming in Python"
response_1 = chat.send_message(user_input_1)
print(f"User: {user_input_1}")
print(f"AI: {response_1.text}\n")
# Turn 2 (can reference previous response)
user_input_2 = "Give 3 examples of asyncio.gather()"
response_2 = chat.send_message(user_input_2)
print(f"User: {user_input_2}")
print(f"AI: {response_2.text}\n")
# Turn 3
user_input_3 = "Which is most practical?"
response_3 = chat.send_message(user_input_3)
print(f"User: {user_input_3}")
print(f"AI: {response_3.text}\n")Important: Conversation context is auto-managed by model.start_chat(). No manual history tracking needed.
Function Calling Basics
Function Calling allows AI to invoke external tools (APIs, databases, functions) as needed.
Tool Definition and Calling Pattern
import json
# Define tools (functions)
tools = [
{
"name": "get_product_info",
"description": "Get product info (price, stock) from product ID",
"input_schema": {
"type": "object",
"properties": {
"product_id": {
"type": "string",
"description": "Product ID (e.g. PROD-12345)"
},
"include_reviews": {
"type": "boolean",
"description": "Include review info (default: false)"
}
},
"required": ["product_id"]
}
},
{
"name": "update_inventory",
"description": "Update inventory count",
"input_schema": {
"type": "object",
"properties": {
"product_id": {"type": "string"},
"quantity_change": {"type": "integer", "description": "Change amount"}
},
"required": ["product_id", "quantity_change"]
}
}
]
# Actual tool implementation
def get_product_info(product_id: str, include_reviews: bool = False) -> dict:
"""Real business logic"""
products_db = {
"PROD-001": {"name": "Laptop", "price": 150000, "stock": 5},
"PROD-002": {"name": "Mouse", "price": 3000, "stock": 50},
}
if product_id not in products_db:
return {"error": "Product not found"}
info = products_db[product_id]
if include_reviews:
info["reviews"] = "4.5 stars (100 reviews)"
return info
def update_inventory(product_id: str, quantity_change: int) -> dict:
"""Inventory update (real: database operation)"""
return {"status": "success", "product_id": product_id, "change": quantity_change}
# API call
model = genai.GenerativeModel(
"gemini-2.5-pro",
tools=tools
)
# User request
user_message = "Reduce PROD-001 inventory by 3 and show latest info"
response = model.generate_content(user_message)
# Handle Function Calling results
for content_part in response.content:
if hasattr(content_part, 'function_call'):
# Get the function AI wants to call
func_call = content_part.function_call
func_name = func_call.name
func_args = {k: v for k, v in func_call.args.items()}
print(f"AI called '{func_name}': {func_args}")
# Execute actual function
if func_name == "get_product_info":
result = get_product_info(**func_args)
elif func_name == "update_inventory":
result = update_inventory(**func_args)
else:
result = {"error": "Unknown function"}
print(f"Result: {result}")
# Return result to AI
second_response = model.generate_content([
user_message,
response,
{"role": "user", "parts": [{"text": f"Function result: {json.dumps(result)}"}]}
])
print(f"AI Final Answer: {second_response.text}")Flow:
User
↓
AI (decides function call needed)
↓
Specify function and arguments
↓
App (execute actual function)
↓
Return result to AI
↓
AI (incorporate result into response)
↓
User
Structured Output (responseSchema) Usage
To always get JSON-formatted responses:
from google.generativeai.types import ResponseSchema, GenerateContentResponse
# Define output schema
schema = [
ResponseSchema(
name="analysis_result",
description="Analysis result",
type="object",
properties=[
ResponseSchema(name="title", description="Title", type="string"),
ResponseSchema(name="summary", description="Summary (max 200 chars)", type="string"),
ResponseSchema(
name="key_points",
description="Important points",
type="array",
items=ResponseSchema(name="point", type="string")
),
ResponseSchema(name="confidence", description="Confidence (0-100)", type="integer")
]
)
]
model = genai.GenerativeModel(
"gemini-2.5-pro",
generation_config=GenerationConfig(
response_mime_type="application/json",
response_schema=schema,
)
)
prompt = """
Analyze this text:
"AI improves diagnosis accuracy in healthcare,
automates anomaly detection in manufacturing,
and strengthens fraud detection in finance"
"""
response = model.generate_content(prompt)
# Directly parse as JSON
import json
result = json.loads(response.text)
print(json.dumps(result, indent=2, ensure_ascii=False))Example Output:
{
"analysis_result": {
"title": "AI's Real-World Application Scope",
"summary": "AI is deployed across healthcare, manufacturing, and finance with measurable impact in diagnosis, anomaly detection, and fraud prevention.",
"key_points": [
"Healthcare: Improved diagnostic accuracy",
"Manufacturing: Automated anomaly detection",
"Finance: Enhanced fraud detection"
],
"confidence": 95
}
}Multimodal Input Fundamentals
Basic Image Analysis
from pathlib import Path
# Load local image
image_path = Path("screenshot.png")
image_data = image_path.read_bytes()
# Detect MIME type
import mimetypes
mime_type, _ = mimetypes.guess_type(str(image_path))
# Send to Gemini
model = genai.GenerativeModel("gemini-2.5-pro")
response = model.generate_content([
"Describe what's in this image. If there's text, translate it to English.",
{
"mime_type": mime_type,
"data": image_data
}
])
print(response.text)Supported Formats:
- Images: PNG, JPEG, GIF, WebP
- Video: MP4, MPEG, MOV, AVI, FLV, MKV, WMV, WEBM (up to 25MB)
- Audio: WAV, MP3, AIFF, AAC, OGG, FLAC
PDF Document Processing
import mimetypes
# Upload PDF
pdf_path = "technical_report.pdf"
with open(pdf_path, "rb") as pdf_file:
pdf_data = pdf_file.read()
model = genai.GenerativeModel("gemini-2.5-pro")
response = model.generate_content([
{
"mime_type": "application/pdf",
"data": pdf_data
},
"Extract from this document:\n1. Main theme\n2. Author\n3. Key conclusions\n4. Reference count"
])
print(response.text)Key Points:
- PDFs up to 1M tokens (all pages) processed at once
- Tables and charts recognized
- OCR-capable for scanned PDFs
Using 1M Token Context
Analyze multiple large files:
import os
from pathlib import Path
# Load multiple PDFs
pdf_files = list(Path("documents/").glob("*.pdf"))
contents = []
# Add prompt first
contents.append({
"text": """Perform integrated analysis of these 3 technical reports:
1. Identify common themes
2. List methodology differences
3. Identify conclusion conflicts
4. State overall recommendations"""
})
# Add all PDFs
for pdf_path in pdf_files[:3]: # First 3 files
with open(pdf_path, "rb") as f:
contents.append({
"mime_type": "application/pdf",
"data": f.read()
})
model = genai.GenerativeModel("gemini-2.5-pro")
response = model.generate_content(contents)
print(response.text)Real-World Uses:
- Compare 5-10 academic papers
- Integrate insights from multiple academic sources
- Analyze multi-year monthly/annual reports
- Check reference consistency in legal documents
Google Workspace Integration Tips
Docs Auto-Document Creation Pattern
from google.auth.transport.requests import Request
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
# Create document via Google Docs API
docs_service = build('docs', 'v1', credentials=credentials)
drive_service = build('drive', 'v3', credentials=credentials)
# Create new document
doc = docs_service.documents().create(body={'title': 'AI Report 2026'}).execute()
doc_id = doc['documentId']
# Generate content with Gemini
model = genai.GenerativeModel("gemini-2.5-pro")
response = model.generate_content(
"Write a 3000-word report on Japan's AI industry state and prospects. Output in markdown."
)
# Insert Gemini output into Docs
requests = [
{
'insertText': {
'text': response.text,
'location': {'index': 1}
}
}
]
docs_service.documents().batchUpdate(
documentId=doc_id,
body={'requests': requests}
).execute()
print(f"Document created: https://docs.google.com/document/d/{doc_id}")Sheets Data Analysis
# Load data from Google Sheets
sheets_service = build('sheets', 'v4', credentials=credentials)
result = sheets_service.spreadsheets().values().get(
spreadsheetId='SHEET_ID',
range='Sheet1!A1:Z100'
).execute()
data = result.get('values', [])
# Analyze with Gemini
model = genai.GenerativeModel("gemini-2.5-pro")
analysis = model.generate_content(f"""
Analyze this sales data:
{data}
Analysis:
1. Top 3 products by revenue
2. Revenue share by region
3. Month-over-month change rate
4. Improvement recommendations
""")
# Record analysis results in Sheets
sheets_service.spreadsheets().values().update(
spreadsheetId='SHEET_ID',
range='Analysis!A1',
valueInputOption='USER_ENTERED',
body={'values': [[analysis.text]]}
).execute()Next Steps — Part 2 (Premium) Topics
Part 1 covered API basics and introductory techniques. Part 2 dives deeper into:
- Parallel Tool Calling: Execute multiple functions simultaneously for performance
- Live API Voice Processing: Real-time voice conversation interface
- Production Agent Systems: Autonomous agents with Gemini 2.5 Pro
- Vertex AI Fine-tuning: Build domain-specific models
- Context Caching: Cost reduction for large inputs
- Veo 3 Video API: Auto-generate video from text
- SaaS Monetization: Business models using Gemini API
Premium articles include production project code examples, performance optimization best practices, and troubleshooting guides.
Discuss your next steps in the Gemini Lab community.