Assemble Streamed Tool Calls

medium
Unit 19 · Streaming0/3 to milestone

When an LLM API streams a response that calls tools, each chunk carries a fragment of one or more tool calls:

{"index": 0, "id": "call_a", "function": {"name": "get_wea", "arguments": ""}}
{"index": 0, "function": {"name": "ther", "arguments": "{\"city\": \"Pa"}}
{"index": 1, "id": "call_b", "function": {"name": "time", "arguments": "{}"}}
{"index": 0, "function": {"arguments": "ris\"}"}}

Implement merge_tool_call_deltas(deltas):

  • Group fragments by index. The id arrives once (keep the first one seen). Name and argument fragments are concatenated in arrival order.
  • Fragments for different calls can interleave.
  • Return a list sorted by index of {"id": ..., "name": ..., "arguments": parsed_dict}, parsing the joined arguments with json.loads (an empty string means {}). Raise ValueError naming the call id if the arguments aren't valid JSON or don't parse to a dict.
#streaming#tool-use#apis2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Assemble Streamed Tool Calls · TechGradify