@@ -1323,6 +1323,12 @@ async def _finalize_stream() -> None:
13231323 from ._types import ChatResponse
13241324
13251325 try :
1326+ if result_stream ._stream_error is not None : # pyright: ignore[reportPrivateUsage]
1327+ # Stream errored; skip get_final_response() to avoid firing
1328+ # result hooks such as after_run context providers on error
1329+ # paths. Capture the error on the span before returning.
1330+ capture_exception (span = span , exception = result_stream ._stream_error , timestamp = time_ns ()) # pyright: ignore[reportPrivateUsage]
1331+ return
13261332 response : ChatResponse [Any ] = await result_stream .get_final_response ()
13271333 duration = duration_state .get ("duration" )
13281334 response_attributes = _get_response_attributes (attributes , response )
@@ -1579,6 +1585,12 @@ async def _finalize_stream() -> None:
15791585 from ._types import AgentResponse
15801586
15811587 try :
1588+ if result_stream ._stream_error is not None : # pyright: ignore[reportPrivateUsage]
1589+ # Stream errored; skip get_final_response() to avoid firing
1590+ # result hooks such as after_run context providers on error
1591+ # paths. Capture the error on the span before returning.
1592+ capture_exception (span = span , exception = result_stream ._stream_error , timestamp = time_ns ()) # pyright: ignore[reportPrivateUsage]
1593+ return
15821594 response : AgentResponse [Any ] = await result_stream .get_final_response ()
15831595 duration = duration_state .get ("duration" )
15841596 response_attributes = _get_response_attributes (
0 commit comments