-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Open
Description
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) callsself.__stream.close()AsyncChatCompletionStream.close()(line 215) callsself._response.aclose()self._response = raw_stream.response— when instrumentation wrapsAsyncStream,.responseresolves to theAsyncStreamitself rather than the underlyinghttpx.ResponseAsyncStreamhasclose()but notaclose()→ 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
- The sync
Streamclass is not affected since its callers useclose(). aclose()is the standard Python async cleanup convention — async generators,asyncio.StreamWriter,httpx.AsyncByteStream, etc. all expose it.- This is also referenced in langfuse/langfuse#5685 as the root cause.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels