-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix: merge streaming tool calls by logical index #3377
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -744,7 +744,7 @@ def _convert_initial_chunk_into_snapshot(chunk: ChatCompletionChunk) -> ParsedCh | |
| for choice in chunk.choices: | ||
| choices[choice.index] = { | ||
| **choice.model_dump(exclude_unset=True, exclude={"delta"}), | ||
| "message": choice.delta.to_dict(), | ||
| "message": accumulate_delta({}, cast("dict[object, object]", choice.delta.to_dict())), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This normalization only runs for choices present in the stream's first chunk. When streaming multiple choices, a choice may first appear in a later chunk; the Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| return cast( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from openai.lib.streaming import _deltas, _assistants | ||
|
|
||
|
|
||
| def test_accumulate_delta_merges_duplicate_indexes_on_first_chunk() -> None: | ||
| acc: dict[object, object] = {} | ||
|
|
||
| _deltas.accumulate_delta( | ||
| acc, | ||
| { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_abc", "type": "function", "function": {"name": "list_files"}}, | ||
| {"index": 0, "function": {"arguments": ' {"'}}, | ||
| ] | ||
| }, | ||
| ) | ||
| _deltas.accumulate_delta(acc, {"tool_calls": [{"index": 0, "function": {"arguments": 'path": "."}'}}]}) | ||
|
|
||
| assert acc["tool_calls"] == [ | ||
| { | ||
| "index": 0, | ||
| "id": "call_abc", | ||
| "type": "function", | ||
| "function": {"name": "list_files", "arguments": ' {"path": "."}'}, | ||
| } | ||
| ] | ||
|
|
||
|
|
||
| def test_assistants_accumulate_delta_merges_duplicate_indexes_on_first_chunk() -> None: | ||
| acc: dict[object, object] = {} | ||
|
|
||
| _assistants.accumulate_delta( | ||
| acc, | ||
| { | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_abc", "type": "function", "function": {"name": "list_files"}}, | ||
| {"index": 0, "function": {"arguments": ' {"'}}, | ||
| ] | ||
| }, | ||
| ) | ||
| _assistants.accumulate_delta(acc, {"tool_calls": [{"index": 0, "function": {"arguments": 'path": "."}'}}]}) | ||
|
|
||
| assert acc["tool_calls"] == [ | ||
| { | ||
| "index": 0, | ||
| "id": "call_abc", | ||
| "type": "function", | ||
| "function": {"name": "list_files", "arguments": ' {"path": "."}'}, | ||
| } | ||
| ] |
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.
For Assistants run steps,
run_step_snapshotsare seeded fromthread.run.step.createdwith finalRunSteptool-call objects, whose generated types do not include anindex; only subsequent delta objects carry one. With this new lookup, an existing unindexed entry never matchesdelta_entry["index"], so the first delta for tool call0is appended as a duplicate instead of being merged into the existing slot. The event handler later readsstep_snapshot.step_details.tool_calls[tool_call_delta.index], so code-interpreter/function deltas for created run steps can surface stale or empty snapshots rather than the accumulated arguments/input.Useful? React with 👍 / 👎.