GEMINI LABJP
NANOLITE — Nano Banana 2 Lite is here: Google's fastest and most cost-efficient Gemini Image model, made for running lightweight image generation cheaplyOMNIFLASH — Gemini Omni Flash is in public preview, a natively multimodal model that lets enterprises and developers build custom, dynamic video workflowsAGENTS — Managed Agents expand with background: true for async server-side runs and polling, remote MCP server integration, and refreshing credentials across interactionsMEMORY — The Memory Bank IngestEvents API is generally available, decoupling event ingestion from memory generation so you can stream content continuouslyTHROUGHPUT — Provisioned Throughput now lets you submit up to seven pending orders for the same model and regionDEPRECATE — Image generation models shut down on August 17, and the Grok 4.1 family on the Gemini Enterprise Agent Platform on August 20NANOLITE — Nano Banana 2 Lite is here: Google's fastest and most cost-efficient Gemini Image model, made for running lightweight image generation cheaplyOMNIFLASH — Gemini Omni Flash is in public preview, a natively multimodal model that lets enterprises and developers build custom, dynamic video workflowsAGENTS — Managed Agents expand with background: true for async server-side runs and polling, remote MCP server integration, and refreshing credentials across interactionsMEMORY — The Memory Bank IngestEvents API is generally available, decoupling event ingestion from memory generation so you can stream content continuouslyTHROUGHPUT — Provisioned Throughput now lets you submit up to seven pending orders for the same model and regionDEPRECATE — Image generation models shut down on August 17, and the Grok 4.1 family on the Gemini Enterprise Agent Platform on August 20
Articles/API / SDK
API / SDK/2026-04-09Intermediate

Classify Gemini API Errors by Status — Handling 429, SAFETY, and Token Limits in Production

Fix common Gemini API errors including 429 rate limits, 400 bad requests, 401/403 authentication errors, and 500 server errors. A practical troubleshooting guide for developers getting started with Gemini API.

gemini-api277troubleshooting82error15rate-limit4

Premium Article

On the first day I wired the Gemini API into one of my indie apps, what cost me the most time wasn't the error codes themselves — it was deciding whether a given failure would clear up if I waited, or whether I had to go fix my code. A 429 tells you something is wrong, but not whether it's a rate limit or an auth problem until you read the status in the response.

This walkthrough organizes the errors you'll hit by their status and finish_reason, then turns them into handling that won't stall in production — with the code I actually use. The goal isn't a list of errors; it's being able to act without hesitation the moment one shows up.

Common Gemini API Errors

When working with the Gemini API, you'll likely run into one of these error codes:

  • 400 Bad Request — Your request is malformed or missing required fields
  • 401/403 Unauthorized/Forbidden — Authentication failed or credentials are invalid
  • 429 Too Many Requests — You've hit the rate limit
  • 500/503 Internal Server Error — Google's servers are experiencing issues

The good news is that once you understand what each error means, most of them are easy to fix.

400 Bad Request — Invalid Requests and Wrong Model Names

A 400 error means the API can't understand your request. The three most common causes are model name mistakes, malformed JSON, and input that's too long.

1. Using the wrong model name

Typos or outdated model names are the number-one cause of 400 errors. The Gemini API only accepts specific model names.

import google.generativeai as genai
 
genai.configure(api_key="YOUR_GEMINI_API_KEY")
 
# ❌ Wrong: Model doesn't exist
model = genai.GenerativeModel("gemini-invalid-model")
 
# ✅ Correct: Use a real model name
model = genai.GenerativeModel("gemini-2.0-flash")
response = model.generate_content("Hello, world!")
print(response.text)

You can find the latest available models on the Google AI Studio model selection page. Here are the most commonly used ones:

  • gemini-2.0-flash — Fast and efficient
  • gemini-2.0-pro — Higher quality responses
  • gemini-2.0-flash-thinking-exp-01-21 — Extended thinking capabilities

2. Malformed JSON in your request

When using TypeScript or JavaScript, if your request body's JSON structure isn't correct, you'll get a 400 error. Make sure all required fields are present.

import { GoogleGenerativeAI } from "@google/generative-ai";
 
const genAI = new GoogleGenerativeAI("YOUR_GEMINI_API_KEY");
const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });
 
// ✅ Correct request structure
const response = await model.generateContent({
  contents: [
    {
      role: "user",
      parts: [{ text: "Hello, world!" }],
    },
  ],
});

Pay special attention to required fields like contents, parts, and role. Missing any of these will cause a 400 error.

3. Input text exceeds maximum length

Very long text inputs can trigger a 400 error. Each Gemini model has a context window limit (the maximum amount of text you can send):

  • gemini-2.0-flash — Up to 1 million tokens

If you need to process large amounts of text, split it into smaller chunks and make multiple requests.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
A single error handler that classifies exceptions by status and finish_reason to decide retry vs. fail
Splitting 429s across RPM, RPD, and TPM, with jittered exponential backoff to avoid retry storms
Turning empty responses, SAFETY, RECITATION, and context overflow into cause-specific fixes
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

API / SDK2026-05-31
Why Gemini API Throws 'Unsupported MIME type' and How to Fix It
The 'Unsupported MIME type' error from the Gemini API has three distinct causes: a misspelled MIME string, an octet-stream upload, and a genuinely unsupported format. Here is how to tell them apart with code that actually works.
API / SDK2026-05-30
Why Gemini 2.5 Pro Rejects thinkingBudget: 0 (and How to Fix It)
Setting thinkingBudget to 0 on Gemini 2.5 Pro returns a 400 INVALID_ARGUMENT error. Here is why the per-model thinking budget ranges differ, how to minimize thinking on Pro the right way, and when to switch to Flash, with Python and JavaScript examples.
API / SDK2026-04-16
Gemini API System Instructions Not Working — 4 Common Causes and How to Fix Them
Set up System Instructions but the model keeps ignoring them? This guide covers the 4 most common reasons why system prompts fail in Gemini API — from wrong parameter placement to multi-turn drift — with working code examples.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →