diff --git a/libs/core/langchain_core/runnables/base.py b/libs/core/langchain_core/runnables/base.py index 88d71407ca4..8b265d7f33c 100644 --- a/libs/core/langchain_core/runnables/base.py +++ b/libs/core/langchain_core/runnables/base.py @@ -1344,6 +1344,35 @@ class Runnable(Generic[Input, Output], ABC): ) -> Runnable[Input, Output]: """Create a new Runnable that retries the original runnable on exceptions. + Example: + + .. code-block:: python + from langchain_core.runnables import RunnableLambda + + count = 0 + + + def _lambda(x: int) -> None: + global count + count = count + 1 + if x == 1: + raise ValueError("x is 1") + else: + pass + + + runnable = RunnableLambda(_lambda) + try: + runnable.with_retry( + stop_after_attempt=2, + retry_if_exception_type=(ValueError,), + ).invoke(1) + except ValueError: + pass + + assert (count == 2) + + Args: retry_if_exception_type: A tuple of exception types to retry on wait_exponential_jitter: Whether to add jitter to the wait time