Skip to content

Fix streaming delta merge for duplicate tool call indexes - #3425

Merged
marcuswood-oai merged 4 commits into
openai:mainfrom
fengjikui:codex/fix-duplicate-tool-call-deltas
Sep 10, 2026
Merged

marcuswood-oai merged 4 commits into
openai:mainfrom
fengjikui:codex/fix-duplicate-tool-call-deltas

Conversation

@fengjikui

@fengjikui fengjikui commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 10, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-10T17:06:48.371700Z ac1c3c4 New commits
🔒 Security Review Completed 2026-09-10T17:07:53.659645Z ac1c3c4 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

Castiron custom code

✅ No new custom-code files detected.

36 mixed files remain; 0 existing customizations changed.

Compared 802b334928d8ac1c3c445261. Generated baselines verified.

36 existing customizations unchanged
  • api.md
  • scripts/castiron/README.md
  • scripts/castiron/custom_code_report.py
  • scripts/castiron/test_custom_code_report.py
  • src/openai/init.py
  • src/openai/_client.py
  • src/openai/resources/audio/transcriptions.py
  • src/openai/resources/audio/translations.py
  • src/openai/resources/beta/beta.py
  • src/openai/resources/beta/responses/responses.py
  • src/openai/resources/beta/threads/runs/runs.py
  • src/openai/resources/beta/threads/threads.py
  • src/openai/resources/chat/completions/completions.py
  • src/openai/resources/embeddings.py
  • src/openai/resources/files.py
  • src/openai/resources/realtime/realtime.py
  • src/openai/resources/responses/responses.py
  • src/openai/resources/uploads/uploads.py
  • src/openai/resources/vector_stores/file_batches.py
  • src/openai/resources/vector_stores/files.py
  • src/openai/resources/videos.py
  • src/openai/resources/webhooks/init.py
  • src/openai/resources/webhooks/webhooks.py
  • src/openai/types/chat/init.py
  • src/openai/types/chat/chat_completion_message_tool_call.py
  • src/openai/types/fine_tuning/fine_tuning_job_integration.py
  • src/openai/types/responses/init.py
  • src/openai/types/responses/response.py
  • src/openai/types/responses/response_function_web_search.py
  • src/openai/types/responses/response_function_web_search_param.py
  • src/openai/types/responses/responses_client_event.py
  • src/openai/types/responses/responses_client_event_param.py
  • src/openai/types/responses/tool.py
  • src/openai/types/responses/tool_param.py
  • src/openai/types/webhooks/init.py
  • tests/api_resources/test_videos.py

A changed generated baseline means this report cannot reliably identify which handwritten lines changed.

Inspect the custom-code diff

Download the exact patch produced by this run (requires repository access):

gh run download 34505850390 --repo openai/openai-python \
  --name castiron-custom-code-34505850390-1 --dir /tmp/castiron-custom-code-34505850390-1
git apply --stat /tmp/castiron-custom-code-34505850390-1/custom-code.patch
cat /tmp/castiron-custom-code-34505850390-1/custom-code.patch

Or reproduce it from an SDK checkout containing the vendored reporter:

git fetch --no-tags origin 802b334928d8f0c3e24e5fce15a0eaa1b28c61cd ac1c3c445261639676a69a956c1a22153ee16c7a
python3 scripts/castiron/custom_code_report.py report \
  --base 802b334928d8f0c3e24e5fce15a0eaa1b28c61cd \
  --head ac1c3c445261639676a69a956c1a22153ee16c7a --fetch --require-head-hash --public \
  --out /tmp/castiron-custom-code-ac1c3c445261
cat /tmp/castiron-custom-code-ac1c3c445261/custom-code.patch

This is the current full custom patch for mixed files, not an attribution of only the handwritten lines changed by this PR.

Full report and patch

@marcuswood-oai marcuswood-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix and regression tests! We’ve simplified the shared accumulator and expanded the coverage. The full diff review and checks look good.

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

2 participants