Gemini structured output returns 400 INVALID_ARGUMENT. Simplify the schema
A 400 INVALID_ARGUMENT means the JSON schema is too complex or still on response_schema. Flatten it and use response_json_schema or response_format.schema. Short answer: 5 steps. 1) Confirm it’s a schema rejection, not an API key or safety block; 2) Use the JSON Schema path your SDK recommends; 3) Flatten nesting and bound recursion; 4) Cut branching: optionals, big enums, and anyOf; 5) Validate in your app for unenforced keywords.
Last updated: · This page is the decision checklist; the tools list for “Fix Gemini structured-output schema 400s” is a separate URL.
How do I simplify the schema when Gemini returns 400 INVALID_ARGUMENT?
Confirm it’s a schema rejection, not an API key or safety block
Read the 400 body. Schema/complexity failures mention INVALID_ARGUMENT around generation config or schema fields. That is different from an invalid API key and from safety blocks. Structured outputs guide: https://ai.google.dev/gemini-api/docs/structured-output
Use the JSON Schema path your SDK recommends
Prefer `response_json_schema` (or the Interactions/SDK `response_format` with `mime_type: application/json` and a JSON Schema / Pydantic `model_json_schema()` / Zod schema) over the older proto-style `response_schema` field when you need full JSON Schema features such as nested `additionalProperties` or `"type": ["string", "null"]`. Google’s structured-outputs update documents JSON Schema support across actively supported Gemini models.
Flatten nesting and bound recursion
Constrained decoding compiles your schema into a state machine with limits on states, branching, and depth. Replace deep object trees with flatter shapes. If you use `$defs`/`$ref` recursion, make the recursive reference optional/nullable and avoid required recursion or `minItems` > 0 on recursive arrays.
Cut branching: optionals, big enums, and anyOf
Optional properties, large `enum` lists, and `anyOf` unions multiply grammar paths. Mark fields `required` where you can, allow `null` instead of omitting optionals, trim enums, and move soft rules into `description` text plus app-side validation. Shorten property names; drop `minItems`/`maxItems`/`minimum`/`maximum` when they only inflate the state space.
Validate in your app for unenforced keywords
Gemini ignores some JSON Schema keywords during constrained decoding (`minLength`, `maxLength`, `minProperties`, `maxProperties`, `uniqueItems`, and related contains constraints)—it strips them rather than erroring. Keep those checks in Pydantic/Zod after you get a 200. Retry with a minimal schema first, then grow it until the 400 returns so you know which branch blew the budget.
Which tools are best to fix Gemini structured-output schema 400s?
Structured outputs
JSON Schema support, limitations, and how to simplify rejected schemas.
Gemini API troubleshooting
Maps common API errors including INVALID_ARGUMENT paths.
Structured outputs announcement
JSON Schema support and property-ordering updates for Gemini models.
Google AI Studio
Try a minimal schema interactively before changing production clients.
Open: 1 spot per week
One labeled sponsor for this topic each week; this week's spot is open. See open weeks →
All tools for “Fix Gemini structured-output schema 400s” →
Related fixes
- Stop a Cursor agent loopHow to fix a Cursor agent loop (same edit, or work after Stop)Fix →
- Claude Code vs Codex vs Cursor ~$20 plansClaude Code vs Codex vs Cursor $20 plan limits: how to pick without burning the weekFix →
- Scan a vibe-coded app for leaked API keysCheck a vibe-coded app for leaked API keys before you share itFix →
- Add an MCP server to Claude Code, Cursor, or CodexHow to add an MCP server to Claude Code, Cursor, or CodexFix →
See also AI tools launched today on the daily board.
NewAIToolsToday Daily
Top 3 AI launches, one model or news item that matters, and one simple fix. A 5-minute read, every weekday.