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

Abierto Apto para principiantes
#4,306 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python

Línea de trabajo

El error está en src/openai/lib/_response_resources.py, en async_stream(): la comprobación isinstance(input, NotGiven) cerca de la línea 587 y la comprobación isinstance(response_id, Omit) cerca de la línea 650 deberían coincidir con el helper is_given() que ya usa el stream() síncrono. Empieza leyendo el stream() síncrono alrededor de las líneas 412 y 477 para ver el patrón. Está terminado cuando AsyncOpenAI lanza el mismo ValueError que OpenAI cuando se omite input, y un test cubre ambos casos: el caso omit y el caso NOT_GIVEN.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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)

Lenguaje dominante
Python
Estrellas
32.2k
Forks
5.8k
Merge medio
12 h 29 min
PR fusionados (30 d)
112

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de openai/openai-python

Todos los issues de openai/openai-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.