Skip to content

Commit 33439bf

Browse files
authored
Python: Pass stream errors to nested streams when closing them (#9476)
* Python: Pass stream errors to nested streams when closing them When a ResponseStream failed or was cancelled, close() shut nested ResponseStreams without the error, so their cleanup hooks treated the run as successful. With agent middleware this made the agent telemetry stream call get_final_response(), which ran every ContextProvider.after_run and saved the partial turn to history. close() now takes the terminal error and records it on itself and on nested streams while their cleanup hooks run. * Python: Pass stream errors to nested streams in buffered mode too A transform failure or cancellation in a buffered outer stream went through _abort_buffered_materialization, which closed the stream without the error, so the nested stream still ran its cleanup as a normal completion. It now passes the recorded error to close(), and the regression test gains a buffered variant.
1 parent d0c3e6a commit 33439bf

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4594,7 +4594,7 @@ async def _abort_buffered_materialization(self, exc: BaseException) -> None:
45944594
self._stream_error = exc
45954595
try:
45964596
# close() also runs the hooks if releasing the iterator fails.
4597-
await self.close()
4597+
await self.close(exc)
45984598
finally:
45994599
self._stream_error = None
46004600

@@ -4780,22 +4780,25 @@ async def __anext__(self) -> UpdateT:
47804780
logger.warning("Stream cleanup hook failed while handling an existing exception.", exc_info=True)
47814781
finally:
47824782
try:
4783-
await self.close()
4783+
await self.close(exc)
47844784
except Exception:
47854785
logger.warning("Stream cleanup failed while handling an existing exception.", exc_info=True)
47864786
raise
47874787

4788-
async def close(self) -> None:
4788+
async def close(self, exc: BaseException | None = None) -> None:
47894789
"""Close the active iterator and run cleanup hooks.
47904790
47914791
This method is idempotent and also closes nested ``ResponseStream`` wrappers.
4792+
4793+
Args:
4794+
exc: The error that ended the stream, recorded on this and nested streams while cleanup hooks run.
47924795
"""
47934796
iterator_failed = False
47944797
try:
47954798
iterator: AsyncIterator[UpdateT] | None = self._iterator
47964799
if iterator is not None:
47974800
if isinstance(iterator, ResponseStream):
4798-
await cast(ResponseStream[UpdateT, Any], iterator).close()
4801+
await cast(ResponseStream[UpdateT, Any], iterator).close(exc)
47994802
else:
48004803
close = getattr(iterator, "aclose", None)
48014804
if close is not None:
@@ -4806,7 +4809,10 @@ async def close(self) -> None:
48064809
finally:
48074810
self._consumed = True
48084811
try:
4809-
await self._run_cleanup_hooks()
4812+
if exc is None:
4813+
await self._run_cleanup_hooks()
4814+
else:
4815+
await self._handle_stream_error(exc)
48104816
except Exception:
48114817
if not iterator_failed:
48124818
raise

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@
1818
from pytest import fixture, mark, raises
1919

2020
from agent_framework import (
21+
Agent,
2122
AgentResponse,
2223
AgentResponseUpdate,
2324
Annotation,
25+
BaseChatClient,
2426
ChatOptions,
2527
ChatResponse,
2628
ChatResponseUpdate,
2729
ComputerSafetyCheck,
2830
Content,
31+
ContextProvider,
32+
FunctionInvocationLayer,
2933
FunctionTool,
34+
InMemoryHistoryProvider,
3035
Message,
3136
ResponseStream,
3237
TextSpanRegion,
3338
ToolMode,
3439
UsageDetails,
40+
agent_middleware,
3541
detect_media_type_from_base64,
3642
get_namespaced_value,
3743
merge_additional_properties,
@@ -5616,6 +5622,51 @@ async def cancelling_transform(update: ChatResponseUpdate) -> ChatResponseUpdate
56165622

56175623
assert events == ["iterator"]
56185624

5625+
@pytest.mark.parametrize("buffered", [False, True])
5626+
@pytest.mark.parametrize("failure", [RuntimeError("hook failed"), asyncio.CancelledError()])
5627+
async def test_outer_stream_error_skips_after_run_with_agent_middleware(
5628+
self, span_exporter: Any, failure: BaseException, buffered: bool
5629+
) -> None:
5630+
"""An error in a middleware-wrapped agent stream reaches the inner stream, so after_run does not fire."""
5631+
5632+
class StreamingClient(FunctionInvocationLayer, BaseChatClient):
5633+
def _inner_get_response(self, *, messages: Any, stream: bool, options: Any, **kwargs: Any) -> Any:
5634+
async def updates() -> AsyncIterable[ChatResponseUpdate]:
5635+
for i in range(3):
5636+
yield ChatResponseUpdate(contents=[Content.from_text(f"part{i} ")], role="assistant")
5637+
5638+
return ResponseStream(updates(), finalizer=ChatResponse.from_updates)
5639+
5640+
class RecordingProvider(ContextProvider):
5641+
def __init__(self) -> None:
5642+
super().__init__("recorder")
5643+
self.after_run_calls = 0
5644+
5645+
async def after_run(self, *, agent: Any, session: Any, context: Any, state: Any) -> None:
5646+
self.after_run_calls += 1
5647+
5648+
@agent_middleware
5649+
async def passthrough(context: Any, call_next: Any) -> None:
5650+
await call_next()
5651+
5652+
def failing_hook(update: AgentResponseUpdate) -> AgentResponseUpdate:
5653+
raise failure
5654+
5655+
recorder = RecordingProvider()
5656+
history = InMemoryHistoryProvider()
5657+
agent = Agent(client=StreamingClient(), context_providers=[history, recorder], middleware=[passthrough])
5658+
session = agent.create_session()
5659+
5660+
stream = agent.run("hi", stream=True, session=session).with_transform_hook(failing_hook)
5661+
if buffered:
5662+
stream = stream.buffer_updates()
5663+
with pytest.raises(type(failure)):
5664+
async for _ in stream:
5665+
pass
5666+
5667+
assert recorder.after_run_calls == 0
5668+
assert await history.get_messages(session.session_id, state=session.state.get(history.source_id, {})) == []
5669+
56195670
async def test_cleanup_hook_called_after_iteration(self) -> None:
56205671
"""Cleanup hook is called after iteration completes."""
56215672
cleanup_called = {"value": False}

0 commit comments

Comments
 (0)