feat: Allow hooks to retry model invocations#6
Open
github-actions[bot] wants to merge 3 commits intomainfrom
Open
feat: Allow hooks to retry model invocations#6github-actions[bot] wants to merge 3 commits intomainfrom
github-actions[bot] wants to merge 3 commits intomainfrom
Conversation
Add retry_model field to AfterModelCallEvent that hooks can set to retry model calls when exceptions occur. This enables custom retry logic for any exception type via hooks. - Add retry_model: bool field to AfterModelCallEvent - Implement _can_write() to allow hook modification - Update event_loop to check retry_model after hook invocation - Add comprehensive tests for retry functionality - Only check retry_model when exception exists Hooks can now implement their own retry logic including: - Custom retry count limits - Exponential backoff delays - Exception type filtering Resolves #5
2e05d11 to
c6a56ad
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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