Search⌘ K
AI Features

Parsing Tool-Call Streaming Into State

Explore how to parse and integrate streaming tool-call events into explicit AI state using a reducer with strict rules. Learn to track argument buffers per function call, handle event boundaries, and prevent partial or premature execution. This lesson helps you maintain correct multi-turn state and ensure tool calls execute only when JSON arguments are fully received and validated.

A tool call in a stream fails in a specific way when the UI treats partial argument text as complete JSON and fires execution mid-stream. The event log looks orderly, but the reducer updates leak across boundaries and the tool runs with truncated inputs.

This lesson traces the request pipeline from tokenized user input to context assembly, then into a streaming model call that emits events you must fold into state, and finally into post-processing that decides whether a tool can run.

To make the failure concrete, step through a minimal timeline and decide where execution is allowed to start.

Streamed argument deltas assemble incomplete JSON until arguments.done confirms completion.
Streamed argument deltas assemble incomplete JSON until arguments.done confirms completion.

The key rule is strict. Never treat arguments as complete until the stream emits the done boundary for that specific function call. A probabilistic generator can output valid JSON only at the end, and the stream may cut across tokens that split braces, quotes, or Unicode escapes. ...