From 2c835baae4c638484f343bf746ee48233d0471dd Mon Sep 17 00:00:00 2001 From: Guangdong Liu Date: Wed, 20 Mar 2024 00:52:29 +0800 Subject: [PATCH] code[patch]: Add in code documentation to core Runnable with_retry method (docs only) (#19192) - **Description:** Add in code documentation to core Runnable with_retry method (docs only) - **Issue:** #18804 @baskaryan @eyurtsev PTAL --------- Co-authored-by: ccurme --- libs/core/langchain_core/runnables/base.py | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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