AsyncOpenAI.responses.stream() silently skips input validation due to stale isinstance(input, NotGiven) check
維護者通常 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:
- Calling
async_client.responses.stream(model="gpt-4o-mini")withoutinputsilently bypassesValueError("input must be provided when creating a new response"), returning anAsyncResponseStreamManagerwrapping a liveresource.create(input=omit, ...)coroutine (which dispatches an invalidPOST /v1/responsesrequest when entered, or emitsRuntimeWarning: coroutine 'AsyncResponses.create' was never awaitedif not entered). - Passing legacy
NOT_GIVENasresponse_idsimilarly bypasses line 650 (isinstance(response_id, Omit)), failing only later insideretrieve()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
- Instantiate both
OpenAIandAsyncOpenAI. - Call
.responses.stream(model="gpt-4o-mini")withoutinput. - Observe that
OpenAIimmediately raisesValueError("input must be provided when creating a new response"), whileAsyncOpenAIreturns anAsyncResponseStreamManagerwithout 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
環境準備
在瀏覽器裡用你自己的 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 於 2 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
openai/openai-python#4311 · 4 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
openai/openai-python#4310 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 82/100
openai/openai-python#4309 · 1 則留言 ·
維護者通常 1 天內回覆
查看 openai/openai-python 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 62/100
-
bug help wanted
難度 2/5 1-3 小時 新手友好度 86/100
cofade/open-garden-planner#1044 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 68/100
codecrafters-io/build-your-own-x#2126 ·
維護者通常 5 天內回覆
-
documentation good first issue
難度 2/5 1-3 小時 新手友好度 84/100
julianleno/cortai#10 ·
-
difficulty:easy func:python os:windows prio:high type:bug
難度 2/5 1-3 小時 新手友好度 78/100
NatronGitHub/Natron#2029 ·
維護者通常 1 天內回覆