chat.completions.stream() drops tool_calls.function.arguments.done when a tool call arrives in the same chunk as finish_reason
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 82/100
Direzione di ricerca
Inizia in src/openai/lib/streaming/chat/_completions.py da ChoiceEventState.get_done_events(), leggendo l'intero metodo per vedere come interagiscono __current_tool_call_index, _done_tool_calls e il ramo finish_reason. Riproduci con lo snippet nell'issue, oppure esegui i test di streaming sotto tests/lib/streaming/chat/ per vedere come vengono verificati gli eventi in sequenza, poi sposta il ciclo tool-call sopra il controllo finish_reason. Fatto significa che sia il caso single-chunk sia quello second-tool-call emettono tool_calls.function.arguments.done e che i test esistenti sull'ordinamento multi-chunk continuano a passare.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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)
- Lingua principale
- Python
- Stelle
- 32.2k
- Fork
- 5.8k
- Merge medio
- 12h 29m
- PR unite (30g)
- 112
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openai/openai-python
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
openai/openai-python#4338 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
openai/openai-python#4337 ·
I maintainer di solito rispondono entro 1 giorno
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersForse già presa @Rewaskc02-lang l’ha presa 1 giorno fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openai/openai-python#4311 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openai/openai-python#4310 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
openai/openai-python#4308 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openai/openai-python
Issue simili
-
ui
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
I maintainer di solito rispondono entro 1 giorno
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonForse già presa @ronaldtse l’ha presa oggi. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
pydantic/pytest-examples#102 · 1 commento ·