@@ -230,18 +230,15 @@ async def event_loop_cycle(
230230
231231 tracer .end_event_loop_cycle_span (cycle_span , message )
232232 yield EventLoopStopEvent (stop_reason , message , agent .event_loop_metrics , invocation_state ["request_state" ])
233- except StructuredOutputException as e :
233+ except (
234+ StructuredOutputException ,
235+ EventLoopException ,
236+ ContextWindowOverflowException ,
237+ MaxTokensReachedException ,
238+ ) as e :
239+ # Known exceptions bubble up directly rather than getting wrapped in an EventLoopException
234240 tracer .end_span_with_error (cycle_span , str (e ), e )
235241 raise
236- except EventLoopException as e :
237- tracer .end_span_with_error (cycle_span , str (e ), e )
238- # Don't yield or log the exception - we already did it when we
239- # raised the exception and we don't need that duplication.
240- raise
241- except (ContextWindowOverflowException , MaxTokensReachedException ) as e :
242- # Special cased exceptions which we want to bubble up rather than get wrapped in an EventLoopException
243- tracer .end_span_with_error (cycle_span , str (e ), e )
244- raise e
245242 except Exception as e :
246243 tracer .end_span_with_error (cycle_span , str (e ), e )
247244 # Handle any other exceptions
@@ -363,11 +360,8 @@ async def _handle_model_execution(
363360 model_state = agent ._model_state ,
364361 cancel_signal = agent ._cancel_signal ,
365362 )
366- try :
367- async for event in streamed_events :
368- yield event
369- finally :
370- await streamed_events .aclose ()
363+ async for event in streamed_events :
364+ yield event
371365
372366 stop_reason , message , usage , metrics = event ["stop" ]
373367 invocation_state .setdefault ("request_state" , {})
0 commit comments