chat.completions.stream() drops tool_calls.function.arguments.done when a tool call arrives in the same chunk as finish_reason

Đang mở Phù hợp với người mới
#4,309 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
api

Hướng nghiên cứu

Bắt đầu trong src/openai/lib/streaming/chat/_completions.py tại ChoiceEventState.get_done_events(), đọc toàn bộ phương thức để xem __current_tool_call_index, _done_tool_calls và nhánh finish_reason tương tác với nhau như thế nào. Tái hiện bằng đoạn snippet trong issue, hoặc chạy các bài test streaming dưới tests/lib/streaming/chat/ để xem các chuỗi sự kiện được assert ra sao, rồi di chuyển vòng lặp tool-call lên trên kiểm tra finish_reason. Hoàn thành nghĩa là cả trường hợp single-chunk và second-tool-call đều phát ra tool_calls.function.arguments.done và các test thứ tự multi-chunk hiện có vẫn pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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):

  1. For a single tool call in the terminal chunk, self.__current_tool_call_index is still None when finish_reason is processed, so tool_calls.function.arguments.done is never emitted (and parsed_arguments is never finalized on the snapshot).
  2. When a second tool call (e.g. index=1) arrives in the terminal chunk, index=0 emits arguments.done, then self.__current_tool_call_index is updated to 1 after the finish_reason check, so index=1 never emits tool_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
  1. Create a ChatCompletionStreamState (or client.chat.completions.stream()) with a function tool.
  2. Feed a chunk containing both delta.tool_calls (with a new tool call index) and finish_reason="tool_calls".
  3. Observe that tool_calls.function.arguments.delta is emitted, but tool_calls.function.arguments.done is 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)

Ngôn ngữ chính
Python
Star
32.2k
Fork
5.8k
Merge trung bình
12 giờ 29 phút
Pull request đã merge (30 ngày)
112

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của openai/openai-python

Tất cả issue của openai/openai-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.