Skip to content

Commit 29eaae5

Browse files
committed
Simplify exception handling
1 parent 1b7b6f2 commit 29eaae5

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/strands/event_loop/event_loop.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ async def event_loop_cycle(
163163

164164
stop_reason, message, *_ = model_event["stop"]
165165
yield ModelMessageEvent(message=message)
166-
except Exception as e:
167-
tracer.end_span_with_error(cycle_span, str(e), e)
168-
raise
169166
except BaseException as e:
170167
tracer.end_span_with_error(cycle_span, str(e), e)
171168
raise
@@ -230,18 +227,16 @@ async def event_loop_cycle(
230227

231228
tracer.end_event_loop_cycle_span(cycle_span, message)
232229
yield EventLoopStopEvent(stop_reason, message, agent.event_loop_metrics, invocation_state["request_state"])
233-
except StructuredOutputException as e:
230+
except (
231+
StructuredOutputException,
232+
EventLoopException,
233+
ContextWindowOverflowException,
234+
MaxTokensReachedException,
235+
) as e:
236+
# These exceptions bubble up directly without wrapping in EventLoopException.
237+
# EventLoopException is not yielded or logged to avoid duplication.
234238
tracer.end_span_with_error(cycle_span, str(e), e)
235239
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
245240
except Exception as e:
246241
tracer.end_span_with_error(cycle_span, str(e), e)
247242
# Handle any other exceptions

0 commit comments

Comments
 (0)