AsyncOpenAI.responses.stream() silently skips input validation due to stale isinstance(input, NotGiven) check

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

问题位于 src/openai/lib/_response_resources.py 的 async_stream() 中:第 587 行附近的 isinstance(input, NotGiven) 检查和第 650 行附近的 isinstance(response_id, Omit) 检查,应与同步版 stream() 已在使用的 is_given() 辅助函数保持一致。请先阅读第 412 行和第 477 行附近的同步版 stream() 以了解该模式。完成标准是:当省略 input 时,AsyncOpenAI 抛出与 OpenAI 相同的 ValueError,并且有测试覆盖两种情况:omit 情况和 NOT_GIVEN 情况。

由索引模型根据 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/_response_resources.py, async_stream() (backing AsyncOpenAI.responses.stream()) checks:

  • Line 587: if isinstance(input, NotGiven):
  • Line 650: if isinstance(response_id, Omit):

However, both input and response_id default to omit (an instance of Omit, not NotGiven), and isinstance(omit, NotGiven) is False.

As a result:

  1. Calling async_client.responses.stream(model="gpt-4o-mini") without input silently bypasses ValueError("input must be provided when creating a new response"), returning an AsyncResponseStreamManager wrapping a live resource.create(input=omit, ...) coroutine (which dispatches an invalid POST /v1/responses request when entered, or emits RuntimeWarning: coroutine 'AsyncResponses.create' was never awaited if not entered).
  2. Passing legacy NOT_GIVEN as response_id similarly bypasses line 650 (isinstance(response_id, Omit)), failing only later inside retrieve() upon __aenter__ instead of failing fast at call time.

By contrast, sync stream() in the same file uses if not is_given(input): (line 412) and if not is_given(response_id): (line 477), and async_stream() itself uses if not is_given(model): three lines below (line 590).

To Reproduce
  1. Instantiate both OpenAI and AsyncOpenAI.
  2. Call .responses.stream(model="gpt-4o-mini") without input.
  3. Observe that OpenAI immediately raises ValueError("input must be provided when creating a new response"), while AsyncOpenAI returns an AsyncResponseStreamManager without raising.
Code snippets
from openai import AsyncOpenAI, OpenAI
from openai._types import NOT_GIVEN

sync_client = OpenAI(api_key="test-key")
async_client = AsyncOpenAI(api_key="test-key")

# Sync raises ValueError as expected:
try:
    sync_client.responses.stream(model="gpt-4o-mini")
except ValueError as exc:
    print("sync:", exc)
# sync: input must be provided when creating a new response

# Async silently bypasses validation because isinstance(omit, NotGiven) is False:
mgr = async_client.responses.stream(model="gpt-4o-mini")
print("async returned:", type(mgr).__name__)
# async returned: AsyncResponseStreamManager
mgr._AsyncResponseStreamManager__api_request.close()

# Same for legacy NOT_GIVEN on response_id:
mgr2 = async_client.responses.stream(response_id=NOT_GIVEN)
print("async response_id=NOT_GIVEN returned:", type(mgr2).__name__)
# async response_id=NOT_GIVEN returned: AsyncResponseStreamManager
mgr2._AsyncResponseStreamManager__api_request.close()

Suggested fix in src/openai/lib/_response_resources.py:

     tools = _make_tools(tools)
     if len(new_response_args_names) > 0:
-        if isinstance(input, NotGiven):
+        if not is_given(input):
             raise ValueError("input must be provided when creating a new response")
@@ -647,7 +647,7 @@
             starting_after=None,
         )
     else:
-        if isinstance(response_id, Omit):
+        if not is_given(response_id):
             raise ValueError("response_id must be provided when streaming an existing response")
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

环境准备

在 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 摘要。