Skip to content

Fix streaming delta merge for duplicate tool call indexes#3425

Open
fengjikui wants to merge 2 commits into
openai:mainfrom
fengjikui:codex/fix-duplicate-tool-call-deltas
Open

Fix streaming delta merge for duplicate tool call indexes#3425
fengjikui wants to merge 2 commits into
openai:mainfrom
fengjikui:codex/fix-duplicate-tool-call-deltas

Conversation

@fengjikui

@fengjikui fengjikui commented Jun 23, 2026

Copy link
Copy Markdown

Fixes #3201.

Summary

Streaming tool call deltas can include multiple entries with the same logical index in the first chunk. The accumulator used to assign that first list wholesale when the key was absent or None, so duplicate-index entries bypassed the existing recursive merge path and later argument fragments merged into the wrong physical list entry.

This changes the streaming delta accumulators to initialize indexed lists through the existing merge loop instead of taking the first indexed list as-is. It also applies the same initial-list normalization to the chat completion snapshot path and the matching assistant streaming accumulator, which shared the same early-return behavior.

Root cause

accumulate_delta() already knew how to merge list entries by their logical index, but only after an accumulator entry existed. When the first streamed value for tool_calls contained two entries with index: 0, the fast path copied both entries directly into the snapshot. The next chunk then merged into physical slot 0 while the earlier arguments fragment stayed orphaned in physical slot 1, producing invalid final JSON.

Validation

Reproduced on current origin/main with a minimal script: duplicate index: 0 entries remained as two physical list entries and the assertion that they merge failed.

env -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy uv run --with pytest --with pytest-xdist --with pytest-asyncio --with respx --with inline-snapshot --with syrupy --with anyio --with pydantic --with httpx --with distro pytest tests/lib/test_streaming_deltas.py tests/lib/chat/test_completions_streaming.py::test_duplicate_tool_call_index_in_initial_chunk -q
# 3 passed

uv run --with ruff ruff check src/openai/lib/streaming/_deltas.py src/openai/lib/streaming/_assistants.py src/openai/lib/streaming/chat/_completions.py tests/lib/test_streaming_deltas.py tests/lib/chat/test_completions_streaming.py
# All checks passed!

uv run --with ruff ruff format --check src/openai/lib/streaming/_deltas.py src/openai/lib/streaming/_assistants.py src/openai/lib/streaming/chat/_completions.py tests/lib/test_streaming_deltas.py tests/lib/chat/test_completions_streaming.py
# 5 files already formatted

git diff --check origin/main..HEAD
# passed

@fengjikui fengjikui requested a review from a team as a code owner June 23, 2026 07:54

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

Copy link
Copy Markdown

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: 8a006bb32c

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/openai/lib/streaming/_deltas.py
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