openai/openai-python · error · MissingStreamClassError

Missing stream class

Error message

Missing stream class

What it means

Raised when a response is expected to be streamed but the client has no default stream class configured. The SDK needs a Stream/AsyncStream class to wrap the response, and it looks it up via client._default_stream_class; if that attribute is None (typically because the client was constructed in an unusual or partially-initialized way, or a custom client subclass did not set a stream class), parsing a streaming response cannot proceed.

Source

Thrown at src/openai/_legacy_response.py:242

                        client=cast(Any, self._client),
                        options=self._options,
                    ),
                )

            if self._stream_cls:
                return cast(
                    R,
                    self._stream_cls(
                        cast_to=extract_stream_chunk_type(self._stream_cls),
                        response=self.http_response,
                        client=cast(Any, self._client),
                        options=self._options,
                    ),
                )

            stream_cls = cast("type[Stream[Any]] | type[AsyncStream[Any]] | None", self._client._default_stream_cls)
            if stream_cls is None:
                raise MissingStreamClassError()

            return cast(
                R,
                stream_cls(
                    cast_to=cast_to,
                    response=self.http_response,
                    client=cast(Any, self._client),
                    options=self._options,
                ),
            )

        if cast_to is NoneType:
            return cast(R, None)

        response = self.http_response
        if cast_to == str:
            return cast(R, response.text)

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Construct the client normally with OpenAI() / AsyncOpenAI() instead of a custom subclass or mock
  2. If subclassing, ensure super().__init__() is called so _default_stream_class is set
  3. Don't patch client internals; use recorded responses or a mock transport instead

Example fix

// before
class MyClient(OpenAI):
    def __init__(self):
        self.api_key = '...'
# after
class MyClient(OpenAI):
    def __init__(self):
        super().__init__()
Defensive patterns

Strategy: validation

Validate before calling

from openai import OpenAI
client = OpenAI()
assert getattr(client, '_default_stream_class', None) is not None, 'client misconfigured; call super().__init__() in subclasses'

Prevention

When it happens

Trigger: Calling client.post(..., stream=True) (or any streaming endpoint) with cast_to that requires a stream wrapper when _default_stream_class is None; constructing an OpenAI/AsyncOpenAI instance via a custom subclass or mock that never assigns the stream class.

Common situations: Mocking or patching the OpenAI client in tests so internals are missing; using a forked/custom HTTP client subclass that overrides __init__ and skips stream class setup.

Related errors


AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28). Data as JSON: /api/errors/98786342ddb15911. Report an issue: GitHub.