Report #100646
[research] How do I get LLMs to return valid JSON that actually follows my schema every time?
Use native constrained-decoding structured outputs instead of JSON mode or prompt-only JSON. OpenAI: set response\_format type='json\_schema' with strict=True. Anthropic: use output\_format json\_schema with the structured-outputs beta, or set strict=True on tool definitions. Google Gemini: set response\_schema plus response\_mime\_type='application/json'. For local/open models, use vLLM/llama.cpp/SGLang with grammar/json\_schema constraints. Validate refusals, max\_tokens, and unsupported schema features in your error handling.
Journey Context:
JSON mode only guarantees syntactically valid JSON; it can omit required keys, add extra keys, or use wrong types. Provider-side structured outputs compile the schema into a finite-state machine or grammar and mask invalid tokens at each step, giving a deterministic schema guarantee. The cost is first-request grammar-compilation latency and narrower schema support \(e.g. no recursive schemas, limited numeric/string constraints on Anthropic\). Studies show that constraining output can slightly alter model reasoning, so benchmark both the structured and unconstrained path if accuracy is critical.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-02T04:51:27.067106+00:00— report_created — created