Mental model
Standard JSON.parse() is all-or-nothing: if you omit even a single closing brace, it throws a syntax error. When a model outputs JSON tokens incrementally (e.g. {"status": "pending", "data": {"percentage": 42), standard parsers fail. Partial JSON parsing acts as a repair worker: it temporarily appends closing brackets, quotes, and braces to the end of the text to synthesize a valid JSON string at any point in the stream.
Theory
When streaming structured tool arguments, the model outputs JSON incrementally:
To render the name "John" to the UI before the generator finishes, we must parse the partial string. A custom parser tracks the state of open delimiters:
- Unclosed Strings: If a string literal is open (uneven quotes
"), append a closing quote"to the end. - Open Objects: Track open braces
{and append matching closing braces}in reverse order. - Open Arrays: Track open brackets
[and append matching closing brackets].
Here is a simplified state-based repair logic block:
Alternatives and trade-offs
- Manual Regex extraction: Using regex templates (e.g.
"name":\s*"([^"]*)") to pull properties. Very fast, but breaks down if keys are nested or have identical names. - Auto-Repair Parser: Restructuring incomplete strings on the fly. Works with nested arrays and objects, but requires additional CPU time to scan and repair strings on every chunk.
- Server-Side Chunk Buffering: Buffering full JSON structures on the server and only streaming complete property keys. Minimizes client complexity, but delays the initial Time-to-First-Token rendering of nested structures.
Failure modes and misconceptions
- Swallowing Numeric values: If a number value is truncated (e.g.,
"age": 3), parsing the repaired string immediately gives3. If the next chunk yields0(making it30), the client UI will show a rapid jump from 3 to 30. Account for active cursors in numeric fields. - Malformed Escape Characters: If a chunk terminates right after a backslash (
\), appending a quote immediately creates an escaped quote (\"), causing the repair step to fail. Strip trailing backslashes before running repairs.
Knowledge check
What characters must be tracked to safely repair an incomplete JSON stream?
Decision scenario
When developing a wizard that displays a structured list of recommendations as they stream from a model, do not wait for the generation to finish. Use a partial JSON parser (like Vercel AI SDK's built-in chunk-aware parser) to read the array incrementally and render item cards on the screen as soon as their nested fields validate.
Learning outcomes
- Explain Partial JSON Parsing for Streams as a system mechanism rather than a slogan.
- Compare its alternatives, trade-offs, and production failure modes.
- Apply the concept to a decision and identify evidence that would validate it.
Trade-offs
Using Partial JSON Parsing for Streams can improve capability or control, but it also introduces cost, latency, complexity, and failure modes that must be measured against an explicit objective.