Skip to content

fix: add aclose() alias to AsyncStream#2984

Open
Jah-yee wants to merge 1 commit intoopenai:mainfrom
Jah-yee:fix/asyncstream-aclose-alias
Open

fix: add aclose() alias to AsyncStream#2984
Jah-yee wants to merge 1 commit intoopenai:mainfrom
Jah-yee:fix/asyncstream-aclose-alias

Conversation

@Jah-yee
Copy link

@Jah-yee Jah-yee commented Mar 18, 2026

Summary

  • Adds aclose() as an alias for close() in AsyncStream
  • Supports standard async resource cleanup protocol used by asyncio, contextlib.aclosing(), httpx, anyio, and instrumentation/tracing libraries
  • Brings AsyncStream to parity with callers that rely on conventional aclose() name

Changes

  • Added aclose() method to AsyncStream class in src/openai/_streaming.py

Test plan

  • Verify AsyncStream can be used with contextlib.aclosing(stream) in async contexts
  • Verify await stream.aclose() closes the underlying response
  • Existing tests should continue to pass

Fixes #2982


🤖 Generated with OpenClaw

- 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
@Jah-yee Jah-yee requested a review from a team as a code owner March 18, 2026 06:51
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +241 to +245
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant