The JSON that wasn't
Your recipe app asks the AI for a shopping list as JSON. It worked all week. Today one in five requests crashes.
This answers: Why does my AI return invalid JSON? No coding needed. 3 quick decisions.
- Debugging without panic
- Supervising your AI
Step 1 of 3
This is what the model sent back on a failing request. Why did parsing fail?
model output:```json{ "items": ["eggs", "flour", "milk"] }```SyntaxError: Unexpected token '`', "```jsonWhy does my AI return invalid JSON?
Asking for JSON in a prompt makes it likely, not guaranteed. Models sometimes wrap it in markdown backticks, add a sentence, or change field names, and JSON.parse fails. Use your provider's structured output or tool-calling mode with a JSON schema so replies must match your shape, then validate with a schema library and handle refusals.
Breaks at Your server: Reply parsed without a schema or check
Why AI tools write this bug
AI coding tools often generate a prompt saying 'respond in JSON' and a bare JSON.parse, which works in testing and fails on a small share of real requests.
How to fix it
- Switch to structured output or tool calling with a schema.
- Validate the parsed result with zod or similar.
- Handle refusals and empty results without crashing.
- Log the raw reply when validation fails.
Check your own app
Paste this into Lovable, Cursor or Claude Code:
Without changing anything, find every place in this project that sends a request to an AI model and parses the reply as JSON. For each, tell me whether it uses the provider's structured output or tool mode, whether the parsed result is validated against a schema, and what happens to the user if parsing fails.
Common questions
- Can I just strip the backticks?
- As a stopgap, yes. But it won't catch missing fields or wrong types; structured output plus validation does.