gemini-api
Skill87 starsChanged 7 months agoMITView on GitHub
Google Gemini API patterns for Python and TypeScript. Covers content generation, streaming, tool use (function calling), vision, system instructions, context caching, batch requests, and agent workflows. Use when building applications with the Gemini API or Google Generative AI SDKs.
- Reads credentials
- Installs packages
4 other projects use a version of this.
Adopted by
Most recently taken up by
- google/skillstheir own version, first seen Sep 29, 2026
- drinkspiller/skillstheir own version, first seen Sep 16, 2026
- isaccanedo/skillstheir own version, first seen Aug 4, 2026
- jmxt3/gitscape.aitheir own version, first seen Jun 28, 2026
Other versions4 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 4
- google/skills★ 21ksame name
- jmxt3/gitscape.ai★ 32same name
- drinkspiller/skills★ 1same name
- isaccanedo/skills★ 1same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository182 other files it gives its agents.
- accessibilitySkill
- agent-evalSkill
- agent-harness-constructionSkill
- agentic-engineeringSkill
- agent-introspection-debuggingSkill
- agent-payment-x402Skill
- agent-sortSkill
- ai-first-engineeringSkill
- ai-regression-testingSkill
- android-clean-architectureSkill
- api-connector-builderSkill
- api-designSkill
- architecture-decision-recordsSkill
- article-writingSkill
- automation-audit-opsSkill
- autonomous-agent-harnessSkill
- autonomous-loopsSkill
- backend-patternsSkill
- benchmarkSkill
- blueprintSkill
- brand-voiceSkill
- browser-qaSkill
- bun-runtimeSkill
- canary-watchSkill
- carrier-relationship-managementSkill
- ckSkill
- gemini-devfleetSkill
- clickhouse-ioSkill
- click-path-auditSkill
- codebase-onboardingSkill
- code-tourSkill
- coding-standardsSkill
- compose-multiplatform-patternsSkill
- configure-eccSkill
- connections-optimizerSkill
- content-engineSkill
- content-hash-cache-patternSkill
- context-budgetSkill
- continuous-agent-loopSkill
- continuous-learningSkill
- continuous-learning-v2Skill
- cost-aware-llm-pipelineSkill
- councilSkill
- cpp-coding-standardsSkill
- cpp-testingSkill
- crosspostSkill
- csharp-testingSkill
- customer-billing-opsSkill
- customs-trade-complianceSkill
- dart-flutter-patternsSkill
- dashboard-builderSkill
- database-migrationsSkill
- data-scraper-agentSkill
- deep-researchSkill
- defi-amm-securitySkill
- deployment-patternsSkill
- design-systemSkill
- django-patternsSkill
- django-securitySkill
- django-tddSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
2 rules. 0 are common, 2 are distinctive, and 0 are very specific to this project.
0
Common
Most of the 4 edited versions we read keep these.
2
Distinctive
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 27
Default to Flash 2.5 unless the task requires deep reasoning (Pro) or speed/cost optimization (Flash Lite).
- Distinctive: line 354
Never hardcode API keys. Always use environment variables.
“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Gemini API
Build applications with the Google Gemini API and SDKs.
When to Use
- Building applications that call the Gemini API
- Code imports
google.generativeai(Python) or@google/generative-ai(TypeScript) - User asks about Gemini API patterns, function calling, streaming, or vision
- Implementing agent workflows with the Gemini API
- Optimizing API costs, token usage, or latency
Model Selection
| Model | ID | Best For |
| Pro 2.5 | gemini-2.5-pro | Complex reasoning, architecture, research |
| Flash 2.5 | gemini-2.5-flash | Balanced coding, most development tasks |
| Flash Lite 2.5 | gemini-2.5-flash-lite | Fast responses, high-volume, cost-sensitive |
Default to Flash 2.5 unless the task requires deep reasoning (Pro) or speed/cost optimization (Flash Lite). For production, prefer pinned snapshot IDs over aliases.
Python SDK
Installation
pip install google-generativeaiBasic Message
import os
import google.generativeai as genai
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content("Explain async/await in Python")
print(response.text)Streaming
model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content(
"Write a haiku about coding",
stream=True,
)
for chunk in response:
print(chunk.text, end="", flush=True)System Instruction
model = genai.GenerativeModel(
"gemini-2.5-flash",
system_instruction="You are a senior Python developer. Be concise.",
)
response = model.generate_content("Review this function")TypeScript SDK
Installation
npm install @google/generative-aiBasic Message
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
const result = await model.generateContent("Explain async/await in TypeScript");
console.log(result.response.text());Streaming
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
const result = await model.generateContentStream("Write a haiku");
for await (const chunk of result.stream) {
const text = chunk.text();
process.stdout.write(text);
}Function Calling (Tool Use)
Define tools and let Gemini call them:
def get_weather(location: str, unit: str = "celsius") -> dict:
"""Get current weather for a location."""
# Your implementation here
return {"temp": 18, "unit": unit, "location": location}
model = genai.GenerativeModel(
"gemini-2.5-flash",
tools=[get_weather],
)
chat = model.start_chat(enable_automatic_function_calling=True)
response = chat.send_message("What's the weather in SF?")
print(response.text)For manual function calling (more control):
import google.generativeai as genai
from google.generativeai.types import FunctionDeclaration, Tool
weather_func = FunctionDeclaration(
name="get_weather",
description="Get current weather for a location",
parameters={
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
)
tool = Tool(function_declarations=[weather_func])
model = genai.GenerativeModel("gemini-2.5-flash", tools=[tool])
chat = model.start_chat()
response = chat.send_message("What's the weather in SF?")
# Check for function call in response
for part in response.parts:
if fn := part.function_call:
# Execute the function with fn.args
result = get_weather(**dict(fn.args))
# Send result back
response = chat.send_message(
genai.protos.Content(
parts=[genai.protos.Part(
function_response=genai.protos.FunctionResponse(
name=fn.name,
response={"result": result},
)
)]
)
)Vision
Send images for analysis:
import PIL.Image
image = PIL.Image.open("diagram.png")
model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content(
["Describe this diagram", image]
)
print(response.text)Or from raw bytes:
with open("diagram.png", "rb") as f:
image_data = f.read()
response = model.generate_content([
"Describe this diagram",
{"mime_type": "image/png", "data": image_data},
])Thinking (Extended Reasoning)
For complex reasoning tasks, enable thinking in the generation config:
model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content(
"Solve this math problem step by step...",
generation_config=genai.GenerationConfig(
thinking_config=genai.types.ThinkingConfig(
thinking_budget=10000,
),
),
)
for part in response.candidates[0].content.parts:
if part.thought:
print(f"Thinking: {part.text}")
else:
print(f"Answer: {part.text}")Context Caching
Cache large contexts to reduce costs on repeated requests:
from google.generativeai import caching
import datetime
cache = caching.CachedContent.create(
model="gemini-2.5-flash",
display_name="my-cached-context",
system_instruction="You are an expert analyst.",
contents=[large_context_text],
ttl=datetime.timedelta(minutes=30),
)
# Use the cached content with a model
model = genai.GenerativeModel.from_cached_content(cache)
response = model.generate_content("Question about the cached context")Batch Requests
Process multiple requests efficiently:
model = genai.GenerativeModel("gemini-2.5-flash")
# Use asyncio for concurrent requests
import asyncio
async def process_batch(prompts: list[str]) -> list[str]:
"""Process multiple prompts concurrently."""
async_model = genai.GenerativeModel("gemini-2.5-flash")
tasks = [
async_model.generate_content_async(prompt)
for prompt in prompts
]
responses = await asyncio.gather(*tasks)
return [r.text for r in responses]
# Run the batch
results = asyncio.run(process_batch(prompts))Agent Loop
Build multi-step agents with function calling:
import google.generativeai as genai
# Define tools as function declarations
tools = [
genai.protos.Tool(function_declarations=[
genai.protos.FunctionDeclaration(
name="search_codebase",
description="Search the codebase for relevant code",
parameters={
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
)
])
]
model = genai.GenerativeModel("gemini-2.5-flash", tools=tools)
chat = model.start_chat()
response = chat.send_message("Review the auth module for security issues")
# Agentic loop: keep processing function calls until the model stops
while response.candidates[0].finish_reason.name == "STOP" and any(
part.function_call for part in response.parts
):
for part in response.parts:
if fn := part.function_call:
result = execute_tool(fn.name, dict(fn.args))
response = chat.send_message(
genai.protos.Content(
parts=[genai.protos.Part(
function_response=genai.protos.FunctionResponse(
name=fn.name,
response={"result": result},
)
)]
)
)
print(response.text)Cost Optimization
| Strategy | Savings | When to Use |
| Context caching | Up to 75% on cached tokens | Repeated system prompts or context |
| Batch with async | Variable | Non-time-sensitive bulk processing |
| Flash Lite instead of Flash | ~75% | Simple tasks, classification, extraction |
| Shorter max_output_tokens | Variable | When you know output will be short |
| Streaming | None (same cost) | Better UX, same price |
Error Handling
import time
from google.api_core.exceptions import (
ResourceExhausted,
ServiceUnavailable,
GoogleAPIError,
)
try:
response = model.generate_content(...)
except ResourceExhausted:
# Rate limited, back off and retry
time.sleep(60)
except ServiceUnavailable:
# Service issue, retry with backoff
pass
except GoogleAPIError as e:
print(f"API error: {e.message}")Environment Setup
# Required
export GEMINI_API_KEY="your-api-key-here"
# Optional: set default model
export GEMINI_MODEL="gemini-2.5-flash"Never hardcode API keys. Always use environment variables.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

