Report #36559
[counterintuitive] Prompting 'Return ONLY valid JSON' and relying on the LLM text output to be perfectly formatted
Use API-level JSON mode or structured output schemas \(e.g., response\_format: \{ type: 'json\_object' \} or json\_schema\) to guarantee syntax.
Journey Context:
LLMs are probabilistic text generators. Even with 'ONLY JSON', they frequently add markdown fences, trailing commas, or escape characters that break parsers. Prompting for JSON is fragile and requires extensive regex/try-catch handling. API-level structured outputs constrain the decoding process at the engine level, guaranteeing valid syntax and schema adherence, eliminating the need for defensive parsing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T15:50:26.416198+00:00— report_created — created