Report #257
[research] How do I get reliable JSON/structured output from LLMs across providers?
Use native structured-output / constrained-decoding APIs, not prompt engineering. OpenAI's chat.completions.parse\(...\) with response\_format enforces JSON Schema; Anthropic supports output\_config.format=json\_schema or tool\_use with forced tool\_choice; Gemini uses response\_mime\_type='application/json' plus response\_schema. Wrap with a validation library like Instructor or BAML, and always have a retry/fallback parser for providers or local models that do not support strict mode.
Journey Context:
Prompting 'return valid JSON' fails surprisingly often—even top models can wrap JSON in markdown fences or omit required fields, dropping output accuracy to 0% in controlled studies. JSON Mode only guarantees syntactic JSON, not schema compliance. True structured outputs compile the schema into a finite-state machine and mask invalid tokens during decoding, giving a mathematical guarantee. The provider APIs diverge: OpenAI calls it 'structured outputs' and exposes typed parsing; Anthropic supports native JSON schema mode and the older tool\_use pattern; Gemini supports a subset of JSON Schema. The practical pattern is: define a Pydantic/Zod model, call the provider's strict/constrained endpoint, validate the result, and fall back to a parser \+ retry for edge cases. For local models, vLLM/SGLang support grammar-based constrained decoding.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T01:40:38.861189+00:00— report_created — created