Retry a Runnable if it fails.
RunnableRetry can be used to add retry logic to any object that subclasses the base Runnable.
Such retries are especially useful for network calls that may fail due to transient errors.
The RunnableRetry is implemented as a RunnableBinding. The easiest
way to use it is through the .with_retry() method on all Runnables.
Example: Here's an example that uses a RunnableLambda to raise an exception
import time
def foo(input) -> None:
'''Fake function that raises an exception.'''
raise ValueError(f"Invoking foo failed. At time {time.time()}")
runnable = RunnableLambda(foo)
runnable_with_retries = runnable.with_retry(
retry_if_exception_type=(ValueError,), # Retry only on ValueError
wait_exponential_jitter=True, # Add jitter to the exponential backoff
stop_after_attempt=2, # Try twice
exponential_jitter_params={"initial": 2}, # if desired, customize backoff
)
# The method invocation above is equivalent to the longer form below:
runnable_with_retries = RunnableRetry(
bound=runnable,
retry_exception_types=(ValueError,),
max_attempt_number=2,
wait_exponential_jitter=True,
exponential_jitter_params={"initial": 2},
)
This logic can be used to retry any Runnable, including a chain of Runnables, but in general it's best practice to keep the scope of the retry as small as possible. For example, if you have a chain of Runnables, you should only retry the Runnable that is likely to fail, not the entire chain.
RunnableRetry(
self,
*,
bound: Runnable[Input, Output],
kwargs: Mapping[str, Any] | None = None,
config: RunnableConfig | None = None,
config_factories: list[Callable[[RunnableConfig], RunnableConfig]] | None = None,
custom_input_type: type[Input] | BaseModel | None = None,
custom_output_type: type[Output] | BaseModel | None = None,
**other_kwargs: Any = {}
)Example:
from langchain_core.chat_models import ChatOpenAI
from langchain_core.prompts import PromptTemplate
template = PromptTemplate.from_template("tell me a joke about {topic}.")
model = ChatOpenAI(temperature=0.5)
# Good
chain = template | model.with_retry()
# Bad
chain = template | model
retryable_chain = chain.with_retry()The exception types to retry on. By default all exceptions are retried.
In general you should only retry on exceptions that are likely to be transient, such as network errors.
Good exceptions to retry are all server errors (5xx) and selected client errors (4xx) such as 429 Too Many Requests.
Whether to add jitter to the exponential backoff.
Parameters for tenacity.wait_exponential_jitter. Namely: initial,
max, exp_base, and jitter (all float values).
The maximum number of attempts to retry the Runnable.
The underlying Runnable that this Runnable delegates to.
kwargs to pass to the underlying Runnable when running.
The config to bind to the underlying Runnable.
The config factories to bind to the underlying Runnable.
Override the input type of the underlying Runnable with a custom type.
Override the output type of the underlying Runnable with a custom type.
The name of the Runnable. Used for debugging and tracing.
Input type.
Output Type.
The type of input this Runnable accepts specified as a Pydantic model.
Output schema.
List configurable fields for this Runnable.
Get the name of the Runnable.
Get a Pydantic model that can be used to validate input to the Runnable.
Get a JSON schema that represents the input to the Runnable.
Get a Pydantic model that can be used to validate output to the Runnable.
Get a JSON schema that represents the output of the Runnable.
The type of config this Runnable accepts specified as a Pydantic model.
Get a JSON schema that represents the config of the Runnable.
Return a graph representation of this Runnable.
Return a list of prompts used by this Runnable.
Pipe Runnable objects.
Pick keys from the output dict of this Runnable.
Assigns new fields to the dict output of this Runnable.
Run invoke in parallel on a list of inputs.
Run ainvoke in parallel on a list of inputs.
Default implementation of stream, which calls invoke.
Default implementation of astream, which calls ainvoke.
Stream all output from a Runnable, as reported to the callback system.
Generate a stream of events.
Transform inputs to outputs.
Transform inputs to outputs.
Bind arguments to a Runnable, returning a new Runnable.
Bind config to a Runnable, returning a new Runnable.
Bind lifecycle listeners to a Runnable, returning a new Runnable.
Bind async lifecycle listeners to a Runnable.
Bind input and output types to a Runnable, returning a new Runnable.
Create a new Runnable that retries the original Runnable on exceptions.
Return a new Runnable that maps a list of inputs to a list of outputs.
Add fallbacks to a Runnable, returning a new Runnable.
Create a BaseTool from a Runnable.