Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Bug: accumulate_delta corrupts tool call items on out-of-order delta indexes

未关闭 适合新手
#4,022 6 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
api

调研方向

从 openai/lib/streaming/_deltas.py 中的 accumulate_delta 开始,复现报告的索引 1 先于索引 0 到达的情况。检查现有的 delta 累积测试,并添加对稀疏、乱序索引的测试覆盖。完成标准是每个工具调用都保留在其预期索引处,且不同调用中的字段不会被拼接在一起。

由索引模型根据 Issue 内容生成。

描述

Describe the bug

In openai.lib.streaming._deltas.accumulate_delta, list delta entries with index attributes are inserted into acc_value via acc_value.insert(index, delta_entry) when IndexError occurs:

try:
    acc_entry = acc_value[index]
except IndexError:
    acc_value.insert(index, delta_entry)

In Python, list.insert(index, obj) on a list shorter than index inserts the element at the end of the list (len(list)). When an out-of-order or sparse stream chunk arrives where index > len(acc_value) (for example, index = 1 arrives when acc_value is empty []), acc_value.insert(1, delta_entry) places the item at index 0.

When a subsequent chunk for index = 0 arrives, acc_value[0] matches the entry intended for index 1, causing accumulate_delta(acc_entry, delta_entry) to concatenate strings across distinct tool calls:

  • id: "call_1" + "call_0" -> "call_1call_0"
  • function.name: "func_1" + "func_0" -> "func_1func_0"
  • function.arguments: '{"b": 2}{"a": 1}' (invalid JSON syntax)
To Reproduce
from openai.lib.streaming._deltas import accumulate_delta

acc = {}
# Chunk for index 1 arrives first
accumulate_delta(
    acc,
    {
        "tool_calls": [
            {"index": 1, "id": "call_1", "type": "function", "function": {"name": "func_1", "arguments": '{"b": 2}'}}
        ]
    },
)

# Chunk for index 0 arrives second
accumulate_delta(
    acc,
    {
        "tool_calls": [
            {"index": 0, "id": "call_0", "type": "function", "function": {"name": "func_0", "arguments": '{"a": 1}'}}
        ]
    },
)

print(acc["tool_calls"])
# Corrupted Output: [{'index': 0, 'id': 'call_1call_0', 'type': 'function', 'function': {'name': 'func_1func_0', 'arguments': '{"b": 2}{"a": 1}'}}]
Expected Behavior

List items should occupy their exact numerical index without collisions or string concatenations between different tool calls.

主要语言
Python
星标
31.8k
派生
7.3k
平均合并
1 天 3 小时
30 天内合并 PR
128

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

openai/openai-python 的其他 Issue

查看 openai/openai-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。