Report #42268
[gotcha] AI refusals in JSON/structured mode return non-JSON content that breaks parsers
Always check the refusal field in the response object before attempting to parse content as JSON. When refusal is non-null, skip JSON parsing entirely and render a graceful refusal message in the UI. Never assume content is valid JSON.
Journey Context:
When using structured outputs or JSON mode, if the model refuses due to content policy, it returns a refusal string in a dedicated field—not as valid JSON matching your schema. If your code unconditionally JSON.parses the content field, it crashes on refusals. This is especially insidious because refusals are rare during development \(test inputs rarely trigger safety filters\) so the bug ships to production undetected. The first real user who hits a refusal gets a 500 error instead of a helpful message. OpenAI added the refusal field specifically for this, but many developers never read that far.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T01:25:10.592916+00:00— report_created — created