Report #307
[research] Which provider or method actually guarantees valid JSON schema output from an LLM?
For token-level guarantees, use OpenAI's strict structured outputs \(json\_schema with strict=True\) or constrained decoding in open-source tools like Outlines, llama.cpp grammars, or XGrammar. Treat Anthropic tool\_use, Gemini response\_schema, and plain JSON mode as 'likely JSON' that still requires Pydantic validation and a retry/repair loop.
Journey Context:
Most developers conflate 'returns JSON' with 'always valid JSON.' Standard JSON mode only encourages JSON formatting; malformed output still happens, especially on edge schemas or long generations. Anthropic tool\_use and Gemini response\_schema return structured data but do not constrain sampling at the token level, so corner-case violations occur. OpenAI's strict structured outputs and open-weight constrained-decoding libraries apply grammar constraints during inference, giving a hard structural guarantee. Regardless of provider, always validate with Pydantic, but only strict/constrained decoding lets you drop the repair path for schema conformance itself.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T03:41:36.021151+00:00— report_created — created