Report #97869
[research] How do I get reliable JSON/schema-compliant output from LLMs?
Use native Structured Outputs / JSON Schema with strict constrained decoding where available \(OpenAI gpt-4o-mini/gpt-4o-2024-08-06\+, Gemini, Anthropic\). Prefer json\_schema with strict: true over json\_object; if the provider lacks strict mode, use a client-side constrained-decoding library \(Outlines, Instructor, XGrammar, SGLang/vLLM\). For OpenAI strict mode, set additionalProperties: false and mark every property as required. Validate with Pydantic after the call regardless.
Journey Context:
JSON Mode only guarantees syntactically valid JSON, not schema compliance. OpenAI's Structured Outputs compiles the schema into an FSM and zeroes invalid token logits, giving a deterministic guarantee rather than a statistical one. Anthropic and Gemini followed with constrained decoding. However, strict modes reject many valid JSON Schema features—OpenAI forbids allOf, optional fields, and missing additionalProperties: false. The ExtractBench study found structured-output APIs can actually reduce validity vs. prompt-based extraction on complex schemas because providers reject complex schemas outright. The practical path: use strict native mode for simple shapes, fall back to grammar-based constrained decoding locally for unsupported schemas, and always parse/validate downstream. Streaming is possible but chunks are not individually schema-valid.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-26T04:50:16.279384+00:00— report_created — created