feat: allow hooks to retry model invocations on exceptions#10
Open
github-actions[bot] wants to merge 6 commits intomainfrom
Open
feat: allow hooks to retry model invocations on exceptions#10github-actions[bot] wants to merge 6 commits intomainfrom
github-actions[bot] wants to merge 6 commits intomainfrom
Conversation
Add retry_model field to AfterModelCallEvent that enables hook providers to retry model invocations when exceptions occur. This provides flexibility for users to implement custom retry logic for any exception type. - Add retry_model: bool field to AfterModelCallEvent (default False) - Implement _can_write() to allow hooks to modify retry_model - Update _handle_model_execution() to check retry_model and retry when set - Hook retries integrate with existing ModelThrottledException retry logic - Hook retries respect MAX_ATTEMPTS limit - Add comprehensive test coverage for retry scenarios Resolves #9
7 tasks
zastrowm
reviewed
Dec 30, 2025
Owner
|
/strands |
Hooks should be able to retry without framework-enforced limits. Removed the attempt count check that artificially limited hook retries. Hooks now control their own retry logic within the loop iterations.
zastrowm
requested changes
Dec 31, 2025
src/strands/event_loop/event_loop.py
Outdated
| await agent.hooks.invoke_callbacks_async(after_model_call_event) | ||
|
|
||
| # Check if hooks want to retry the model call | ||
| if after_model_call_event.retry_model and after_model_call_event.exception: |
Owner
There was a problem hiding this comment.
Go through the exercise of adding support for retrying the the model call even when there aren't exceptions. Add appropriate plumbing and test cases for that scenario
Author
There was a problem hiding this comment.
Added support for retrying on successful calls with test demonstrating content-based retry.
Owner
|
/strands implement |
Extend retry_model functionality to work on both successful and failed model calls, enabling hooks to retry based on response content validation or other custom criteria. - Remove exception requirement from retry_model check - Add retry check on successful model calls - Add test_hook_retry_on_successful_call demonstrating content-based retry - Update test_hook_retry_ignored_without_exception -> test_hook_retry_not_set_on_success - Update AfterModelCallEvent docstring to reflect retry on success
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.
Motivation
Users need the ability to retry model calls on arbitrary exceptions beyond just
ModelThrottledException, and also retry based on response validation. Issue strands-agents#370 requests retrying on ServiceUnavailableException (503 errors), and this feature enables that and more by letting hooks implement custom retry logic for both exceptions and successful responses.Resolves #9
Public API Changes
New Field:
AfterModelCallEvent.retry_modelHook providers can now set
retry_model=Trueto retry model invocations on both exceptions and successful calls:The
retry_modelfield is writable within hook callbacks and defaults toFalse. It can be set for both successful calls (to validate response content) and failed calls (to retry exceptions).Use Cases
Implementation Notes
Hook Retry Integration
Hook-initiated retries work alongside the existing
ModelThrottledExceptionretry mechanism:retry_model=True, the retry loop continuesModelThrottledExceptionas beforeReverse Callback Ordering
AfterModelCallEventuses reverse callback ordering (cleanup pattern). When multiple hooks modifyretry_model, the first-registered hook's value wins because it's called last.No Framework-Enforced Limits
The framework doesn't enforce retry count limits or delays for hook-initiated retries - hooks manage their own state and logic. This provides maximum flexibility while keeping the framework simple.