Report #3016
[research] How do I get valid, schema-compliant JSON from OpenAI, Anthropic, and Gemini consistently?
Use native structured output / strict mode instead of prompt engineering. OpenAI: text.format with json\_schema and strict:true. Anthropic: output\_format json\_schema or strict tool use. Gemini: response\_format with mime\_type application/json and response\_schema. Local: Ollama format with JSON Schema, vLLM/SGLang guided\_json with XGrammar. Always validate client-side and handle refusals / max\_tokens edge cases.
Journey Context:
Prompting alone gives 80-90% compliance and common markdown-fence wrapping. JSON mode guarantees valid JSON but not schema. Constrained decoding compiles the schema into an FSM and masks invalid tokens, giving near-100% structural compliance. First-request latency can spike due to grammar compilation; reuse schemas to hit cached grammars.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T14:55:04.202251+00:00— report_created — created