← Back to Blog

Fix: LLM returns invalid JSON — parse errors, trailing prose, truncated objects

2026-09-17·3 min read·ClawRouters Team
llm returns invalid jsonjson mode openaimodel output not valid jsonjson parse error llmstructured output llmllm json truncated

TL;DR — Four different failures get reported as "the model returned invalid JSON", and they need different fixes: prose or markdown fences around the object (use a structured-output mode), a truncated object (your token budget, not the model), syntactically valid JSON with the wrong shape (schema enforcement, not JSON mode), and valid JSON containing a hallucinated value (no output mode fixes this — validate the content). Find out which one you have before reaching for a regex.

The four failures

1. Prose or fences around the object

Here's the JSON you requested:
```json
{"name": "Ada"}

Let me know if you need anything else!


`json.loads` fails on the first character. This is the one people meet first and the one that is genuinely solved by asking for a structured output mode, which constrains the model to emit only the object.

**2. Truncated object**

```json
{"items": [{"id": 1, "title": "Something long

Valid JSON up to where it stops. This is a token-budget failure, not a formatting failure — the generation hit the cap mid-object. The tell is a length finish reason. Raising the completion budget fixes it; retrying without raising it produces the same truncation at the same place.

3. Valid JSON, wrong shape

{"user_name": "Ada"}     // you expected "name"
{"age": "42"}            // you expected a number

Parses cleanly, then breaks your code downstream. A plain JSON mode does not help here — it guarantees syntax, not structure. What you want is schema-constrained output, where the provider enforces your schema during generation.

4. Valid, correctly-shaped, and wrong

{"founded": 1847, "source": "company website"}

Perfect JSON. The number is invented. No output mode can fix this — structured output constrains form, never truth. If the value matters, it has to be checked against something real, or carry a field saying where it came from.

What to do

Failure Fix
Prose / fences Structured-output or JSON mode
Truncated Raise the completion budget; log finish_reason
Wrong shape Schema-constrained output, plus client-side validation
Wrong content Validation against a source of truth — not an output mode

And regardless of mode, always validate after parsing. Provider-side schema enforcement is strong but it is not a reason to skip a validator you control; version drift and fallback models will eventually hand you something unexpected.

Two implementation notes

Prevention

Ready to Reduce Your AI API Costs?

ClawRouters routes every API call to the optimal model — automatically. Start saving today.

Get Started Free →

Get weekly AI cost optimization tips

Join 2,000+ developers saving on LLM costs