feat(langchain): add a modelRetryMiddleware #9447
Open
+793
−0
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.
This PR introduces
modelRetryMiddleware, a new agent middleware that provides granular retry control for model calls within agents. This middleware leverages the extendedRunnableRetryfunctionality to offer production-ready retry capabilities with configurable backoff, exception filtering, and failure handling strategies.Motivation
Previously, applying retry logic to models used in
createAgentwas challenging:.withRetry()directly on models changed their type, causing incompatibility withcreateAgentThis middleware solves these issues by providing retry logic at the middleware level, allowing fine-grained control without type incompatibilities.
Features
Retry Configuration Options
maxRetries: Maximum number of retry attempts after the initial call (default: 2)retryOn: Filter which exceptions trigger retries (function or array of error constructors)onFailure: Control failure behavior:"raise"(default): Re-raise the exception"return_message": Return anAIMessagewith error detailsbackoffFactor: Exponential backoff multiplier (default: 2.0)initialDelayMs: Initial delay before first retry (default: 1000ms)maxDelayMs: Maximum delay cap (default: 60000ms)jitter: Enable random jitter to avoid thundering herd (default: true)Implementation Details
RunnableRetry: Leverages the extended retry logic fromRunnableRetry.withRetry()by applying retry options directly to the model instance (see feat(core): extended retry logic for RunnableRetry #9446)wrapModelCallhookcreate_agentwithout type incompatibilities