Report #38567
[counterintuitive] How to force LLM to output valid JSON
Use native API structured outputs \(JSON mode / tool schemas\) instead of prompt instructions like 'Output ONLY valid JSON'.
Journey Context:
Prompting for JSON was standard in 2023, but models frequently hallucinated trailing commas or unescaped characters, breaking parsers. Modern APIs constrain the decoding step to guarantee schema validity, making prompt-based JSON formatting obsolete and brittle. Prompt-based schemas also fail to handle optional fields or complex nesting reliably.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:12:49.897134+00:00— report_created — created