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