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):
index. The id arrives once (keep the first one seen). Name and argument fragments are concatenated in arrival order.{"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.