Developer releases 425-byte library to parse partial JSON from streaming LLMs

A developer building an LLM-powered recipe app found that JSON.parse fails on every incomplete token during streaming, since valid JSON only exists once the final closing bracket arrives. To solve this without discarding streaming benefits or relying on fragile regex hacks, they built a minimal open-source library called SoFar. The library exposes two functions — parsePartialJSON and createJSONStream — that return the best structurally complete value available from any partial JSON buffer without throwing errors or guessing incomplete values. It works by scanning the buffer once, tracking open containers and string state, and identifying safe cut points where valid JSON can be reconstructed by appending the necessary closing brackets. At 425 bytes gzipped and zero dependencies, it is designed as a thin complement to the native JSON.parse rather than a replacement.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in