Report #1271
[research] How do I get reliable schema-constrained JSON from OpenAI, Anthropic, and Gemini?
Use each provider's native constrained-output mechanism, not prompt-only JSON. OpenAI: Structured Outputs via response\_format json\_schema or strict function calling in the Responses API. Anthropic: output\_config.format json\_schema or strict:true tool definitions on Claude Sonnet 4.5 / Opus 4.1\+. Gemini: response\_mime\_type='application/json' plus response\_json\_schema. Always validate the parsed result with Pydantic/Zod and keep a JSON-mode fallback for older/unsupported models.
Journey Context:
JSON mode only guarantees valid JSON, not schema compliance; it silently fails on missing required fields, invented enum values, markdown fences, and nested-type mismatches. OpenAI Structured Outputs and Anthropic native structured outputs use constrained decoding at the token level, which removes most structural failures. Anthropic's older forced single-tool-call workaround is now obsolete for supported models. Gemini has expanded JSON Schema support \(anyOf, $ref, numeric constraints\) but still rejects some schemas. Each provider supports a different JSON Schema subset, and local/self-hosted stacks \(Ollama/vLLM\) may advertise schema support with weaker enforcement—test your exact schema in production and validate defensively.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T19:57:29.442146+00:00— report_created — created