Principle:AI QL Tuui Tool Call Incremental Merging
| Knowledge Sources | |
|---|---|
| Domains | LLM, Streaming, Tool_Use |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
A streaming reconstruction pattern that merges incremental tool call fragments from SSE deltas into complete tool call objects with accumulated function names and arguments.
Description
When an LLM streams a response with tool calls, the tool call data arrives in fragments across multiple SSE deltas. The same tool call may appear in successive chunks with partial function names and argument strings that need to be concatenated. The merging pattern identifies whether a new fragment belongs to the last tool call (by matching or absent ID) and either concatenates the function properties or appends a new tool call entry. Null values are skipped to avoid overwriting existing data.
Usage
Use this principle when parsing streamed LLM responses that include tool calls, where the tool call function name and arguments arrive in multiple incremental chunks that must be reassembled.
Theoretical Basis
The merge algorithm:
for each tool in incoming tool_calls:
lastTool = target.tool_calls[last]
if lastTool exists AND (tool.id is null OR tool.id == lastTool.id):
# Merge: same tool call, concatenate properties
for each key in tool.function:
if value is null: skip (don't overwrite)
if lastTool.function[key] is non-empty: concatenate
else: overwrite
else:
# New tool call
if tool.function.arguments is null: set to '{}'
append tool to target.tool_calls
The key insight is that streaming APIs send the same tool call ID across fragments (or omit it), signaling continuation. The arguments field accumulates as a JSON string built incrementally: '{"pa' + 'th": "' + '/tmp"}' = '{"path": "/tmp"}'.