-
Notifications
You must be signed in to change notification settings - Fork 5k
fix: merge list entries by logical index instead of physical position (#3201) #3521
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
rkfshakti
wants to merge
2
commits into
openai:main
Choose a base branch
from
rkfshakti:fix/duplicate-tool-call-index-accumulation
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.
+168
−8
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """Tests for the streaming delta accumulator.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from openai.lib.streaming._deltas import accumulate_delta | ||
|
|
||
|
|
||
| class TestAccumulateDelta: | ||
| """Tests for accumulate_delta — regression for #3201.""" | ||
|
|
||
| def test_duplicate_index_first_chunk_merges(self) -> None: | ||
| """First chunk with two entries at the same index should merge into one.""" | ||
| acc: dict[object, object] = {} | ||
| delta = { | ||
| "tool_calls": [ | ||
| { | ||
| "index": 0, | ||
| "id": "call_abc", | ||
| "function": {"name": "list_files"}, | ||
| "type": "function", | ||
| }, | ||
| { | ||
| "index": 0, | ||
| "function": {"arguments": ' {"'}, | ||
| }, | ||
| ] | ||
| } | ||
| result = accumulate_delta(acc, delta) | ||
| calls = result["tool_calls"] | ||
| assert isinstance(calls, list) | ||
| # Should be a single entry at index 0, not two | ||
| assert len(calls) == 1 | ||
| assert calls[0]["index"] == 0 | ||
| assert calls[0]["id"] == "call_abc" | ||
| assert calls[0]["function"]["name"] == "list_files" | ||
| assert calls[0]["function"]["arguments"] == ' {"' | ||
|
|
||
| def test_duplicate_index_subsequent_chunk_merges(self) -> None: | ||
| """Subsequent chunk with same index should merge into existing entry.""" | ||
| acc: dict[object, object] = { | ||
| "tool_calls": [ | ||
| { | ||
| "index": 0, | ||
| "id": "call_abc", | ||
| "function": {"name": "list_files", "arguments": ' {"'}, | ||
| "type": "function", | ||
| } | ||
| ] | ||
| } | ||
| delta = { | ||
| "tool_calls": [ | ||
| { | ||
| "index": 0, | ||
| "function": {"arguments": 'path": "."}'}, | ||
| } | ||
| ] | ||
| } | ||
| result = accumulate_delta(acc, delta) | ||
| calls = result["tool_calls"] | ||
| assert len(calls) == 1 | ||
| assert calls[0]["function"]["arguments"] == ' {"path": "."}' | ||
|
|
||
| def test_different_indexes_accumulate_separately(self) -> None: | ||
| """Entries with different indexes should accumulate separately.""" | ||
| acc: dict[object, object] = {} | ||
| delta1 = { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_a", "function": {"name": "tool_a"}, "type": "function"}, | ||
| ] | ||
| } | ||
| delta2 = { | ||
| "tool_calls": [ | ||
| {"index": 1, "id": "call_b", "function": {"name": "tool_b"}, "type": "function"}, | ||
| ] | ||
| } | ||
| result = accumulate_delta(acc, delta1) | ||
| result = accumulate_delta(result, delta2) | ||
| calls = result["tool_calls"] | ||
| assert len(calls) == 2 | ||
| assert calls[0]["index"] == 0 | ||
| assert calls[1]["index"] == 1 | ||
|
|
||
| def test_string_accumulation_unchanged(self) -> None: | ||
| """Basic string accumulation should still work.""" | ||
| acc: dict[object, object] = {"content": "hello"} | ||
| delta = {"content": " world"} | ||
| result = accumulate_delta(acc, delta) | ||
| assert result["content"] == "hello world" | ||
|
|
||
| def test_duplicate_index_first_chunk_then_subsequent_merge(self) -> None: | ||
| """Full round-trip: first chunk with duplicate indexes, then subsequent chunk merges correctly.""" | ||
| acc: dict[object, object] = {} | ||
| # First chunk: two entries at index 0 | ||
| delta1 = { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_abc", "function": {"name": "list_files"}, "type": "function"}, | ||
| {"index": 0, "function": {"arguments": ' {"'}}, | ||
| ] | ||
| } | ||
| result = accumulate_delta(acc, delta1) | ||
| calls = result["tool_calls"] | ||
| assert len(calls) == 1, f"Expected 1 entry after coalescing, got {len(calls)}" | ||
| assert calls[0]["function"]["arguments"] == ' {"' | ||
|
|
||
| # Second chunk: more arguments for index 0 | ||
| delta2 = { | ||
| "tool_calls": [ | ||
| {"index": 0, "function": {"arguments": 'path": "."}'}}, | ||
| ] | ||
| } | ||
| result = accumulate_delta(result, delta2) | ||
| calls = result["tool_calls"] | ||
| assert len(calls) == 1 | ||
| assert calls[0]["function"]["arguments"] == ' {"path": "."}' | ||
| assert calls[0]["id"] == "call_abc" | ||
| assert calls[0]["function"]["name"] == "list_files" |
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
tool_callsis first added to the snapshot it is still copied directly, so this loop can receive anacc_valuethat already contains two dicts with the sameindexfrom the first chunk. In that scenario this code merges the next delta into only the first matching entry and then breaks, leaving the earlier duplicate entry stranded; the example in the commit message still produces duplicated index0entries and broken accumulated arguments unless the existing list is coalesced before or during this merge.Useful? React with 👍 / 👎.