Conversation
- Add aclose() as an alias for close() in AsyncStream - Supports standard async resource cleanup protocol used by asyncio, contextlib.aclosing(), httpx, anyio, and instrumentation - Brings AsyncStream to parity with callers that rely on conventional aclose() name Fixes openai#2982
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 38d96b6377
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| async def aclose(self) -> None: | ||
| """ | ||
| Async close method - alias for close() to support standard async cleanup protocol. | ||
|
|
||
| Used by asyncio, contextlib.aclosing(), httpx, anyio, and instrumentation libraries. |
There was a problem hiding this comment.
Add
aclose() to the helper async stream wrappers
This only makes the low-level AsyncStream compatible with contextlib.aclosing() / tracers that await obj.aclose(). The public helper entry points still hand callers wrapper objects with close() but no aclose()—AsyncChatCompletionStream (src/openai/lib/streaming/chat/_completions.py:208), AsyncResponseStream (src/openai/lib/streaming/responses/_responses.py:172), and AsyncAssistantEventHandler (src/openai/lib/streaming/_assistants.py:505). Since client.chat.completions.stream(...) and client.responses.stream(...) return those wrappers via their managers, code that switches to contextlib.aclosing(stream) or instrumentation that unconditionally calls await stream.aclose() will still fail with AttributeError on those higher-level streaming APIs.
Useful? React with 👍 / 👎.
Summary
aclose()as an alias forclose()inAsyncStreamasyncio,contextlib.aclosing(),httpx,anyio, and instrumentation/tracing librariesAsyncStreamto parity with callers that rely on conventionalaclose()nameChanges
aclose()method toAsyncStreamclass insrc/openai/_streaming.pyTest plan
AsyncStreamcan be used withcontextlib.aclosing(stream)in async contextsawait stream.aclose()closes the underlying responseFixes #2982
🤖 Generated with OpenClaw