From 04970ca729fa5eb939ee2fb7cec275f5f17eebe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=B0=E6=9C=9B?= <2304238405@qq.com> Date: Fri, 20 Mar 2026 14:26:16 +0800 Subject: [PATCH] Add aclose alias to AsyncStream --- src/openai/_streaming.py | 4 ++++ tests/test_streaming.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/openai/_streaming.py b/src/openai/_streaming.py index 45c13cc11d..d2fe9d481b 100644 --- a/src/openai/_streaming.py +++ b/src/openai/_streaming.py @@ -238,6 +238,10 @@ async def close(self) -> None: """ await self.response.aclose() + async def aclose(self) -> None: + """Alias for `close()` for compatibility with async context consumers.""" + await self.close() + class ServerSentEvent: def __init__( diff --git a/tests/test_streaming.py b/tests/test_streaming.py index 04f8e51abd..8ce58e2892 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -246,3 +246,26 @@ def make_event_iterator( return AsyncStream( cast_to=object, client=async_client, response=httpx.Response(200, content=to_aiter(content)) )._iter_events() + + +@pytest.mark.asyncio +async def test_async_stream_has_aclose_alias(async_client: AsyncOpenAI) -> None: + stream = AsyncStream( + cast_to=object, + client=async_client, + response=httpx.Response(200, content=to_aiter(iter(()))), + ) + + assert hasattr(stream, "aclose") + assert callable(stream.aclose) + await stream.aclose() + + +@pytest.mark.asyncio +async def test_async_stream_aclose_closes_response(async_client: AsyncOpenAI) -> None: + response = httpx.Response(200, content=to_aiter(iter(()))) + stream = AsyncStream(cast_to=object, client=async_client, response=response) + + await stream.aclose() + + assert response.is_closed