retry_on as a name raises isinstance's TypeError instead of the documented one and skips every retry
What happens
RetryPolicy(retry_on=...) accepts a name as well as an exception class, but a name is a Sequence, so _should_retry_on spreads it into characters and hands them to isinstance:
# libs/langgraph/langgraph/pregel/_retry.py
if isinstance(retry_policy.retry_on, Sequence):
return isinstance(exc, tuple(retry_policy.retry_on))With retry_on="ValueError" and a node that raises ValueError("upstream service returned 503"):
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
File ".../langgraph/pregel/_retry.py", line 844, in _should_retry_on
return isinstance(exc, tuple(retry_policy.retry_on))The node ran once, not max_attempts times, and the ValueError is nowhere in that traceback: nothing says the retry policy was the problem.
Why it happens
str is a Sequence, so the first branch catches it before the type check that would have reported it. The same gap applies to the elements of a list or tuple:
retry_on |
result |
|---|---|
"ValueError" |
TypeError from isinstance, node called once |
["ValueError"] |
TypeError from isinstance |
[ValueError, "TypeError"] |
silently True for every ValueError |
["TypeError", ValueError] |
TypeError from isinstance |
The third row is the one to watch: isinstance returns as soon as it matches, so a list that mixes a class with a name quietly behaves like the class alone until an exception arrives that does not match first, and only then fails with an unrelated message.
Steps to reproduce
from langgraph.graph import StateGraph
from langgraph.pregel import RetryPolicy
from typing_extensions import TypedDict
class State(TypedDict):
value: str
calls = 0
def flaky(state: State) -> State:
global calls
calls += 1
raise ValueError("upstream service returned 503")
graph = StateGraph(State)
graph.add_node("flaky", flaky)
graph.set_entry_point("flaky")
retry_policy = RetryPolicy(retry_on="ValueError", max_attempts=5, initial_interval=0)
graph.compile().invoke(
{"value": "x"},
config={"configurable": {"retry_policy": retry_policy}},
)The call raises TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union, and calls == 1. Passing the class instead, retry_on=ValueError, retries five times and re-raises the ValueError.
retry_on is documented as "an Exception class, a list or tuple of Exception classes, or a callable", so a name is a configuration mistake rather than a supported spelling, and the repo already has the error for it: tests/test_retry.py::test_should_retry_on_invalid_type asserts TypeError("retry_on must be an Exception class, ..."), but it only covers retry_on=123, which is not a Sequence and therefore reaches that branch. Strings and lists containing strings skip it.
Suggested direction
Validate the sequence elements before calling isinstance, and exclude str/bytes from the sequence branch, so these report the documented error. Two alternatives, if either is preferable:
- validate
RetryPolicyup front the wayTimeoutPolicyalready is (coerce()inlanggraph/types.py, called frompregel/main.py,graph/state.pyandpregel/_read.py), which reports the mistake at configuration time instead of inside the retry loop; - resolve a string to an exception class by name, which would make the name spelling work rather than reject it.
Happy to follow whichever the maintainers prefer, and to open a PR either way.
Environment
langgraph on main (07b33185), Python 3.11, macOS.
Source: langchain-ai/langgraph