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:
AsyncChatCompletionStreamManager.__aexit__ (line 290) calls self.__stream.close()
AsyncChatCompletionStream.close() (line 215) calls self._response.aclose()
self._response = raw_stream.response — when instrumentation wraps AsyncStream, .response resolves to the AsyncStream itself rather than the underlying httpx.Response
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