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