chat.completions.stream() drops tool_calls.function.arguments.done when a tool call arrives in the same chunk as finish_reason
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 82/100
Piste de recherche
Commence dans src/openai/lib/streaming/chat/_completions.py à ChoiceEventState.get_done_events(), en lisant toute la méthode pour voir comment __current_tool_call_index, _done_tool_calls et la branche finish_reason interagissent. Reproduis avec l’extrait de l’issue, ou exécute les tests de streaming sous tests/lib/streaming/chat/ pour voir comment les séquences d’événements sont vérifiées, puis déplace la boucle tool-call au-dessus de la vérification finish_reason. Terminé signifie que les cas single-chunk et second-tool-call émettent tous deux tool_calls.function.arguments.done et que les tests existants d’ordre multi-chunk passent toujours.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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)
- Langage dominant
- Python
- Étoiles
- 32.2k
- Forks
- 5.8k
- Merge moyen
- 12 h 29 min
- PR mergées (30 j)
- 112
Préparer son environnement
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de openai/openai-python
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
openai/openai-python#4338 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
openai/openai-python#4337 ·
Les mainteneurs répondent en général sous 1 jour
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersPeut-être pris @Rewaskc02-lang l’a pris il y a 1 jour. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
openai/openai-python#4311 · 4 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
openai/openai-python#4310 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 82/100
openai/openai-python#4308 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de openai/openai-python
Issues similaires
-
ui
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Les mainteneurs répondent en général sous 1 jour
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonPeut-être pris @ronaldtse l’a pris aujourd’hui. Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 95/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
pydantic/pytest-examples#102 · 1 commentaire ·