Skip to content

Commit d8b7a39

Browse files
committed
Python: fix(core): close ResponseStream iterators on abort and hook errors
The Copilot review on #8773 was right: binding the SDK stream with `async with` only unwinds when the wrapping generator is closed, and nothing closed it when a consumer broke out of the loop or a transform hook raised after a yielded update. ResponseStream is now an async context manager whose __aexit__ closes the stream, and __anext__ closes the active iterator when a map/flat_map transform or a transform hook raises, so the provider stream is released on both paths instead of waiting for GC. Signed-off-by: Yufeng He <[email protected]>
1 parent aa46520 commit d8b7a39

3 files changed

Lines changed: 82 additions & 4 deletions

File tree

‎python/packages/core/agent_framework/_types.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3722,6 +3722,13 @@ async def _get_stream(self) -> AsyncIterable[UpdateT]:
37223722
def __aiter__(self) -> ResponseStream[UpdateT, FinalT]:
37233723
return self
37243724

3725+
async def __aenter__(self) -> ResponseStream[UpdateT, FinalT]:
3726+
return self
3727+
3728+
async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None:
3729+
"""Close the stream on block exit, including an early consumer break."""
3730+
await self.close()
3731+
37253732
def _start_content_pipeline(self) -> None:
37263733
if self._content_pipeline_started:
37273734
return
@@ -4005,9 +4012,9 @@ async def __anext__(self) -> UpdateT:
40054012
await self._finish_consumption()
40064013
raise
40074014
except Exception as exc:
4015+
await self.close()
40084016
await self._handle_stream_error(exc)
40094017
raise
4010-
40114018
async def close(self) -> None:
40124019
"""Close the active iterator and run cleanup hooks.
40134020

‎python/packages/core/tests/core/test_types.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4493,6 +4493,51 @@ async def updates() -> AsyncIterable[ChatResponseUpdate]:
44934493

44944494
assert events == ["iterator", "inner", "outer"]
44954495

4496+
async def test_async_with_break_closes_iterator(self) -> None:
4497+
"""Breaking out of `async with stream:` releases the iterator without a manual close."""
4498+
events: list[str] = []
4499+
4500+
async def updates() -> AsyncIterable[ChatResponseUpdate]:
4501+
try:
4502+
yield ChatResponseUpdate(contents=[Content.from_text("first")], role="assistant")
4503+
yield ChatResponseUpdate(contents=[Content.from_text("second")], role="assistant")
4504+
finally:
4505+
events.append("iterator")
4506+
4507+
stream: ResponseStream[ChatResponseUpdate, Sequence[ChatResponseUpdate]] = ResponseStream(
4508+
updates(), cleanup_hooks=[lambda: events.append("cleanup")]
4509+
)
4510+
4511+
async with stream:
4512+
async for _ in stream:
4513+
break
4514+
4515+
assert events == ["iterator", "cleanup"]
4516+
4517+
async def test_transform_hook_error_closes_iterator(self) -> None:
4518+
"""A hook that fails after a yielded update releases the suspended iterator."""
4519+
events: list[str] = []
4520+
4521+
async def updates() -> AsyncIterable[ChatResponseUpdate]:
4522+
try:
4523+
yield ChatResponseUpdate(contents=[Content.from_text("first")], role="assistant")
4524+
yield ChatResponseUpdate(contents=[Content.from_text("second")], role="assistant")
4525+
finally:
4526+
events.append("iterator")
4527+
4528+
def failing_hook(update: ChatResponseUpdate) -> ChatResponseUpdate:
4529+
raise RuntimeError("hook blew up")
4530+
4531+
stream: ResponseStream[ChatResponseUpdate, Sequence[ChatResponseUpdate]] = ResponseStream(
4532+
updates(), transform_hooks=[failing_hook] # ty: ignore[invalid-argument-type]
4533+
)
4534+
4535+
with pytest.raises(RuntimeError, match="hook blew up"):
4536+
async for _ in stream:
4537+
pass
4538+
4539+
assert events == ["iterator"]
4540+
44964541
async def test_cleanup_hook_called_after_iteration(self) -> None:
44974542
"""Cleanup hook is called after iteration completes."""
44984543
cleanup_called = {"value": False}

‎python/packages/openai/tests/openai/test_openai_chat_completion_client.py‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,9 +2215,35 @@ async def create(**kwargs: Any) -> Any:
22152215
with patch.object(client.client.chat.completions, "create", side_effect=create):
22162216
stream = client._inner_get_response(messages=[Message(role="user", contents=["test"])], stream=True, options={})
22172217
assert isinstance(stream, ResponseStream)
2218-
async for _ in stream:
2219-
break
2220-
await stream.close()
2218+
# No manual close(): the async-with protocol is what an early break
2219+
# relies on to release the provider stream.
2220+
async with stream:
2221+
async for _ in stream:
2222+
break
2223+
2224+
assert sdk_stream.closed
2225+
2226+
2227+
async def test_streaming_closes_provider_stream_when_transform_hook_raises(
2228+
openai_unit_test_env: dict[str, str],
2229+
) -> None:
2230+
"""A hook failing after a yielded update must also close the SDK stream (#8762)."""
2231+
client = OpenAIChatCompletionClient()
2232+
sdk_stream = _FakeAsyncStream([_make_content_chunk("hello"), _make_content_chunk("world")])
2233+
2234+
async def create(**kwargs: Any) -> Any:
2235+
return sdk_stream
2236+
2237+
def failing_hook(update: Any) -> Any:
2238+
raise RuntimeError("hook blew up")
2239+
2240+
with patch.object(client.client.chat.completions, "create", side_effect=create):
2241+
stream = client._inner_get_response(messages=[Message(role="user", contents=["test"])], stream=True, options={})
2242+
assert isinstance(stream, ResponseStream)
2243+
stream._transform_hooks.append(failing_hook)
2244+
with pytest.raises(RuntimeError, match="hook blew up"):
2245+
async for _ in stream:
2246+
pass
22212247

22222248
assert sdk_stream.closed
22232249

0 commit comments

Comments
 (0)