chat.completions.stream() drops tool_calls.function.arguments.done when a tool call arrives in the same chunk as finish_reason
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 82/100
Rechercherichtung
Beginne in src/openai/lib/streaming/chat/_completions.py bei ChoiceEventState.get_done_events(), lies die gesamte Methode, um zu sehen, wie __current_tool_call_index, _done_tool_calls und der finish_reason-Zweig zusammenspielen. Reproduziere es mit dem Snippet im Issue oder führe die Streaming-Tests unter tests/lib/streaming/chat/ aus, um zu sehen, wie Event-Sequenzen geprüft werden, und verschiebe dann die Tool-Call-Schleife über die finish_reason-Prüfung. Erledigt bedeutet, dass sowohl der Single-Chunk- als auch der Second-Tool-Call-Fall tool_calls.function.arguments.done ausgeben und die bestehenden Multi-Chunk-Ordering-Tests weiterhin bestehen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This is an issue with the Python library
Describe the bug
In src/openai/lib/streaming/chat/_completions.py, ChoiceEventState.get_done_events() (lines 610-639) checks if choice_snapshot.finish_reason: before iterating over choice_chunk.delta.tool_calls to update self.__current_tool_call_index:
if choice_snapshot.finish_reason:
events_to_fire.extend(self._content_done_events(...))
if (
self.__current_tool_call_index is not None
and self.__current_tool_call_index not in self._done_tool_calls
):
self._add_tool_done_event(..., tool_index=self.__current_tool_call_index)
for tool_call in choice_chunk.delta.tool_calls or []:
if self.__current_tool_call_index != tool_call.index:
...
self.__current_tool_call_index = tool_call.index
As a result, whenever a chunk introduces a tool call and sets finish_reason (common with single-chunk tool calls, multi-tool calls in the terminal chunk, or OpenAI-compatible gateways/proxies):
- For a single tool call in the terminal chunk,
self.__current_tool_call_indexis stillNonewhenfinish_reasonis processed, sotool_calls.function.arguments.doneis never emitted (andparsed_argumentsis never finalized on the snapshot). - When a second tool call (e.g.
index=1) arrives in the terminal chunk,index=0emitsarguments.done, thenself.__current_tool_call_indexis updated to1after thefinish_reasoncheck, soindex=1never emitstool_calls.function.arguments.done.
Processing choice_chunk.delta.tool_calls before if choice_snapshot.finish_reason: preserves exact event ordering for multi-chunk streams (content.done -> prior tool arguments.done -> final tool arguments.done) and ensures every tool call receives its done event.
To Reproduce
- Create a
ChatCompletionStreamState(orclient.chat.completions.stream()) with a function tool. - Feed a chunk containing both
delta.tool_calls(with a new tool call index) andfinish_reason="tool_calls". - Observe that
tool_calls.function.arguments.deltais emitted, buttool_calls.function.arguments.doneis never emitted.
Code snippets
from pydantic import BaseModel
from openai import pydantic_function_tool
from openai.lib.streaming.chat import ChatCompletionStreamState
from openai.types.chat import ChatCompletionChunk
class GetWeather(BaseModel):
city: str
state = ChatCompletionStreamState(input_tools=[pydantic_function_tool(GetWeather)])
events = list(
state.handle_chunk(
ChatCompletionChunk.model_validate(
{
"id": "chatcmpl-1",
"object": "chat.completion.chunk",
"created": 1,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"tool_calls": [
{
"index": 0,
"id": "call_1",
"type": "function",
"function": {"name": "GetWeather", "arguments": '{"city": "NYC"}'},
}
],
},
"finish_reason": "tool_calls",
}
],
}
)
)
)
print([e.type for e in events])
# Actual: ['chunk', 'tool_calls.function.arguments.delta']
# Expected: ['chunk', 'tool_calls.function.arguments.delta', 'tool_calls.function.arguments.done']
Suggested fix in src/openai/lib/streaming/chat/_completions.py (ChoiceEventState.get_done_events):
+ for tool_call in choice_chunk.delta.tool_calls or []:
+ if self.__current_tool_call_index != tool_call.index:
+ events_to_fire.extend(
+ self._content_done_events(choice_snapshot=choice_snapshot, response_format=response_format)
+ )
+
+ if self.__current_tool_call_index is not None:
+ self._add_tool_done_event(
+ events_to_fire=events_to_fire,
+ choice_snapshot=choice_snapshot,
+ tool_index=self.__current_tool_call_index,
+ )
+
+ self.__current_tool_call_index = tool_call.index
+
if choice_snapshot.finish_reason: # ty: ignore[redundant-condition]
events_to_fire.extend(
self._content_done_events(choice_snapshot=choice_snapshot, response_format=response_format)
)
@@ -622,19 +636,5 @@
tool_index=self.__current_tool_call_index,
)
-
- for tool_call in choice_chunk.delta.tool_calls or []:
- if self.__current_tool_call_index != tool_call.index:
- events_to_fire.extend(
- self._content_done_events(choice_snapshot=choice_snapshot, response_format=response_format)
- )
-
- if self.__current_tool_call_index is not None:
- self._add_tool_done_event(
- events_to_fire=events_to_fire,
- choice_snapshot=choice_snapshot,
- tool_index=self.__current_tool_call_index,
- )
-
- self.__current_tool_call_index = tool_call.index
OS
macOS
Python version
Python 3.14
Library version
openai v3.57.0 (main@f63f14b)
- Vorherrschende Sprache
- Python
- Sterne
- 32.2k
- Forks
- 5.8k
- Ø Merge
- 12 Std. 29 Min.
- Gemergte PRs (30 T.)
- 112
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus openai/openai-python
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
openai/openai-python#4338 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
openai/openai-python#4337 ·
Maintainer antworten meist innerhalb von 1 Tag
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersEvtl. vergeben @Rewaskc02-lang hat das vor 1 Tag übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
openai/openai-python#4311 · 4 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
openai/openai-python#4310 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 82/100
openai/openai-python#4308 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in openai/openai-python
Ähnliche Issues
-
ui
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Maintainer antworten meist innerhalb von 1 Tag
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonEvtl. vergeben @ronaldtse hat das heute übernommen. Offen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 95/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
pydantic/pytest-examples#102 · 1 Kommentar ·