fix: correctly merge streaming tool_call deltas with duplicate indexes#3223
fix: correctly merge streaming tool_call deltas with duplicate indexes#3223EvanYao826 wants to merge 2 commits intoopenai:mainfrom
Conversation
Fixes openai#3201 When the first streaming chunk contains multiple tool_calls at the same index (e.g., parallel function calls), the current merge logic stores them as separate list entries instead of merging them by index. This causes incorrect tool_call accumulation during streaming. Updated the delta merge logic to use the tool_call index as the merge key, matching the OpenAI streaming behavior where the index field identifies which tool call a delta belongs to.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 57991d6620
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Address Codex review feedback on PR openai#3223: when acc_value is an empty list (initialized for indexed merge), on empty returns True, causing the code to take the scalar extend path instead of the index-based merge path. Adding as a guard ensures empty lists fall through to the dict-merge logic. Fix applied to both _deltas.py and _assistants.py.
EvanYao826
left a comment
There was a problem hiding this comment.
Thanks for the review! Fixed both issues. The problem was that all(isinstance(x, (str, int, float)) for x in acc_value) returns True on an empty list (Python semantics), so the indexed merge path was bypassed when acc_value was initialized as [].
Fix: added acc_value and as a guard in both _deltas.py and _assistants.py, ensuring empty lists fall through to the dict/index-based merge logic.
Changes pushed to the PR branch.
Fixes #3201
Summary
When the first streaming chunk contains multiple tool_calls at the same index (e.g., parallel function calls), the current merge logic stores them as separate list entries instead of merging them by index. This causes incorrect tool_call accumulation during streaming.
Fix
Updated the delta merge logic in
_deltas.pyand_assistants.pyto use the tool_call index as the merge key, matching the OpenAI streaming behavior where the index field identifies which tool call a delta belongs to.Note
PR #3215 exists as a draft but appears incomplete. This is a clean, complete fix with tests.