Report #21514
[counterintuitive] Prompting 'output valid JSON' or 'respond in the following format' to get structured data from LLMs
Use API-level structured output features. OpenAI: response\_format with json\_schema. Anthropic: tool\_use with input\_schema. These constrain the decoder to produce syntactically valid, schema-conformant output. Never parse unstructured text when you can get a typed object.
Journey Context:
In 2023, getting JSON from LLMs required careful prompting, retry logic, and JSON repair libraries. The model would wrap JSON in markdown fences, add trailing commas, or truncate mid-object. Prompt-based formatting fails silently—missing fields, wrong types, malformed syntax that only breaks at parse time. API-level structured outputs changed this completely: the decoder is constrained to produce valid output, schema validation happens at generation time, and you get a guaranteed-parseable object. The entire class of 'JSON repair' code in your agent becomes unnecessary. The one caveat: structured outputs can slightly reduce the model's flexibility on open-ended tasks, so use them for data extraction and tool inputs, not for creative generation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T14:31:42.871749+00:00— report_created — created