🤖 AI Summary
A developer released json_completer, a Ruby gem that does stateful, incremental JSON completion so streaming LLM tool calls can be parsed and shown live rather than waiting for a full JSON blob. The problem: LLMs stream character-by-character and emit JSON tool arguments that are repeatedly parsed by one-off repair libraries, which reparses the entire accumulated string on every chunk (O(n²)). Json_completer keeps parsing state (last index, context stack for objects/arrays, whether you're inside a string, partial tokens and escape/Unicode states) and resumes from where it left off, emitting a syntactically valid JSON snapshot on every chunk so the UI can parse and render intermediate tool arguments in real time.
Technically, the parser must know context-sensitive completion rules (a string in an object may need a null value, while in an array it may be a complete value), handle incomplete strings including trailing backslashes and Unicode escapes, partial numbers, and deep nesting. In benchmarks with a 12 KB payload streamed in ~5-character chunks, the incremental O(n) approach finished in ~43 ms total vs ~16.7 s for reparsing (388× faster), keeping per-chunk latency sub-millisecond. For AI/ML apps that surface tool calls or structured outputs live, this shifts UX from “spinner” to responsive, real-time feedback and is available as an open-source gem.
Loading comments...
login to comment
loading comments...
no comments yet