vibe.llm_providers.streaming_json_parser

Incremental, path-aware JSON parser that streams string deltas and emits completions.

Handles escapes (including \uXXXX) across chunks. Keeps per-tool_id state and avoids rescanning.

StreamingJsonParser

Incremental JSON parser tailored for tool-call argument streams.

Design goals: - Path-aware names: Emits names like "blocks[0].content" and "options[2]" so downstream components can route deltas precisely without guessing context. - Low latency: Yields string deltas as soon as characters arrive instead of buffering until the full JSON is valid. - Robust escapes: Properly handles common escapes (\n, \t, \", \) and unicode (\uXXXX) even when an escape sequence is split across chunks. - Minimal state: A tiny stack machine tracks whether we're inside an object or array and what property/index we're parsing. No rescanning is needed.

Emitted events (via LLMProvider): - TOOL_ARGUMENT_CHUNK: For in-progress string values. The delta is the new piece of decoded text since the last emission. - TOOL_ARGUMENTS_COMPLETE: For any finished value (string/number/bool/null) with a dict mapping the path-aware name to the final value.

Note: This parser is intentionally specialized. It does not attempt to fully validate or build a JSON tree; it only produces timely deltas and per-token completions sufficient for the StreamTranslator UI.

parse_argument_stream

parse_argument_stream(tool_id: str, args_chunk: str, accumulated_json: str = '') -> Generator[StreamChunk, None, None]

Parse incremental JSON argument stream, yielding field/value chunks.

parse_json_action_stream

parse_json_action_stream(chunk: str) -> Generator[StreamChunk, None, None]

Parse JSON action chunks and emit tool call events.

JSON actions are JSON objects with an 'action' property that names the tool:

This method: 1. Strips code fences (json ...) transparently 2. Emits TOOL_CALL_START when the 'action' property value completes 3. Streams other string arguments via TOOL_ARGUMENT_CHUNK 4. Emits TOOL_ARGUMENTS_COMPLETE for each finished argument 5. Emits TOOL_CALL_END when the JSON object closes 6. Handles action appearing anywhere in the object (buffers until action known)

Parameters:
  • chunk (str) –

    Raw text chunk from the LLM (may include fences, partial JSON)

Yields: