Error reference

Failed to parse JSON from an LLM response

2 min read

The compiler asks a model for JSON and parses what comes back. When both strategies fail, it throws rather than returning something approximate.

The two attempts

First a fenced code block, which is what a well-behaved response looks like. Then a raw match for the outermost brace or bracket pair:

const rawMatch = content.match(/[\[{][\s\S]*[\]}]/);
if (rawMatch) return JSON.parse(rawMatch[0]);
throw new Error('Failed to parse JSON from LLM response');

That second attempt handles the common case of a model wrapping valid JSON in prose — "Here is the structure you asked for:" followed by the object.

Why it stops there

A third strategy is always available: strip trailing commas, close unbalanced brackets, repair quotes. Each of those turns an invalid document into a valid one whose contents nobody verified.

The failure mode is worse than the error. Repaired JSON parses, so it flows downstream and is acted upon, and the thing it describes is whatever the repair happened to produce.

What causes it

Truncation. The response was cut off and the JSON is genuinely incomplete — check the finish reason before blaming the parser.

Commentary inside the structure. Some models annotate fields, which is not valid JSON.

An outright refusal. The model declined and explained why, in prose.

How to fix it

Log the raw response. This error is nearly always diagnosed by reading what came back rather than by changing the parser, and a parser that has been tuned against unlogged failures is a parser fitted to guesses.

Where this sits relative to the planner

The wave planner does not use JSON at all — it asks for markdown tables and parses those, precisely because table parsing degrades more gracefully than JSON does. A malformed row loses a row; malformed JSON loses the document.

The wiki compiler makes the opposite trade, and this error is its cost.