Fix streaming tool_call delta accumulation with duplicate indexes
Summary
Fix streaming tool_call delta accumulation when the first chunk contains multiple tool_calls entries with the same index.
Root Cause
accumulate_delta in _deltas.py assumed that a list entry's index field matches its physical position in the Python list. When the first streaming chunk contains multiple tool_calls entries with the same index, they were stored as separate physical list entries. Subsequent chunks would only merge into the first physical entry at that position, stranding the remaining entries with partial data and producing corrupted tool call arguments.
Solution
Normalize delta list entries by their index field before storing or merging on all three code paths in accumulate_delta:
- Initial store (key not in
acc) — normalize before storing the first value - None value replacement — normalize before storing when the previous value was
None - List merge path (existing entries) — normalize the delta list before iterating and merging into the accumulator
The same fix was applied to the duplicate accumulate_delta in _assistants.py.
Test Plan
Added tests/test_deltas.py with 5 regression tests:
- Duplicate indexes in the first chunk (the reported case)
- Multiple tool calls with different indexes
- Three duplicate indexes in the first chunk
- Sparse indexes
- Normal case (no duplicates) — no regression
All 25 tests pass (20 existing + 5 new).
Fixes #3201
I've pushed a fix that routes the initial snapshot through accumulate_delta({}, delta) so duplicate-indexed tool_calls are merged/normalized from the very first chunk, just like all subsequent chunks.
Thanks for taking the time to work on this! We’ve merged the fix for duplicate-index tool-call deltas in #3425, so I’m closing this as a duplicate. We appreciate the contribution.