Principle:AGenUI AGenUI Incremental Stream Parsing
| Knowledge Sources | |
|---|---|
| Domains | Parsing, Streaming, UI_Framework |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
An incremental parsing technique that processes streaming JSON protocol data chunk-by-chunk through a state machine, extracting complete events and individual components as soon as they become available, without waiting for the entire payload.
Description
Traditional JSON parsing requires the complete document in memory before parsing can begin. In LLM streaming scenarios (SSE, chunked HTTP), data arrives in fragments — a single JSON event may be split across multiple chunks, or multiple events may arrive in one chunk. Incremental parsing solves this by maintaining a persistent buffer and a state machine that can resume parsing across chunk boundaries.
The state machine transitions between states: `Idle` (waiting for event), `StreamingComponents` (inside an `updateComponents` event, extracting individual component objects), and `StreamingDataModel` (inside an `updateDataModel` event, emitting field-level updates). At each step, the parser checks whether enough data is available to extract the next complete unit; if not, it waits for more data.
For `updateComponents`, the parser doesn't wait for the full components array — it extracts individual component JSON objects as soon as their braces close, enabling real-time rendering of each component as it streams in. A stream plugin architecture (text, markdown, composite) allows content-specific streaming strategies (e.g., markdown field-level incremental delivery).
Usage
Use this principle when:
- Parsing JSON protocol data that arrives in fragments from an LLM stream
- Components must render incrementally as they complete, not after the full response
- The parser must handle partial JSON gracefully across chunk boundaries
- Data model fields need field-level incremental updates (e.g., markdown append)
Theoretical Basis
The state machine transitions:
Idle ──(createSurface/updateDataModel/deleteSurface complete)──→ Idle
Idle ──(updateComponents starts)──→ StreamingComponents
StreamingComponents ──(components array closes)──→ Idle
Idle ──(updateDataModel starts)──→ StreamingDataModel
StreamingDataModel ──(value closes)──→ Idle
The incremental extraction algorithm:
# Abstract algorithm (NOT real implementation)
def drive_parser(buffer, state):
results = []
while has_complete_unit(buffer, state):
if state == Idle:
event, end = extract_first_complete_json(buffer)
event_type = detect_event_type(event)
if event_type == "updateComponents":
state = StreamingComponents
# locate components array, don't wait for full array
else:
results.append(ParseResult(event)) # complete event
buffer = buffer[end:]
elif state == StreamingComponents:
component_json, end = extract_next_component(buffer)
if component_json:
results.append(ParseResult(component_json)) # one component
buffer = buffer[end:]
if array_closed(buffer):
state = Idle
return results, buffer # resume on next chunk
Practical Guide
- Call `beginTextStream()` before sending the first chunk.
- Feed each text chunk via `receiveTextChunk(data)`; the parser buffers and processes incrementally.
- Call `endTextStream()` after the stream closes to flush residual data and reset state.
- The buffer has a 10 MB safety limit; overflow data is dropped with a log warning.
Related Pages
Implemented By
- Implementation:AGenUI_AGenUI_ISurfaceManager_Streaming
- Implementation:AGenUI_AGenUI_ProtocolStreamExtractor