Engineering Guides4 min readUpdated: 2026-11-24

How to Repair Malformed & Truncated LLM JSON Outputs Locally

Automatically strip markdown code fences, fix single quotes, trailing commas, Python True/False literals, and truncated brackets from LLM JSON responses.

Quick Answer

Even capable LLMs occasionally wrap JSON in ```json markdown fences, emit JavaScript comments, use Python True/None literals, leave trailing commas, or hit max-token truncation before closing brackets. Repair LLM JSON recovers valid RFC 8259 JSON in milliseconds inside browser RAM.

The 5 most common ways LLM JSON breaks in production

Standard JSON.parse() fails immediately on minor syntax deviations. Across production AI logs, five failure patterns account for nearly every parse exception:

  • Markdown wrappers: leading Here is the JSON: prose and ```json ... ``` fences
  • Unquoted keys or single-quoted strings ('status': 'ok')
  • Inline // or /* */ comments added by reasoning models
  • Trailing commas after the final array item or object property
  • Max-token cutoff leaving unclosed [ or { brackets at EOF

Repair broken LLM JSON now →

Recovery + AJV schema verification pipeline

After repairing syntax with jsonrepair AST recovery, immediately verify structural compliance by handing the repaired JSON straight into JSON Schema Validator or converting it to TOON/Zod via DataTo's local IndexedDB pipeline.

```json
{
  // Cut off mid-stream
  'status': 'ok',
  'items': [{id: 1, active: True,}
```

Validate repaired JSON against schema →

Tools used in this guide

LLM & TOON →

Related formats

Related Comparisons

Frequently asked questions

Does Repair LLM JSON work on truncated streaming responses?

Yes. Missing closing quotes, arrays, and braces at the end of a cut-off response are automatically balanced into parseable JSON.

Will it alter valid strings inside JSON values?

When fences are stripped first and valid delimiters remain intact, jsonrepair modifies only invalid structural tokens.

How can I prevent LLMs from producing broken JSON in the first place?

Use strict Structured Outputs schemas generated with DataTo JSON to OpenAI Function so constrained decoding guarantees valid syntax.

Authoritative References

More engineering guides