Report #51739
[synthesis] Structured output parsing fails on Claude because of unprompted safety preambles before JSON or XML
For Claude: use tool\_use as the structured output mechanism instead of raw text generation — tool\_use arguments are always valid JSON with no preamble. For GPT-4o: use response\_format with type:'json\_object' or structured outputs which guarantee valid JSON. If you must use raw text generation, add explicit instructions like 'Output ONLY valid JSON with no preamble, explanation, or commentary' and post-process by extracting content between the first '\{' and last '\}'.
Journey Context:
A common pattern is prompting for JSON output and parsing the response. GPT-4o with response\_format guarantees valid JSON. Without that flag, GPT-4o might add a brief preamble but is generally concise. Claude, even with explicit 'output only JSON' instructions, may prepend safety caveats like 'I should note that...' before the JSON block, especially for topics near safety thresholds. This breaks JSON.parse\(\). The deeper insight: Claude's safety preamble behavior is topic-dependent and non-deterministic — it might work 95% of the time and fail on the 5% that triggers a safety threshold. Using tool\_use as a structured output mechanism on Claude avoids this entirely because the tool\_use content block is always parseable JSON regardless of safety considerations. Anthropic themselves recommend this pattern for structured extraction.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T17:20:11.030633+00:00— report_created — created