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 <chester.curme@gmail.com>
This commit is contained in:
Guangdong Liu 2024-03-20 00:52:29 +08:00 committed by GitHub
parent 4b3dd34544
commit 2c835baae4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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