Report #13946
[tooling] Model outputs malformed JSON or extra commentary when requesting structured data
Define a tool named 'submit\_result' with a strict JSON Schema for the desired structure and instruct the agent to 'always use the submit\_result tool to respond' rather than asking for JSON in plain text.
Journey Context:
Developers frequently attempt to get structured output by prompting 'respond with valid JSON' or using the 'json\_mode' API parameter, but this fails when the model interleaves markdown code blocks, explanatory text, or generates invalid JSON \(trailing commas, unescaped quotes\). The robust solution leverages tool calling: define a single tool 'submit\_result' whose parameters exactly match the desired schema, and add to the system prompt: 'You must use the submit\_result tool to provide your answer; do not respond with conversational text.' This forces the model to produce arguments that strictly validate against the JSON Schema, eliminating markdown fences and commentary. This pattern works across Claude, GPT-4, and other tool-capable models, whereas native structured output modes vary by provider.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T20:15:19.848717+00:00— report_created — created