Skip to content

The agent engineering platform.

feat(core): accept a callable in with_retry (#41158)

ccurmea055e836fd4f
diff --git a/libs/core/langchain_core/runnables/base.py b/libs/core/langchain_core/runnables/base.py
index 8ba8d3e8da..9e5162f2b4 100644
--- a/libs/core/langchain_core/runnables/base.py
+++ b/libs/core/langchain_core/runnables/base.py
@@ -2102,7 +2102,8 @@ class Runnable(ABC, Generic[Input, Output]):
def with_retry(
self,
*,
- retry_if_exception_type: tuple[type[BaseException], ...] = (Exception,),
+ retry_if_exception_type: tuple[type[BaseException], ...]
+ | Callable[[BaseException], bool] = (Exception,),
wait_exponential_jitter: bool = True,
exponential_jitter_params: ExponentialJitterParams | None = None,
stop_after_attempt: int = 3,
@@ -2110,7 +2111,9 @@ class Runnable(ABC, Generic[Input, Output]):
"""Create a new `Runnable` that retries the original `Runnable` on exceptions.
Args:
- retry_if_exception_type: A tuple of exception types to retry on.
+ retry_if_exception_type: A tuple of exception types to retry on, or a
+ callable that receives the raised exception and returns whether to
+ retry it.
wait_exponential_jitter: Whether to add jitter to the wait
time between retries.
stop_after_attempt: The maximum number of attempts to make before
diff --git a/libs/core/langchain_core/runnables/retry.py b/libs/core/langchain_core/runnables/retry.py
index 5e1e721d6c..ec2257dcee 100644
--- a/libs/core/langchain_core/runnables/retry.py
+++ b/libs/core/langchain_core/runnables/retry.py
@@ -1,5 +1,6 @@
"""`Runnable` that retries a `Runnable` if it fails."""
+from collections.abc import Callable
from typing import (
TYPE_CHECKING,
Any,
@@ -12,6 +13,7 @@ from tenacity import (
RetryCallState,
RetryError,
Retrying,
+ retry_if_exception,
retry_if_exception_type,
stop_after_attempt,
wait_exponential_jitter,
@@ -111,8 +113,11 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
```
"""
- retry_exception_types: tuple[type[BaseException], ...] = (Exception,)
- """The exception types to retry on. By default all exceptions are retried.
+ retry_exception_types: (
+ tuple[type[BaseException], ...] | Callable[[BaseException], bool]
+ ) = (Exception,)
+ """The exception types to retry on, or a callable that receives the raised
+ exception and returns whether to retry it. By default all exceptions are retried.
In general you should only retry on exceptions that are likely to be
transient, such as network errors.
@@ -144,7 +149,9 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
**(self.exponential_jitter_params or {})
)
- if self.retry_exception_types:
+ if callable(self.retry_exception_types):
+ kwargs["retry"] = retry_if_exception(self.retry_exception_types)
+ elif self.retry_exception_types:
kwargs["retry"] = retry_if_exception_type(self.retry_exception_types)
return kwargs
diff --git a/libs/core/tests/unit_tests/runnables/test_runnable.py b/libs/core/tests/unit_tests/runnables/test_runnable.py
index 2a62e88d48..a07455ab79 100644
--- a/libs/core/tests/unit_tests/runnables/test_runnable.py
+++ b/libs/core/tests/unit_tests/runnables/test_runnable.py
@@ -4126,6 +4126,60 @@ async def test_async_retrying(mocker: MockerFixture) -> None:
lambda_mock.reset_mock()
+def _retry_value_errors(exc: BaseException) -> bool:
+ return isinstance(exc, ValueError)
+
+
+def test_retrying_with_predicate(mocker: MockerFixture) -> None:
+ def _lambda(x: int) -> int:
+ if x == 1:
+ msg = "x is 1"
+ raise ValueError(msg)
+ msg = "x is 2"
+ raise RuntimeError(msg)
+
+ lambda_mock = mocker.Mock(side_effect=_lambda)
+ runnable = RunnableLambda(lambda_mock).with_retry(
+ stop_after_attempt=2,
+ wait_exponential_jitter=False,
+ retry_if_exception_type=_retry_value_errors,
+ )
+
+ with pytest.raises(ValueError, match="x is 1"):
+ runnable.invoke(1)
+ assert lambda_mock.call_count == 2 # retried
+ lambda_mock.reset_mock()
+
+ with pytest.raises(RuntimeError, match="x is 2"):
+ runnable.invoke(2)
+ assert lambda_mock.call_count == 1 # did not retry
+
+
+async def test_async_retrying_with_predicate(mocker: MockerFixture) -> None:
+ def _lambda(x: int) -> int:
+ if x == 1:
+ msg = "x is 1"
+ raise ValueError(msg)
+ msg = "x is 2"
+ raise RuntimeError(msg)
+
+ lambda_mock = mocker.Mock(side_effect=_lambda)
+ runnable = RunnableLambda(lambda_mock).with_retry(
+ stop_after_attempt=2,
+ wait_exponential_jitter=False,
+ retry_if_exception_type=_retry_value_errors,
+ )
+
+ with pytest.raises(ValueError, match="x is 1"):
+ await runnable.ainvoke(1)
+ assert lambda_mock.call_count == 2 # retried
+ lambda_mock.reset_mock()
+
+ with pytest.raises(RuntimeError, match="x is 2"):
+ await runnable.ainvoke(2)
+ assert lambda_mock.call_count == 1 # did not retry
+
+
def test_runnable_lambda_stream() -> None:
"""Test that stream works for both normal functions & those returning Runnable."""
# Normal output should work