Skip to content

The agent engineering platform.

fix(langchain): re-raise non-retryable exceptions in ModelRetryMiddleware (#38960)

Yiğit ERDOĞANe92c6db3b300
diff --git a/libs/langchain_v1/langchain/agents/middleware/model_retry.py b/libs/langchain_v1/langchain/agents/middleware/model_retry.py
index 2b4a39e725..710de470b9 100644
--- a/libs/langchain_v1/langchain/agents/middleware/model_retry.py
+++ b/libs/langchain_v1/langchain/agents/middleware/model_retry.py
@@ -59,12 +59,15 @@ class ModelRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, Resp
!!! example "Custom exception filtering"
+ Exceptions for which the callable returns `False` propagate to the caller.
+ They are not passed to `on_failure`.
+
```python
from anthropic import APIStatusError
def should_retry(exc: Exception) -> bool:
- # Only retry on 5xx errors
+ # Retry 5xx; anything else propagates
if isinstance(exc, APIStatusError):
return 500 <= exc.status_code < 600
return False
@@ -129,7 +132,8 @@ class ModelRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, Resp
retry_on: Either a tuple of exception types to retry on, or a callable
that takes an exception and returns `True` if it should be retried.
- Default is to retry on all exceptions.
+ Default is to retry on all exceptions. Exceptions that do not match
+ propagate immediately and are not handled by `on_failure`.
on_failure: Behavior when all retries are exhausted.
Options:
@@ -227,6 +231,8 @@ class ModelRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, Resp
`ModelResponse` or `AIMessage` (the final result).
Raises:
+ Exception: Any exception not matched by `retry_on` is re-raised immediately,
+ without being passed to `on_failure`.
RuntimeError: If the retry loop completes without returning. (This should not happen.)
"""
# Initial attempt + retries
@@ -240,8 +246,8 @@ class ModelRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, Resp
# Check if we should retry this exception
if not should_retry_exception(exc, self.retry_on):
- # Exception is not retryable, handle failure immediately
- return self._handle_failure(exc, attempts_made)
+ # Exception is not retryable, re-raise immediately
+ raise
# Check if we have more retries left
if attempt < self.max_retries:
@@ -279,6 +285,8 @@ class ModelRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, Resp
`ModelResponse` or `AIMessage` (the final result).
Raises:
+ Exception: Any exception not matched by `retry_on` is re-raised immediately,
+ without being passed to `on_failure`.
RuntimeError: If the retry loop completes without returning. (This should not happen.)
"""
# Initial attempt + retries
@@ -292,8 +300,8 @@ class ModelRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, Resp
# Check if we should retry this exception
if not should_retry_exception(exc, self.retry_on):
- # Exception is not retryable, handle failure immediately
- return self._handle_failure(exc, attempts_made)
+ # Exception is not retryable, re-raise immediately
+ raise
# Check if we have more retries left
if attempt < self.max_retries:
diff --git a/libs/langchain_v1/langchain/agents/middleware/tool_retry.py b/libs/langchain_v1/langchain/agents/middleware/tool_retry.py
index aa7f713f9b..a00bc2fe97 100644
--- a/libs/langchain_v1/langchain/agents/middleware/tool_retry.py
+++ b/libs/langchain_v1/langchain/agents/middleware/tool_retry.py
@@ -57,12 +57,15 @@ class ToolRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, Respo
!!! example "Custom exception filtering"
+ Exceptions for which the callable returns `False` propagate to the caller.
+ They are not passed to `on_failure`.
+
```python
from requests.exceptions import HTTPError
def should_retry(exc: Exception) -> bool:
- # Only retry on 5xx errors
+ # Retry 5xx; anything else propagates
if isinstance(exc, HTTPError):
return 500 <= exc.status_code < 600
return False
@@ -302,6 +305,8 @@ class ToolRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, Respo
`ToolMessage` or `Command` (the final result).
Raises:
+ Exception: Any exception not matched by `retry_on` is re-raised immediately,
+ without being passed to `on_failure`.
RuntimeError: If the retry loop completes without returning. This should not happen.
"""
tool_name = request.tool.name if request.tool else request.tool_call["name"]
@@ -364,6 +369,8 @@ class ToolRetryMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, Respo
`ToolMessage` or `Command` (the final result).
Raises:
+ Exception: Any exception not matched by `retry_on` is re-raised immediately,
+ without being passed to `on_failure`.
RuntimeError: If the retry loop completes without returning. This should not happen.
"""
tool_name = request.tool.name if request.tool else request.tool_call["name"]
diff --git a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_model_retry.py b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_model_retry.py
index 3b6e2fd976..3a1a741d23 100644
--- a/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_model_retry.py
+++ b/libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_model_retry.py
@@ -291,12 +291,12 @@ def test_model_retry_succeeds_after_retries() -> None:
assert model.attempt == 3
-def test_model_retry_specific_exceptions() -> None:
- """Test ModelRetryMiddleware only retries specific exception types."""
+def test_model_retry_non_retryable_exception_reraises() -> None:
+ """Non-retryable exceptions are re-raised even when on_failure='continue'."""
# This model will fail with RuntimeError, which we won't retry
model = AlwaysFailingModel(error_message="Runtime error", error_type=RuntimeError)
- # Only retry ValueError
+ # Only retry ValueError — RuntimeError is not retryable
retry = ModelRetryMiddleware(
max_retries=2,
retry_on=(ValueError,),
@@ -312,15 +312,13 @@ def test_model_retry_specific_exceptions() -> None:
checkpointer=InMemorySaver(),
)
- result = agent.invoke(
- {"messages": [HumanMessage("Hello")]},
- {"configurable": {"thread_id": "test"}},
- )
-
- ai_messages = [m for m in result["messages"] if isinstance(m, AIMessage)]
- assert len(ai_messages) >= 1
- # RuntimeError should fail immediately (1 attempt only)
- assert "1 attempt" in ai_messages[-1].content
+ # RuntimeError does not match retry_on, so it is re-raised
+ # even though on_failure="continue"
+ with pytest.raises(RuntimeError, match="Runtime error"):
+ agent.invoke(
+ {"messages": [HumanMessage("Hello")]},
+ {"configurable": {"thread_id": "test"}},
+ )
def test_model_retry_custom_exception_filter() -> None:
@@ -390,17 +388,16 @@ def test_model_retry_custom_exception_filter() -> None:
checkpointer=InMemorySaver(),
)
- result = agent.invoke(
- {"messages": [HumanMessage("Hello")]},
- {"configurable": {"thread_id": "test"}},
- )
-
- ai_messages = [m for m in result["messages"] if isinstance(m, AIMessage)]
- assert len(ai_messages) >= 1
+ # First attempt raises retryable error → retried.
+ # Second attempt raises non-retryable error → re-raised (not swallowed),
+ # even though on_failure="continue".
+ with pytest.raises(CustomError, match="Non-retryable error"):
+ agent.invoke(
+ {"messages": [HumanMessage("Hello")]},
+ {"configurable": {"thread_id": "test"}},
+ )
- # Should retry once (attempt 1 with retry_me=True), then fail on attempt 2 (retry_me=False)
assert attempt_count["value"] == 2
- assert "2 attempts" in ai_messages[-1].content
def test_model_retry_reraises_graph_bubble_up() -> None:
@@ -611,6 +608,34 @@ async def test_model_retry_async_failing_model() -> None:
assert "ValueError" in last_msg
+async def test_model_retry_async_non_retryable_exception_reraises() -> None:
+ """Non-retryable exceptions are re-raised on the async path too."""
+ model = AlwaysFailingModel(error_message="Runtime error", error_type=RuntimeError)
+
+ # Only retry ValueError — RuntimeError is not retryable
+ retry = ModelRetryMiddleware(
+ max_retries=2,
+ retry_on=(ValueError,),
+ initial_delay=0.01,
+ jitter=False,
+ on_failure="continue",
+ )
+
+ agent = create_agent(
+ model=model,
+ tools=[],
+ middleware=[retry],
+ checkpointer=InMemorySaver(),
+ )
+
+ with pytest.raises(RuntimeError, match="Runtime error"):
+ await agent.ainvoke(
+ {"messages": [HumanMessage("Hello")]},
+ {"configurable": {"thread_id": "test"}},
+ )
+
+
@pytest.mark.asyncio
async def test_model_retry_async_succeeds_after_retries() -> None:
"""Test ModelRetryMiddleware async execution succeeds after temporary failures."""