October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Parse Partial JSON from an LLM Stream (and Why JSON.parse Fails)

LLM stream chunks are not complete JSON documents. Use partial parsing for provisional UI updates, then strictly parse and validate the final response.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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

  1. Collect the text fragments from the API, concatenating them in the order required by that API.
  2. Pass the accumulated text to the partial parser when the interface needs an update, and render returned values as provisional.
  3. When the stream ends, strictly parse the raw final text with JSON.parse.
  4. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.