feat: Allow hooks to retry model invocations #6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Implements #5 - Allows hooks to retry model invocations by adding a
retry_modelfield toAfterModelCallEvent.Why This Change?
Users need flexibility to retry model calls on arbitrary exceptions (e.g., ServiceUnavailableException with 503 errors). The existing retry mechanism only handles
ModelThrottledException. This change enables hooks to implement custom retry logic without hardcoding specific exception types into the framework.What Changed?
Core Changes
AfterModelCallEventEnhancement (src/strands/hooks/events.py)retry_model: bool = Falsefield_can_write()method to allow hook modificationEvent Loop Integration (
src/strands/event_loop/event_loop.py)_handle_model_execution()to checkretry_modelafter hook invocationretry_model=Trueand exception exists, continues retry loopComprehensive Tests (
tests/strands/agent/hooks/test_agent_events.py)Public API Changes
New Field:
AfterModelCallEvent.retry_modelDesign Decisions
retry_modelis ignored when no exception exists, preventing retry on successful callsModelThrottledExceptionretry without interferenceAfterModelCallEvent's existing reverse callback pattern - first registered hook's value winsTesting
All unit tests pass (1627/1627):
Resolves
Resolves #5