-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix: merge duplicate-index entries in streaming delta accumulator (#3201) #3446
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
C1-BA-B1-F3
wants to merge
1
commit into
openai:main
Choose a base branch
from
C1-BA-B1-F3:fix/duplicate-index-streaming
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.
+191
−2
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| """Tests for accumulate_delta handling of duplicate-index entries. | ||
|
|
||
| Issue #3201: when the first streamed chunk contains multiple tool_calls entries | ||
| with the same `index`, the accumulator stores them as separate list entries. | ||
| Subsequent chunks only merge into the first one, leaving the second stranded. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from openai.lib.streaming._deltas import accumulate_delta | ||
|
|
||
|
|
||
| def test_single_index_in_first_chunk(): | ||
| """Normal case: one entry per index in the first chunk.""" | ||
| acc: dict[object, object] = {} | ||
| # First chunk: one tool_call at index 0 | ||
| acc = accumulate_delta(acc, { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_1", "function": {"name": "list_files"}, "type": "function"}, | ||
| ] | ||
| }) | ||
| # Second chunk: arguments for index 0 | ||
| acc = accumulate_delta(acc, { | ||
| "tool_calls": [ | ||
| {"index": 0, "function": {"arguments": ' {"path": "."}'}}, | ||
| ] | ||
| }) | ||
|
|
||
| tool_calls = acc["tool_calls"] | ||
| assert isinstance(tool_calls, list) | ||
| assert len(tool_calls) == 1 | ||
| assert tool_calls[0]["id"] == "call_1" | ||
| assert tool_calls[0]["function"]["name"] == "list_files" | ||
| assert tool_calls[0]["function"]["arguments"] == ' {"path": "."}' | ||
|
|
||
|
|
||
| def test_duplicate_index_in_first_chunk(): | ||
| """Issue #3201: first chunk has two entries with the same index.""" | ||
| acc: dict[object, object] = {} | ||
| # First chunk: two entries both at index 0 (id+name, then start of arguments) | ||
| acc = accumulate_delta(acc, { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_1", "function": {"name": "list_files"}, "type": "function"}, | ||
| {"index": 0, "function": {"arguments": ' {"'}}, | ||
| ] | ||
| }) | ||
| # Second chunk: rest of arguments for index 0 | ||
| acc = accumulate_delta(acc, { | ||
| "tool_calls": [ | ||
| {"index": 0, "function": {"arguments": 'path": "."}'}}, | ||
| ] | ||
| }) | ||
|
|
||
| tool_calls = acc["tool_calls"] | ||
| assert isinstance(tool_calls, list) | ||
| assert len(tool_calls) == 1, f"Expected 1 tool_call, got {len(tool_calls)}: {tool_calls}" | ||
| assert tool_calls[0]["id"] == "call_1" | ||
| assert tool_calls[0]["function"]["name"] == "list_files" | ||
| assert tool_calls[0]["function"]["arguments"] == ' {"path": "."}' | ||
|
|
||
|
|
||
| def test_duplicate_index_preserves_type_and_id(): | ||
| """Ensure merged entry keeps id and type from the first occurrence.""" | ||
| acc: dict[object, object] = {} | ||
| acc = accumulate_delta(acc, { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_abc", "type": "function", "function": {"name": "search"}}, | ||
| {"index": 0, "function": {"arguments": '{"q":'}}, | ||
| ] | ||
| }) | ||
|
|
||
| tool_calls = acc["tool_calls"] | ||
| assert len(tool_calls) == 1 | ||
| assert tool_calls[0]["id"] == "call_abc" | ||
| assert tool_calls[0]["type"] == "function" | ||
| assert tool_calls[0]["function"]["name"] == "search" | ||
| assert tool_calls[0]["function"]["arguments"] == '{"q":' | ||
|
|
||
|
|
||
| def test_multiple_distinct_indices_in_first_chunk(): | ||
| """Multiple entries with different indices should stay separate.""" | ||
| acc: dict[object, object] = {} | ||
| acc = accumulate_delta(acc, { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_1", "function": {"name": "fn_a"}, "type": "function"}, | ||
| {"index": 1, "id": "call_2", "function": {"name": "fn_b"}, "type": "function"}, | ||
| ] | ||
| }) | ||
|
|
||
| tool_calls = acc["tool_calls"] | ||
| assert isinstance(tool_calls, list) | ||
| assert len(tool_calls) == 2 | ||
| assert tool_calls[0]["id"] == "call_1" | ||
| assert tool_calls[1]["id"] == "call_2" | ||
|
|
||
|
|
||
| def test_multiple_indices_with_duplicates_in_first_chunk(): | ||
| """Mix of duplicate and distinct indices.""" | ||
| acc: dict[object, object] = {} | ||
| acc = accumulate_delta(acc, { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_1", "function": {"name": "fn_a"}, "type": "function"}, | ||
| {"index": 0, "function": {"arguments": '{"a":'}}, | ||
| {"index": 1, "id": "call_2", "function": {"name": "fn_b"}, "type": "function"}, | ||
| {"index": 1, "function": {"arguments": '{"b":'}}, | ||
| ] | ||
| }) | ||
| # More args for both | ||
| acc = accumulate_delta(acc, { | ||
| "tool_calls": [ | ||
| {"index": 0, "function": {"arguments": '"x"}'}}, | ||
| {"index": 1, "function": {"arguments": '"y"}'}}, | ||
| ] | ||
| }) | ||
|
|
||
| tool_calls = acc["tool_calls"] | ||
| assert isinstance(tool_calls, list) | ||
| assert len(tool_calls) == 2 | ||
| assert tool_calls[0]["id"] == "call_1" | ||
| assert tool_calls[0]["function"]["arguments"] == '{"a":"x"}' | ||
| assert tool_calls[1]["id"] == "call_2" | ||
| assert tool_calls[1]["function"]["arguments"] == '{"b":"y"}' | ||
|
|
||
|
|
||
| def test_non_indexed_lists_unchanged(): | ||
| """Lists without integer `index` fields should pass through normally.""" | ||
| acc: dict[object, object] = {} | ||
| acc = accumulate_delta(acc, { | ||
| "content": ["hello", "world"] | ||
| }) | ||
| acc = accumulate_delta(acc, { | ||
| "content": ["!"] | ||
| }) | ||
|
|
||
| assert acc["content"] == ["hello", "world", "!"] |
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.
When the duplicate
tool_callsentries arrive in the very first chat streaming chunk, this branch is never reached:ChatCompletionStreamState._accumulate_chunk()returns_convert_initial_chunk_into_snapshot()while the snapshot isNone, and that path copieschoice.delta.to_dict()directly. In that provider scenario, the first chunk still stores two entries withindex == 0, so the next chunk merges intotool_calls[0]and leaves the argument prefix stranded intool_calls[1]; apply the same normalization in the initial conversion path or route the first chunk through the accumulator.Useful? React with 👍 / 👎.