| I've been building LLM-powered apps and kept hitting the same problem: when you stream JSON from OpenAI/Anthropic, it arrives incomplete mid-generation. {"message": "I'm currently generating your resp JSON.parse dies. You either wait for the full response (slow) or try to parse incrementally (hard).
I wrote a single-pass state machine that repairs broken JSON as chunks arrive. Technical approach:
- Zero external dependencies - everything from scratch
- No regex (avoids ReDoS vulnerabilities)
- O(n) single-pass processing
- Stack-based context tracking
- Character classification via bitmask lookup table
- Works in Node.js, Deno, Bun, browsers, Cloudflare Workers What it handles:
- Truncated strings and unclosed brackets
- Python constants (None, True, False)
- Single quotes, trailing commas, unquoted keys
- JSONP wrappers, MongoDB types (NumberLong)
- LLM "thinking" blocks and markdown fences
- String concatenation ("a" + "b") The streaming benchmark shows 1.7x faster than jsonrepair - we avoid re-parsing the entire document on each chunk.
7KB minified. TypeScript-first with full type definitions. Curious what edge cases others have hit - always looking to improve coverage. |