-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix streaming tool_call delta accumulation with duplicate indexes #3508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mangodxd
wants to merge
2
commits into
openai:main
Choose a base branch
from
mangodxd:fix/streaming-duplicate-toolcall-indexes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+265
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from openai.lib.streaming._deltas import accumulate_delta | ||
|
|
||
|
|
||
| def test_duplicate_index_in_first_chunk() -> None: | ||
| """ | ||
| Regression test for issue #3201: | ||
| When the first chunk contains multiple tool_calls entries with the same | ||
| `index`, they should be merged into a single entry by logical index, | ||
| not stored as separate physical entries. | ||
| """ | ||
| chunk1 = { | ||
| "tool_calls": [ | ||
| { | ||
| "index": 0, | ||
| "id": "call_1", | ||
| "type": "function", | ||
| "function": {"name": "get_weather", "arguments": ""}, | ||
| }, | ||
| { | ||
| "index": 0, | ||
| "function": {"arguments": ' {"cit'}, | ||
| }, | ||
| ] | ||
| } | ||
|
|
||
| chunk2 = { | ||
| "tool_calls": [ | ||
| { | ||
| "index": 0, | ||
| "function": {"arguments": 'y": "London"}'}, | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| acc: dict = {} | ||
| acc = accumulate_delta(acc, chunk1) | ||
| assert len(acc["tool_calls"]) == 1, f"Expected 1 tool call, got {len(acc['tool_calls'])}" | ||
| assert acc["tool_calls"][0]["function"]["arguments"] == ' {"cit' | ||
|
|
||
| acc = accumulate_delta(acc, chunk2) | ||
| assert len(acc["tool_calls"]) == 1, f"Expected 1 tool call, got {len(acc['tool_calls'])}" | ||
| assert acc["tool_calls"][0]["function"]["arguments"] == ' {"city": "London"}' | ||
|
|
||
|
|
||
| def test_multiple_tool_calls_different_indexes() -> None: | ||
| """Multiple tool calls with different indexes should remain separate.""" | ||
| chunk1 = { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_1", "type": "function", "function": {"name": "get_weather", "arguments": ""}}, | ||
| {"index": 1, "id": "call_2", "type": "function", "function": {"name": "get_time", "arguments": ""}}, | ||
| ] | ||
| } | ||
|
|
||
| chunk2 = { | ||
| "tool_calls": [ | ||
| {"index": 0, "function": {"arguments": ' {"city": "London"}'}}, | ||
| {"index": 1, "function": {"arguments": ' {"tz": "UTC"}'}}, | ||
| ] | ||
| } | ||
|
|
||
| acc: dict = {} | ||
| acc = accumulate_delta(acc, chunk1) | ||
| assert len(acc["tool_calls"]) == 2 | ||
|
|
||
| acc = accumulate_delta(acc, chunk2) | ||
| assert len(acc["tool_calls"]) == 2 | ||
| assert acc["tool_calls"][0]["function"]["arguments"] == ' {"city": "London"}' | ||
| assert acc["tool_calls"][1]["function"]["arguments"] == ' {"tz": "UTC"}' | ||
|
|
||
|
|
||
| def test_sparse_indexes() -> None: | ||
| """Sparse indexes (index 2 arriving before index 1) should be handled correctly. | ||
|
|
||
| Note: The accumulator inserts at the physical position matching the index, | ||
| so index 2 is stored at position 0 when the list is empty, then index 1 | ||
| is inserted at position 1 (append). This is existing behavior. | ||
| """ | ||
| chunk1 = { | ||
| "tool_calls": [ | ||
| {"index": 2, "id": "call_3", "type": "function", "function": {"name": "search", "arguments": ""}}, | ||
| ] | ||
| } | ||
|
|
||
| chunk2 = { | ||
| "tool_calls": [ | ||
| {"index": 1, "id": "call_2", "type": "function", "function": {"name": "get_time", "arguments": ""}}, | ||
| ] | ||
| } | ||
|
|
||
| acc: dict = {} | ||
| acc = accumulate_delta(acc, chunk1) | ||
| assert len(acc["tool_calls"]) == 1 | ||
| assert acc["tool_calls"][0]["index"] == 2 | ||
|
|
||
| acc = accumulate_delta(acc, chunk2) | ||
| assert len(acc["tool_calls"]) == 2 | ||
| assert acc["tool_calls"][0]["index"] == 2 | ||
| assert acc["tool_calls"][1]["index"] == 1 | ||
|
|
||
|
|
||
| def test_normal_case_no_duplicates() -> None: | ||
| """Normal case with no duplicate indexes should work as before.""" | ||
| chunk1 = { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_1", "type": "function", "function": {"name": "get_weather", "arguments": ""}}, | ||
| ] | ||
| } | ||
|
|
||
| chunk2 = { | ||
| "tool_calls": [ | ||
| {"index": 0, "function": {"arguments": ' {"city": "London"}'}}, | ||
| ] | ||
| } | ||
|
|
||
| acc: dict = {} | ||
| acc = accumulate_delta(acc, chunk1) | ||
| assert len(acc["tool_calls"]) == 1 | ||
|
|
||
| acc = accumulate_delta(acc, chunk2) | ||
| assert len(acc["tool_calls"]) == 1 | ||
| assert acc["tool_calls"][0]["function"]["arguments"] == ' {"city": "London"}' | ||
|
|
||
|
|
||
| def test_three_duplicate_indexes_in_first_chunk() -> None: | ||
| """Three entries with the same index in the first chunk should all be merged.""" | ||
| chunk1 = { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_1", "type": "function", "function": {"name": "test", "arguments": ""}}, | ||
| {"index": 0, "function": {"arguments": ' {"a"'}}, | ||
| {"index": 0, "function": {"arguments": ': 1}'}}, | ||
| ] | ||
| } | ||
|
|
||
| acc: dict = {} | ||
| acc = accumulate_delta(acc, chunk1) | ||
| assert len(acc["tool_calls"]) == 1 | ||
| assert acc["tool_calls"][0]["function"]["arguments"] == ' {"a": 1}' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes the
accumulate_deltafast path, but duplicatetool_callson the very firstChatCompletionStreamStatechunk still bypass this code:_accumulate_chunk()returns_convert_initial_chunk_into_snapshot(), which seeds the message from rawchoice.delta.to_dict()instead of callingaccumulate_delta. In that first-SSE-chunk scenario the duplicate entries are still stored as separate physical list items, so later fragments keep merging into the wrong entry; please route the initial snapshot creation through the same index merge/normalization.Useful? React with 👍 / 👎.