← 返回全部急救指南
medium
✓ 实测验证日期: 2026-11-26 Gemini API v1beta / google-genai 3.6.1
protocol google gemini-apiagy
400 INVALID_ARGUMENT:结构化输出 Schema 嵌套过深或状态过多
400 INVALID_ARGUMENT
常见症状特征
- • 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.
故障根因
受限解码会将 JSON Schema 编译为有限状态机;嵌套过深、递归 `$ref` 或过宽的 `anyOf` 联合类型会超出状态机编译上限。
分步修复步骤
- 1 将嵌套层级压平至 <= 4 层,并用枚举标识字段 + 扁平可选字段替代复杂的
anyOf联合。 - 2 移除递归
$ref或包含数百项的超大enum列表。 - 3 若因挂载大量工具导致 Schema 膨胀,可在 Interactions API 中开启 Tool Retrieval(
defer_loading: true+tool_search)。
一键复制修复命令
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
node -e 'console.log("Validate flattened schema against Gemini API test prompt")'