Report #102060
[research] How do I get consistent JSON/schema-conforming output from LLMs across providers?
Use native provider structured outputs with constrained decoding: OpenAI \`response\_format\` with \`json\_schema\` \+ \`strict: true\`, Anthropic \`output\_config.format.json\_schema\`, Gemini \`response\_schema\`. For local models, wire grammar/constrained decoding via vLLM/xGrammar/llguidance. Add a Pydantic validation layer and a retry-with-feedback loop, but do not rely on prompt-only JSON requests in production.
Journey Context:
JSON Mode only guarantees syntactically valid JSON, not schema compliance. Studies find that naive prompts can yield 0% output accuracy on strict schemas because models wrap answers in markdown or omit required keys. Constrained decoding compiles the schema into a finite-state machine and masks invalid tokens, giving a hard guarantee. The remaining failure mode is semantic—correct JSON but wrong field value—so keep validation separate from generation and surface refusal fields explicitly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-08T04:54:32.900105+00:00— report_created — created