diff --git a/libs/langgraph/langgraph/pregel/retry.py b/libs/langgraph/langgraph/pregel/retry.py index cdf7b33d3..34374962a 100644 --- a/libs/langgraph/langgraph/pregel/retry.py +++ b/libs/langgraph/langgraph/pregel/retry.py @@ -38,11 +38,19 @@ def run_with_retry( # increment attempts attempts += 1 # check if we should retry - if callable(retry_policy.retry_on): + if isinstance(retry_policy.retry_on, (list, tuple)): + if not isinstance(exc, tuple(retry_policy.retry_on)): + raise + elif isinstance(retry_policy.retry_on, type) and issubclass(retry_policy.retry_on, Exception): + if not isinstance(exc, retry_policy.retry_on): + raise + elif callable(retry_policy.retry_on): if not retry_policy.retry_on(exc): raise - elif not isinstance(exc, retry_policy.retry_on): - raise + else: + raise TypeError( + "retry_on must be an Exception class, a list or tuple of Exception classes, or a callable" + ) # check if we should give up if attempts >= retry_policy.max_attempts: raise @@ -94,11 +102,19 @@ async def arun_with_retry( # increment attempts attempts += 1 # check if we should retry - if callable(retry_policy.retry_on): + if isinstance(retry_policy.retry_on, (list, tuple)): + if not isinstance(exc, tuple(retry_policy.retry_on)): + raise + elif isinstance(retry_policy.retry_on, type) and issubclass(retry_policy.retry_on, Exception): + if not isinstance(exc, retry_policy.retry_on): + raise + elif callable(retry_policy.retry_on): if not retry_policy.retry_on(exc): raise - elif not isinstance(exc, retry_policy.retry_on): - raise + else: + raise TypeError( + "retry_on must be an Exception class, a list or tuple of Exception classes, or a callable" + ) # check if we should give up if attempts >= retry_policy.max_attempts: raise