Skip to content

AsyncStream missing aclose() causes AttributeError in streaming structured output #2853

@landim

Description

@landim

Bug Report

Summary

AsyncStream exposes close() but not aclose(), causing AttributeError when callers use the standard Python async cleanup convention.

Reproduction

from openai._streaming import AsyncStream

stream = AsyncStream(...)
await stream.aclose()
# AttributeError: 'AsyncStream' object has no attribute 'aclose'. Did you mean: 'close'?

The error surfaces in production when using client.beta.chat.completions.stream() (or any path through AsyncChatCompletionStream) with instrumentation libraries (e.g. Langfuse) that wrap the raw stream. The call chain is:

  1. AsyncChatCompletionStreamManager.__aexit__ (line 290) calls self.__stream.close()
  2. AsyncChatCompletionStream.close() (line 215) calls self._response.aclose()
  3. self._response = raw_stream.response — when instrumentation wraps AsyncStream, .response resolves to the AsyncStream itself rather than the underlying httpx.Response
  4. AsyncStream has close() but not aclose()AttributeError

Full traceback

File ".../openai/lib/streaming/chat/_completions.py", line 290, in __aexit__
    await self.__stream.close()
  File ".../openai/lib/streaming/chat/_completions.py", line 215, in close
    await self._response.aclose()
AttributeError: 'AsyncStream' object has no attribute 'aclose'. Did you mean: 'close'?

Environment

  • openai SDK version: 2.9.0
  • Python 3.12
  • Instrumentation: Langfuse 3.11.2+ (wraps OpenAI streaming responses)
  • Framework: LangChain + langchain-openai (uses with_structured_output() with streaming)

Expected behavior

AsyncStream should expose aclose() as a standard async cleanup method, matching the convention used by httpx.Response, asyncio.StreamWriter, and Python async generators.

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions