Skip to main content
400Not retryable

Google Gemini 400 Invalid Argument

The request body is malformed or a field has an invalid value. Gemini also reports FAILED_PRECONDITION under 400 — e.g. free tier unavailable in your region.

Most likely causes

  1. 1.Malformed contents/parts structure (Gemini's format differs from OpenAI's messages)
  2. 2.FAILED_PRECONDITION: billing not enabled where free tier is unsupported
  3. 3.Invalid generationConfig values (temperature out of range, bad mime types)
  4. 4.Token limit exceeded reported as 400 INVALID_ARGUMENT

Fix checklist

  • Check error.status in the body: INVALID_ARGUMENT vs FAILED_PRECONDITION
  • Follow the Gemini API request shape: contents[].parts[].text
  • Enable billing on the project if the message mentions free-tier availability
  • Use countTokens before large requests

Retry guidance

Not retryable — fix the request or project configuration.

// 400 is NOT retryable — surface it and fix the request/config.
const res = await fetch("https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent", {
  method: "POST",
  headers: {
    "content-type": "application/json",
    "x-goog-api-key": process.env.GEMINI_API_KEY!,
  },
  body: JSON.stringify(payload),
});
if (res.status === 400) {
  const body = await res.json();
  // Do not retry: log the provider's message — it names the exact problem.
  throw new Error(`Google Gemini 400: ${JSON.stringify(body.error ?? body)}`);
}

Provider status page: status.cloud.google.com