Report #54362
[synthesis] Claude wraps JSON output in markdown code fences despite instructions not to, breaking strict JSON parsers
Always strip markdown fences \(\`\`\`json ... \`\`\`\) from Claude output before parsing as JSON. For GPT-4o, use response\_format: \{ type: 'json\_object' \} which guarantees raw JSON without fences. For Claude, use tool\_use with a JSON-schema-structured tool as the forced output format instead of relying on prompt engineering alone.
Journey Context:
A common assumption is that 'output only JSON, no other text' in the prompt is sufficient. It is not. Claude models \(particularly 3.5 Sonnet\) frequently wrap JSON in markdown fences even with explicit instructions not to, especially when the JSON is large or complex. GPT-4o with response\_format=json\_object reliably produces raw JSON but requires at least the word 'json' in the prompt. The deeper insight: Claude's tendency to add fences correlates with output complexity—short JSON objects are usually clean, but nested structures or arrays over ~500 tokens almost always get fenced. Anthropic's own recommendation is to use forced tool use for structured output rather than prompt-based JSON extraction. The right call is defensive parsing plus structured tool output, not prompt fighting.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T21:44:41.783610+00:00— report_created — created