chat.completions.stream() drops tool_calls.function.arguments.done when a tool call arrives in the same chunk as finish_reason
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 src/openai/lib/streaming/chat/_completions.py 中的 ChoiceEventState.get_done_events() 开始,阅读整个方法,了解 __current_tool_call_index、_done_tool_calls 与 finish_reason 分支如何相互作用。用 issue 中的代码片段复现,或运行 tests/lib/streaming/chat/ 下的 streaming 测试,看看事件序列是如何断言的,然后把 tool-call 循环移到 finish_reason 检查之前。完成意味着 single-chunk 和 second-tool-call 两种情况都会发出 tool_calls.function.arguments.done,并且现有的 multi-chunk 顺序测试仍然通过。
由索引模型根据 Issue 内容生成。
描述
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)
- 主要语言
- Python
- 星标
- 32.2k
- 派生
- 5.8k
- 平均合并
- 12 小时 29 分钟
- 30 天内合并 PR
- 112
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
openai/openai-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
openai/openai-python#4338 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 65/100
openai/openai-python#4337 ·
维护者通常 1 天内回复
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parameters可能已有人在做 @Rewaskc02-lang 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
openai/openai-python#4311 · 4 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
openai/openai-python#4310 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 82/100
openai/openai-python#4308 · 1 条评论 ·
维护者通常 1 天内回复
查看 openai/openai-python 的全部 Issue
相似的 Issue
-
triage
难度 1/5 1-3 小时 新手友好度 88/100
kornia/kornia-rs#1416 · 1 条评论 ·
维护者通常 2 天内回复
-
ui
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
PolicyEngine/ai-inequality#64 ·
维护者通常 1 天内回复
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over time可能已有人在做 @PabloRoque 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
pymc-devs/pymc-extras#839 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复