How to Fix Gemini API Error 400 (Bad Request)
Updated 10/3/2026
A 400 Bad Request or INVALID_ARGUMENT error from the Gemini API indicates that the server received a request it could not understand or process. Unlike authentication errors (401/403) or rate limits (429), a 400 error means there is a structural or logical mistake inside your API call. This is almost always a client-side issue that you can fix by modifying your code, payload, or configuration.
Use this step-by-step troubleshooting guide to identify and fix the root causes of Gemini API 400 errors.
1. Verify Your Model Identifier String One of the most common triggers for a 400 error is calling a model that does not exist, has been deprecated, or is misspelled in your initialization code.
- Check the exact string: Ensure you are using current models like gemini-1.5-flash or gemini-1.5-pro. Older model strings like gemini-pro or gemini-ultra are deprecated and will throw a 400 error in newer SDK versions.
- Verify API Version: If you are prepending the API version to your endpoint manually (e.g., /v1beta/models/... vs /v1/models/...), ensure the model you are requesting is supported in that specific release. For example, experimental models are often only accessible via /v1beta endpoints.
2. Validate Generation Config Parameters If your request payload includes generation configurations (like temperature, topK, or topP), an out-of-bounds value will immediately trigger a `400 INVALID_ARGUMENT` response.
Verify that your configuration variables fall within these strict limits: * Temperature: Must be between 0.0 and 2.0 (inclusive). Setting temperature to a negative number or a value higher than 2.0 will fail. * TopP: Must be between 0.0 and 1.0. * TopK: Must be an integer greater than or equal to 1. Note that some models no longer require or support custom topK values; try omitting this parameter if you continue to receive errors. * Max Output Tokens: Must be a positive integer and cannot exceed the model's physical output limit (e.g., 8,192 tokens for Gemini 1.5 models).
3. Check JSON Structure and Payload Nesting If you are raw-coding your HTTP POST requests rather than using an official Google GenAI SDK, your JSON schema must match Google's expectations exactly. A misplaced key or missing wrapper will trigger a 400 error.
* The "contents" wrapper: Every prompt must be wrapped inside a contents array containing parts, which in turn contains a text or inlineData key. * System Instructions: If passing system instructions, make sure they are structured as a separate object outside the chat history list, like this: `json { "contents": [ { "parts": [{"text": "Explain quantum computing in one sentence."}] } ], "systemInstruction": { "parts": [{"text": "You are a friendly high school physics teacher."}] } } ` * Schema Validation: If you are enforcing structured JSON output using responseSchema, verify that your schema object is fully compliant with the OpenAPI 3.0 specification. Invalid property types or unsupported keywords in your schema will cause the API to reject the request.
4. Audit Multimodal Media and Payload Sizes When sending images, audio, or video files inline via the API, format errors are highly common.
- Base64 Formatting: Do not include data URI headers (like data:image/jpeg;base64,) inside your inlineData base64 string. The API expects the raw, clean base64 payload string only.
- Correct MIME Types: Ensure the specified mimeType matches the actual format of the uploaded file (e.g., image/png, application/pdf, audio/mp3).
- Payload Size Limits: Sending massive files inline can exceed the HTTP payload limit. For files larger than a few megabytes, use the Files API (files.upload method) to upload the media to Google's servers first, then reference the file URI in your generation request instead of passing the binary data inline.
5. Upgrade and Align Your SDKs If you are using Python, Node.js, or Go SDKs, out-of-date libraries often send payloads with legacy parameter names that the production Gemini endpoints no longer accept.
- Update the package: Run pip install --upgrade google-genai (for the modern Google GenAI SDK) or npm install @google/genai@latest depending on your stack.
- Avoid mixed libraries: Ensure you are not importing deprecated legacy packages (like the old google-generativeai) alongside the new google-genai SDK, as namespace conflicts can cause unexpected payload serialization errors.