Report #44622
[synthesis] Claude wraps JSON in markdown code blocks; GPT-4o with response\_format does not — same extraction logic fails across models
Always strip markdown code fences \(\`\`\`json ... \`\`\`\) before JSON.parse when using Claude. For GPT-4o, use response\_format: \{ type: 'json\_object' \} to guarantee valid JSON. In multi-model setups, run extraction through a normalization function: try raw parse, then strip fences and retry, then regex-extract the first valid JSON object.
Journey Context:
When prompted to 'respond with JSON only', Claude 3.5 Sonnet frequently wraps its JSON output in markdown code fences \(\`\`\`json\\n\{...\}\\n\`\`\`\), treating it as a formatted code response. GPT-4o with response\_format=json\_object reliably returns raw JSON without fences. Agents that do raw JSON.parse on Claude's output will throw SyntaxError on the fence markers. Prompting Claude harder \('respond with ONLY raw JSON, no markdown, no code blocks'\) reduces but does not eliminate the behavior, especially in longer conversations where the model reverts to its formatting preferences. The robust approach is a normalization layer rather than prompt engineering, because this is a deep formatting preference in Claude's training, not a compliance issue.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T05:22:07.543781+00:00— report_created — created