Repair Malformed JSON with jsonrepair

Learn how to use the jsonrepair library to automatically fix truncated or malformed JSON output from language models.

2 min readerror-handlingjsonstructured-outputsView source

When generating structured outputs, language models sometimes produce malformed JSON output. This can happen due to: - Truncated responses (hitting token limits) - Syntax errors like single quotes instead of double quotes - Missing closing brackets or braces - Trailing commas The AI TOOLKIT provides an experimental_repairText option that lets you intercept and repair malformed JSON before parsing. Combined with the `jsonrepair` library, you can automatically fix many common JSON issues.

Run it locally

$ npm install ai

Installation

First, install the jsonrepair library:

pnpm add jsonrepair

bash
  1. 1pnpm add jsonrepair

Using with generateObject

Here's how to use jsonrepair with generateObject:

import { generateObject } from 'ai-toolkit';

__PROVIDER_IMPORT__;

import { jsonrepair } from 'jsonrepair';

import { z } from 'zod';

const result = await generateObject({

model: __MODEL__,

schema: z.object({

recipe: z.object({

name: z.string(),

ingredients: z.array(

z.object({

name: z.string(),

amount: z.string(),

}),

),

steps: z.array(z.string()),

}),

}),

prompt: 'Generate a lasagna recipe.',

experimental_repairText: async ({ text }) => {

try {

return jsonrepair(text);

} catch {

return null; // Return null if jsonrepair cannot fix the text

}

},

});

console.log(result.object.recipe);

ts
  1. 1import { generateObject } from 'ai-toolkit';
  2. 2__PROVIDER_IMPORT__;
  3. 3import { jsonrepair } from 'jsonrepair';
  4. 4import { z } from 'zod';
  5. 5const result = await generateObject({
  6. 6 model: __MODEL__,
  7. 7 schema: z.object({
  8. 8 recipe: z.object({
  9. 9 name: z.string(),
  10. 10 ingredients: z.array(
  11. 11 z.object({
  12. 12 name: z.string(),
  13. 13 amount: z.string(),
  14. 14 }),
  15. 15 ),
  16. 16 steps: z.array(z.string()),
  17. 17 }),
  18. 18 }),
  19. 19 prompt: 'Generate a lasagna recipe.',
  20. 20 experimental_repairText: async ({ text }) => {
  21. 21 try {
  22. 22 return jsonrepair(text);
  23. 23 } catch {
  24. 24 return null; // Return null if jsonrepair cannot fix the text
  25. 25 }
  26. 26 },
  27. 27});
  28. 28console.log(result.object.recipe);

How it Works

The experimental_repairText function is called in two scenarios:

1. `JSONParseError` - The model output cannot be parsed as JSON (syntax errors, truncation)

2. `TypeValidationError` - The JSON is valid but doesn't match the schema

When called, it receives:

- text: The raw text output from the model

- error: Either a JSONParseError or TypeValidationError

The function should return:

- A repaired string that will be re-parsed and validated

- null if the text cannot be repaired (the original error will be thrown)

What jsonrepair Can Fix

The jsonrepair library can automatically fix many common issues:

- Missing closing brackets: {"name": "test"{"name": "test"}

- Single quotes: {'name': 'test'}{"name": "test"}

- Missing quotes around keys: {name: "test"}{"name": "test"}

- Trailing commas: {"items": [1, 2, 3,]}{"items": [1, 2, 3]}

- Comments in JSON

- Unescaped special characters

Considerations

1. Repair is Best-Effort - While jsonrepair handles many cases, it cannot fix all malformed JSON (e.g., semantically incorrect data that happens to be valid JSON)

2. Schema Validation - Even after repair, the object must still match your schema. If the model produces structurally wrong data, repair won't help

3. Truncated Content - For severely truncated responses, consider increasing maxOutputTokens or simplifying your schema