Here's a failure mode I've seen in nearly every AI agent codebase I've reviewed: the agent receives a model response, trusts the JSON it contains, and calls .result.items[0].id — which throws Cannot read properties of undefined at 2 AM because the model returned {"result": null} on an edge case.
The model didn't hallucinate the content. It hallucinated the structure.
This is surprisingly common, and the fix isn't "use a better prompt." The fix is a validation layer that runs between the raw model output and the code that acts on it.
Why structured output isn't enough
Claude and GPT-4 both support structured output modes that constrain the model to emit valid JSON matching a given schema. This is genuinely useful and you should use it. But it doesn't fully solve the problem, for two reasons:






