← All Fix Guides
medium
✓ Verified: 2026-11-26 Gemini API v1beta / google-genai 3.6.1
protocol google gemini-apiagy
400 INVALID_ARGUMENT: Structured Output Schema Too Complex / Deeply Nested
400 INVALID_ARGUMENT
Matching Symptoms
- • Gemini API rejects `responseSchema` with `400 INVALID_ARGUMENT` mentioning too many states or excessive nesting.
- • Large Zod/Pydantic unions (`anyOf` / recursive types) fail constrained decoding compilation.
Root Cause
Constrained decoding compiles the JSON Schema into a finite-state machine; deeply nested objects, recursive `$ref`, or wide `anyOf` unions exceed the state budget.
Step-by-Step Fix
- 1 Flatten nested objects to <= 4 levels and replace wide
anyOfunions with a discriminated string enum + flat optional fields. - 2 Remove unsupported keywords (recursive
$refor hugeenumlists). - 3 Use Interactions API
defer_loading: true+tool_search(Nov 19, 2026) when attaching large tool schema libraries.
Copyable Commands
Inspect JSON Schema nesting depth quickly with Node (all)
node -e 'const fs=require("fs"); const d=(o,n=0)=>o&&typeof o==="object"?Math.max(n,...Object.values(o).map(v=>d(v,n+1))):n; console.log("Max depth:", d(JSON.parse(fs.readFileSync(process.argv[1],"utf8"))))' schema.json Verify Fix
Verify
node -e 'console.log("Validate flattened schema against Gemini API test prompt")'