Whether you're just getting started with the Gemini API or upgrading to a newer model, the first roadblock is usually the same: code that looks right, documentation that seems to match — and an error that doesn't explain itself clearly.
Here are the five most common Gemini API errors, what's actually causing them, and how to fix each one.
Error 1: models/gemini-pro is not found or 404 NOT_FOUND
What's happening
The model names gemini-pro and gemini-1.0-pro have been retired. Requests using these names return a 404 error.
[GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/...
Status: 404 Not Found
{"error":{"code":404,"message":"models/gemini-pro is not found for API version v1beta"}}
Why it happens
Older tutorials and GitHub samples often reference deprecated model names. Even when official documentation is updated, the code people are following isn't.
How to fix it
Switch to a currently supported model name. As of April 2026:
# ❌ Old (broken)
model = genai.GenerativeModel('gemini-pro')
model = genai.GenerativeModel('gemini-1.0-pro')
# ✅ Current
model = genai.GenerativeModel('gemini-2.5-pro-latest') # Latest Gemini 2.5 Pro
model = genai.GenerativeModel('gemini-2.0-flash') # Fast and cost-efficient
model = genai.GenerativeModel('gemini-2.5-flash-preview-04-17') # 2.5 Flash (preview)To see what models are available to your project:
import google.generativeai as genai
genai.configure(api_key="YOUR_GEMINI_API_KEY")
for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)Error 2: 400 INVALID_ARGUMENT — Request Format Issues
What's happening
The request structure doesn't match the API spec. This is especially common when migrating from Gemini 1.x to 2.x — the API format for certain parameters changed.
Pattern A: Old system_instruction format
# ❌ Gemini 1.x style (may fail in 2.x)
response = model.generate_content(
[{"role": "system", "parts": [{"text": "You are a helpful assistant"}]},
{"role": "user", "parts": [{"text": "Hello"}]}]
)
# ✅ Gemini 2.x recommended
model = genai.GenerativeModel(
model_name='gemini-2.5-pro-latest',
system_instruction="You are a helpful assistant"
)
response = model.generate_content("Hello")Pattern B: Unsupported parameter in generation_config
# ❌ topK not supported on all models
config = genai.GenerationConfig(topK=40)
# ✅ Stick to core parameters
config = genai.GenerationConfig(
temperature=0.7,
max_output_tokens=1024
)How to fix it
Check the details field in the error response — it usually specifies which field is causing the problem. Then verify against the official generate content reference.
Error 3: 429 RESOURCE_EXHAUSTED — Rate Limit Hit
What's happening
You've hit the API rate limit for your project tier.
google.api_core.exceptions.ResourceExhausted: 429 RESOURCE_EXHAUSTED
Quota exceeded for quota metric 'generate_content_requests'
and limit 'generate_content_requests_per_day_per_project'
The three rate limits and how to handle each
| Limit Type | Free Tier (reference) | Fix |
|---|---|---|
| Requests per minute (RPM) | 15 RPM | Add time.sleep() between requests |
| Tokens per minute (TPM) | 1,000,000 TPM | Shorten prompts |
| Requests per day (RPD) | 1,500 RPD | Wait until tomorrow or upgrade |
For any script that makes multiple API calls, implement retry logic:
import time
import google.api_core.exceptions
def generate_with_retry(model, prompt, max_retries=3):
for attempt in range(max_retries):
try:
return model.generate_content(prompt)
except google.api_core.exceptions.ResourceExhausted:
wait_time = 60 * (attempt + 1) # 60s, 120s, 180s
print(f"Rate limited. Waiting {wait_time}s...")
time.sleep(wait_time)
raise Exception("Max retries reached")If you're consistently hitting the daily 1,500-request limit, upgrading to a paid plan through Google Cloud is the most sustainable fix.
Error 4: SAFETY Block — Empty Response
What's happening
Gemini's safety filters have blocked the response. The finish_reason is SAFETY, and no text is returned — which usually surfaces as an AttributeError or ValueError when accessing response.text.
How to fix it
Check finish_reason before accessing the text:
response = model.generate_content("...")
candidate = response.candidates[0]
print(f"Finish reason: {candidate.finish_reason}")
print(f"Safety ratings: {candidate.safety_ratings}")
if candidate.finish_reason.name == 'SAFETY':
print("Response blocked by safety filter")
# Revise prompt or adjust safety_settingsYou can adjust safety_settings to relax filtering thresholds for specific categories. Note that this adjustment requires the Google Cloud API endpoint (not Google AI Studio) and should be used carefully.
Error 5: APIKeyInvalidError or Authentication Failure
What's happening
The API key is missing, expired, or belongs to a different project.
google.api_core.exceptions.PermissionDenied: 403 API_KEY_INVALID
Verification steps
- Go to Google AI Studio → "Get API key" to confirm the key is still valid
- Ensure the key is set correctly as an environment variable:
import os
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
raise ValueError("GEMINI_API_KEY environment variable is not set")
genai.configure(api_key=api_key)-
Never hardcode API keys in source files — if committed to Git, Google's secret scanning will automatically invalidate it. Always use environment variables or a
.envfile. -
If using Google Cloud authentication, verify your
GOOGLE_APPLICATION_CREDENTIALSpath and that the service account has thegenerativelanguage.googleapis.comAPI enabled.
Migration Checklist: Moving to gemini-2.5-pro-latest
Key things to verify when upgrading:
- Update
system_instructionto the new constructor-level format - Optionally add
thinking_configto control the reasoning/thinking process - The context window is significantly larger — update any hardcoded length checks
- Higher output token counts mean you'll hit TPM limits faster with large batches
When a Gemini API error appears, the first thing to check is the HTTP status code (400/403/404/429). That narrows it down to one of these five categories, and the fix is usually straightforward once you know which one you're dealing with.