JSON.parse expects a complete JSON document, so it will throw when an LLM stream has delivered only part of a string, value, object, or array. For a live interface, use a parser that can return best-effort snapshots from incomplete text, display those snapshots as provisional, and strictly parse and validate the complete raw response when streaming ends.
Why JSON.parse fails during streaming
A stream delivers fragments, and fragment boundaries do not necessarily line up with JSON syntax. One chunk may end halfway through a string or after an opening brace; the next chunk supplies the rest. A network or API chunk is not guaranteed to be a complete JSON token, let alone a valid standalone document. Chrome for Developers’ overview of LLM streaming describes responses arriving incrementally.
As an Amazon Associate I earn from qualifying purchases.
Appending received content to a buffer and calling JSON.parse on every update does not change that requirement: until the accumulated text is complete and valid, strict parsing can fail. Keep the raw text as it arrives, and use a partial parser only for the UI state that needs to update before completion.
Use SoFar for best-effort JavaScript snapshots
In his DEV Community article, Manthan Kansagra presents SoFar, a small JavaScript library for parsing incomplete JSON. Its parsePartialJSON function accepts a partial buffer; createJSONStream provides a stateful feed interface for stream updates. The article describes a scanner that tracks open containers, string and escape state, and safe cut points. It tries to parse a best-effort completion, then can try earlier safe cut points if that attempt fails. Kansagra’s article reports the package at 425 bytes gzipped and says its benchmark was about 1.3× the cost of bare JSON.parse on a 1.6 MB buffer. Those are the author’s claims, not independently measured results.
#1 Best Overall
A partial result is a snapshot of what can be recovered safely now, not proof that the model has finished producing a valid value. For example, an incomplete prefix such as tru does not establish that the intended literal is true. SoFar’s described approach avoids guessing incomplete numbers or literals and does not promise to repair output that would still be invalid when complete.
Keep the streaming path separate from the final path
- Collect the text fragments from the API, concatenating them in the order required by that API.
- Pass the accumulated text to the partial parser when the interface needs an update, and render returned values as provisional.
- When the stream ends, strictly parse the raw final text with
JSON.parse. - Validate the resulting complete value against the application’s expected schema before using it for business logic or persistence.
If strict parsing or schema validation fails, show an error or use an explicit recovery path. Do not silently promote an incomplete snapshot into an authoritative result.
Choose a parser by its recovery behavior
“Partial JSON parser” does not describe one consistent set of guarantees. Implementations differ in what they recover, whether they reparse accumulated text or maintain stream state, and how they handle malformed syntax. Compare those choices against the format your model is expected to emit.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →| Option | Language and approach | Recovery and validation | Resource safeguards |
|---|---|---|---|
| SoFar | JavaScript; the article describes a partial-buffer function and a stateful feed interface. |
Conservative about incomplete numbers and literals; attempts best-effort completion and earlier safe cut points. Final raw text should still be strictly parsed. | The author reports 425 bytes gzipped and about 1.3× bare parse cost on a 1.6 MB buffer; these figures are not independently verified here. |
| Symfony AI PartialJsonParser | PHP; documents best-effort parsing and integration with a partial JSON stream. | Documents recovery for trailing commas, unclosed strings, dangling colons, partial literals, and open containers. It says unchanged snapshots may be skipped and validation runs on the final object, not partial snapshots. | Not stated in the Symfony documentation. |
| OpenAI Node SDK ChatCompletionStream | Node.js SDK implementation; source tracks structured JSON fragments. | Useful as an example of structured stream consumption; it is not presented here as a general-purpose drop-in partial parser. | Source includes limits for bytes, fragments, nesting depth, and parse work. |
See the Symfony AI platform documentation for its recovery behavior and final-validation guidance, and the OpenAI Node SDK stream implementation for an example of bounded processing. The sources establish no independent adoption, reliability, or industry-wide performance statistic for partial JSON parsers.
Rank #3
Bound parsing work in production
Repeatedly parsing a growing buffer can consume significant work for large or long-lived streams. The OpenAI Node SDK source demonstrates practical safeguards by limiting bytes, fragments, nesting depth, and parsing work. Choose limits appropriate to your application, and decide what the user sees if a stream exceeds them. A partial parser is still processing untrusted input; its ability to tolerate incomplete text is not a reason to leave resource use unbounded.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Partial parsing is not general JSON repair
There is an important distinction between incomplete output that may become valid as more text arrives and malformed JSON-like text that remains invalid when complete. SoFar’s author says it will not repair arbitrary malformed output and points to jsonrepair for cases such as comments or unquoted keys. Pick a repair tool only when repairing that kind of completed input is actually part of the requirement; do not treat it as a substitute for final strict parsing and validation.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.




