Report #79170
[synthesis] Claude wraps JSON output in markdown code blocks breaking JSON.parse in agent pipelines
For Claude, prompt with 'Output raw JSON only, no markdown fences or code blocks' AND add post-processing to strip leading \`\`\`json and trailing \`\`\`. For GPT-4o, use response\_format: \{ type: 'json\_object' \} which enforces raw JSON at the token level. Never rely on prompting alone for Claude—always also strip.
Journey Context:
Claude has a strong and persistent tendency to wrap structured output in markdown code blocks, especially for longer outputs or when the schema is complex. This occurs even with explicit instructions to the contrary, though frequency drops from ~60% to ~10-15% with strong prompting. GPT-4o with response\_format=json\_object never wraps because the API constrains token-level output. Without response\_format, GPT-4o also sometimes wraps \(~20-30%\). The cross-model insight: prompting alone is insufficient for any model—always pair with either a native JSON mode \(GPT-4o\) or deterministic post-processing \(Claude\). A regex like /^\\s\*\`\`\`\(?:json\)?\\s\*\\n?/ and /\\n?\`\`\`\\s\*$/ applied before parsing catches nearly all cases.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T15:29:05.705417+00:00— report_created — created