Skip to content

fix: correctly merge streaming tool_call deltas with duplicate indexes#3223

Open
EvanYao826 wants to merge 2 commits intoopenai:mainfrom
EvanYao826:fix/streaming-tool-call-index-merge-3201
Open

fix: correctly merge streaming tool_call deltas with duplicate indexes#3223
EvanYao826 wants to merge 2 commits intoopenai:mainfrom
EvanYao826:fix/streaming-tool-call-index-merge-3201

Conversation

@EvanYao826
Copy link
Copy Markdown

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.py and _assistants.py 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.

Note

PR #3215 exists as a draft but appears incomplete. This is a clean, complete fix with tests.

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.
@EvanYao826 EvanYao826 requested a review from a team as a code owner May 11, 2026 12:43
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/openai/lib/streaming/_deltas.py
Comment thread src/openai/lib/streaming/_assistants.py
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.
Copy link
Copy Markdown
Author

@EvanYao826 EvanYao826 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streaming tool_call deltas with duplicate indexes in first chunk are accumulated incorrectly

1 participant