Guides · Updated 13 October 2026

How to Fix Invalid JSON from LLMs

Why AI models return broken JSON (code fences, trailing commas, cut-off replies), how to repair it, and how structured outputs stop it happening.

You ask a model for JSON and get something that almost parses. JSON.parse throws, your pipeline stops, and the error message points at character 1,742. Here's why it happens, how to repair a reply you already have, and how to stop it happening.

Why models return broken JSON

A language model writes text one token at a time. Unless the API forces a structure, nothing stops it from writing something that only looks like JSON. The usual problems:

Repair a reply you already have

Paste the whole reply into AI JSON Repair. It finds the JSON inside the surrounding text, fixes the problems above, and lists every change it made, so you can check nothing was guessed wrongly. A cut-off reply is closed so it parses, but the missing data can't be recovered. If a reply was cut off, raise the maximum output length and ask again.

Once it parses, the JSON Formatter makes it readable and points to the exact line of any remaining error.

Stop it happening: use structured outputs

The reliable fix is to have the API enforce the shape, instead of asking nicely in the prompt:

Each provider supports a slightly different part of JSON Schema. For example, OpenAI's strict mode requires every property to be listed as required and additionalProperties to be false. The AI JSON Schema Builder creates a schema from a sample of the JSON you want, and checks it against each provider's rules.

If you can't use structured outputs

Checklist