Most JSON errors are syntax problems: a stray comma, a single quote, a comment the model slipped in. Truncation is different. The JSON was fine right up until the moment it stopped, because the model did not make a mistake. It ran out of room.
The cut-off almost always looks the same
The fragment ends inside a string, or inside an array that never got its closing bracket. The parser error names the end of the input because there is nothing after it, not because a character is wrong. If you scroll to the very end of the text and find a half-written value or a dangling quote, that is truncation. The usual suspect is the token limit: your max_tokens budget expired while the model was still writing, and the API handed you everything it had.
There is a cheap way to confirm. Every major API tells you why the response ended. Check the finish_reason on the choice before you try to parse anything. If it says stop, the model finished on its own and your problem is elsewhere. If it says length, or the status is incomplete with a max_output_tokens reason, the output was cut off mid-write. That single check separates a data problem from a budget problem, and the two need different fixes.
Why closing the brackets is the wrong instinct
This is the part most repair guides rush past. A JSON repairer can do something genuinely useful with a truncated blob: walk the open structures, close the unclosed string, shut every open array and object, and hand you something that parses. It feels like the problem is solved.
It is not. Here is the trap, and it matters more than the syntax trick: valid is not the same as complete. Take this fragment:
{
"items": [
{"id": 1, "name": "Lamp"},
{"id": 2, "name": "Cab
A repair closes the string and the brackets and reports success. The second item now has the name "Cab", and whatever the model was going to write after it is gone forever. If your code then processes that "valid" JSON, you have silently accepted corrupted data. One recent write-up of this exact failure found a repair tool returning success: true on truncated output in about one in six test cases, with only a free-text warning as the signal. If your pipeline cannot act on that signal, the repair made things worse, not better.
What actually fixes truncated JSON from AI output
First, stop trying to parse it. Treat a length finish reason as a retry signal, not a repair job. Raise max_tokens to roughly double the tokens you expect the answer to use: if typical responses to your prompt land around 400 tokens, set the budget to at least 800. Truncation almost always means the budget was too tight, not that the model misbehaved.
Second, if the response is legitimately long, split the request. Ask for the JSON in smaller pieces (paginated, or one section per call) instead of one giant object. Smaller responses finish inside the budget more reliably than one large response with a large budget.
Third, if you still repair, do it loudly. A repair of truncated input should set an explicit flag, something like truncated: true, that your code can branch on. A warning buried in a message string that nobody parses is the same as no warning. Use the repaired fragment for logging or display if you must, but never feed it into downstream logic as though it were the whole answer.
One more honest option for the edge case where you genuinely cannot re-request: ask the model to continue from where it stopped, then splice the pieces together before parsing. It costs another call, but it is the only way to get the missing data back, because the missing data never existed anywhere except in the model's unfinished output.
Paste your broken JSON into the repair tool and it will show you exactly what it changed, including a warning when the input was never closed.
Frequently asked questions
Why does an AI model stop in the middle of JSON output?
The model hit its token limit (max_tokens) before finishing. The API returns a finish reason of length instead of stop. The content is otherwise normal; it simply ends wherever the budget ran out.
Does JSON mode stop models from truncating output?
No. JSON mode and structured output guarantee syntactic validity of what is returned, not completeness. A response cut off by the token limit is still invalid JSON, so you must check the finish reason before parsing.
Can a repair tool recover the data missing from truncated JSON?
No. A repair tool can close unclosed strings, arrays, and objects so the fragment parses, but the content after the cutoff is gone for good. The repaired file will open in your editor without complaint and still be missing data. Ask the model to continue or re-request in smaller pieces.
How do I detect truncated JSON in code?
Check the API response's finish_reason before calling JSON.parse. If it is length, or the status is incomplete with reason max_output_tokens, treat the output as truncated and retry with a higher max_tokens value instead of trying to parse it.
How high should I set max_tokens for JSON output?
Give it roughly double the tokens you expect the answer to need. If a typical response to your prompt is around 400 tokens, set max_tokens to at least 800. Truncation usually means the budget was too tight, not that the model misbehaved.
Get one practical dev-tools guide a week: subscribe to the newsletter for new JSON guides and parser explainers.
Related reading: Unexpected End of JSON Input: Empty Body, Cut-Off Payload, or Unclosed Bracket · How to Fix the JSON Single Quotes Error · NDJSON Broke My Parser: How to Fix JSON Lines Errors · Why Is My JSON Invalid? The 7 Syntax Errors That Break JSON.parse