Report #6933
[agent\_craft] Model generates malformed JSON or adds markdown fences around structured output
Use the native 'json\_mode' or 'structured\_outputs' API parameter with strict schema validation, rather than prompting 'respond with JSON'. Supply the JSON schema in the response\_format parameter, not just in the text prompt.
Journey Context:
Asking for JSON in the prompt leaves the model free to wrap it in markdown code blocks \(\`\`\`json\) or hallucinate invalid syntax. OpenAI's JSON mode \(response\_format: \{type: 'json\_object'\}\) and the newer Structured Outputs feature constrain the token sampler to valid JSON adhering to the provided schema. This eliminates parsing failures and removes the need to strip markdown fences from the response. It also ensures all required fields are present and types are correct, which is critical for downstream tool consumption.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:21:08.204093+00:00— report_created — created