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

オープン 初心者向け
#4,306 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
112

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openai/openai-python のほかの issue

openai/openai-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。