Repair Malformed JSON with jsonrepair
Learn how to use the jsonrepair library to automatically fix truncated or malformed JSON output from language models.
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 aiInstallation
First, install the jsonrepair library:
pnpm add jsonrepair
- 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);
- 1import { generateObject } from 'ai-toolkit';
- 2__PROVIDER_IMPORT__;
- 3import { jsonrepair } from 'jsonrepair';
- 4import { z } from 'zod';
- 5const result = await generateObject({
- 6 model: __MODEL__,
- 7 schema: z.object({
- 8 recipe: z.object({
- 9 name: z.string(),
- 10 ingredients: z.array(
- 11 z.object({
- 12 name: z.string(),
- 13 amount: z.string(),
- 14 }),
- 15 ),
- 16 steps: z.array(z.string()),
- 17 }),
- 18 }),
- 19 prompt: 'Generate a lasagna recipe.',
- 20 experimental_repairText: async ({ text }) => {
- 21 try {
- 22 return jsonrepair(text);
- 23 } catch {
- 24 return null; // Return null if jsonrepair cannot fix the text
- 25 }
- 26 },
- 27});
- 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