RunnableRetry Class — langchain Architecture
Architecture documentation for the RunnableRetry class in retry.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD dcb89960_9531_c0ae_7764_c192a29f52c0["RunnableRetry"] 4b1ae7cc_f0e0_3b9c_39b8_cde719e2067d["retry.py"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|defined in| 4b1ae7cc_f0e0_3b9c_39b8_cde719e2067d 96fac358_fc30_b0a1_ef92_49921799d111["_kwargs_retrying()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 96fac358_fc30_b0a1_ef92_49921799d111 19d453a1_fd58_2e89_f266_f99b0f5a741e["_sync_retrying()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 19d453a1_fd58_2e89_f266_f99b0f5a741e a05cbbdd_13ef_9d8b_5022_8f9b0a46c86a["_async_retrying()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| a05cbbdd_13ef_9d8b_5022_8f9b0a46c86a 9efc1d1c_fe0c_3ced_4e9c_39e7311c06af["_patch_config()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 9efc1d1c_fe0c_3ced_4e9c_39e7311c06af 790f2192_2e50_38d8_2e94_a32c9487f5b6["_patch_config_list()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 790f2192_2e50_38d8_2e94_a32c9487f5b6 7b74a65c_4658_df66_f42d_f8d8eb8c5f5d["_invoke()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 7b74a65c_4658_df66_f42d_f8d8eb8c5f5d fa11af91_03d5_9d9a_937f_0cfec291cba1["invoke()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| fa11af91_03d5_9d9a_937f_0cfec291cba1 2547348e_7cd5_195f_d5d3_cbe68344929a["_ainvoke()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 2547348e_7cd5_195f_d5d3_cbe68344929a 8602ffdb_2ded_ea4a_3459_0b066c02e2bf["ainvoke()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 8602ffdb_2ded_ea4a_3459_0b066c02e2bf 52a44127_1c98_6129_693b_200d35f8fd7b["_batch()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 52a44127_1c98_6129_693b_200d35f8fd7b 7a99fa12_967f_04c9_9db3_b8ff6c02e8a1["batch()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 7a99fa12_967f_04c9_9db3_b8ff6c02e8a1 d376a50b_f292_726b_5406_bcd22f556419["_abatch()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| d376a50b_f292_726b_5406_bcd22f556419 7dea3d8c_e973_7709_8b13_2e21005cfcf4["abatch()"] dcb89960_9531_c0ae_7764_c192a29f52c0 -->|method| 7dea3d8c_e973_7709_8b13_2e21005cfcf4
Relationship Graph
Source Code
libs/core/langchain_core/runnables/retry.py lines 48–379
class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-redef]
"""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
```python
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.
Example:
```python
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()
```
"""
retry_exception_types: tuple[type[BaseException], ...] = (Exception,)
"""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.
"""
wait_exponential_jitter: bool = True
"""Whether to add jitter to the exponential backoff."""
exponential_jitter_params: ExponentialJitterParams | None = None
"""Parameters for `tenacity.wait_exponential_jitter`. Namely: `initial`,
Defined In
Source
Frequently Asked Questions
What is the RunnableRetry class?
RunnableRetry is a class in the langchain codebase, defined in libs/core/langchain_core/runnables/retry.py.
Where is RunnableRetry defined?
RunnableRetry is defined in libs/core/langchain_core/runnables/retry.py at line 48.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free