fix(openai): keep streamed tool calls apart when they reuse an index #3585
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
| Files with missing lines | Coverage Δ | |
|---|---|---|
| ag2/config/openai/openai\_client.py | 86.50% <100.00%> (+8.90%) | :arrow_up: |
... and 1 file with indirect coverage changes
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
if two whole tool calls both arrive with index 0 (some OpenAI compatible APIs do that), they still collapse into one slot here. the new tests only cover a missing index. worth treating a new call id under an occupied index as a separate call?
Good catch: a call that arrives with a new id under an index that already holds a different id now opens its own entry (indices map to entries instead of being list positions), so two whole calls both sent with index: 0 stay separate. Continuation chunks without an id still append to the call at their index. Added test_whole_tool_calls_that_all_use_index_zero_stay_separate (3875734).
This comment was drafted with Claude Code.
its-amann
pcbeingused333
Why are these changes needed?
_process_streaminag2/config/openai/openai_client.pyusestc.indexas the position of a streamed tool call. When an OpenAI-compatible endpoint sends two whole calls with different ids under the same index (for exampleindex: 0for every parallel call), they land in one entry: the second id and name overwrite the first and the arguments are concatenated, so the agent gets one broken call instead of two.Each index now maps to an entry, and a delta with a new id under an index that already holds a different id opens a new entry. Fragments without an id still append to the call at their index, and deltas without an index keep the behaviour from #3611.
The case was raised in review on this PR. The PR's original scope, calls without an index, landed in #3611, so this is now limited to reused indexes.
Related issue number
None.
Checks
test_whole_tool_calls_that_all_use_index_zero_stay_separatefails on main and passes here; the streams are built by hand with a mock transport, no live endpoint was called)AI assistance
This change was made with Claude Code and Codex.
🤖 Generated with Claude Code