fix(langchain): re-raise non-retryable exceptions in ModelRetryMiddleware - #38960
Merged
ccurme (ccurme) merged 4 commits intoAug 18, 2026
Conversation
…eware` `ModelRetryMiddleware` routed exceptions that do not match `retry_on` into `_handle_failure`, which under the default `on_failure="continue"` converted them into an error `AIMessage` — so an error the user deliberately excluded from retrying was swallowed and the agent continued on a fabricated turn. langchain-ai#38845 changed this exact branch on the tool side to re-raise, and langchain-ai#38884 documented the contract: exceptions that do not match `retry_on` propagate immediately and are not handled by `on_failure`. The model side kept the pre-langchain-ai#38845 code, so `retry_on` meant opposite things for tools and models. This mirrors langchain-ai#38845 in both the sync and async paths and aligns the `retry_on` docstring with langchain-ai#38884. Fixes langchain-ai#38893 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ThW31LHCRsNnUgVgUrQeB9
This comment has been minimized.
This comment has been minimized.
29 tasks
Merging this PR will not alter performance
Comparing Footnotes
|
Raja Gopal (Rajagopalhertzian)
added a commit
to Rajagopalhertzian/langchain
that referenced
this pull request
Aug 14, 2026
Closes langchain-ai#38960 - ModelRetryMiddleware now re-raises non-retryable exceptions immediately instead of swallowing them via on_failure='continue' - Matches ToolRetryMiddleware behavior and retry_on contract (langchain-ai#38884) - Updated tests to reflect new behavior
Yiğit ERDOĞAN (Yigtwxx)
deleted the
Yigtwxx/langchain/propagate-non-retryable-model-exceptions
branch
August 19, 2026 06:15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #38893
Setting
retry_on=(RateLimitError,)is the natural way to say "retry transient API failures, surface everything else."ToolRetryMiddlewarehonors that: since #38845 an exception that doesn't matchretry_onis re-raised immediately, and #38884 documented the contract — "Exceptions that do not match propagate immediately and are not handled byon_failure."ModelRetryMiddlewarenever received that change. It still routes non-matching exceptions into_handle_failure, which under the defaulton_failure="continue"converts them into a plain errorAIMessage. So aTypeErrorfrom a callback or a customwrap_model_callmiddleware becomes an assistant turn readingModel call failed after 1 attempt with TypeError: ..., and the agent proceeds on that fabricated answer instead of failing.Two things follow from this. First, errors the user deliberately excluded from retry are swallowed exactly when they most need to surface — a
TypeErrorusually means the pipeline is structurally broken, and the error message ends up occupying the slot where the model's answer should be, structurally indistinguishable from a real response downstream. Second,retry_onmeans opposite things on the two sides: narrowing it surfaces errors for tools and hides them for models, same package, same parameter name, same documented contract.This mirrors #38845 on the model side — the two
_handle_failurecalls in the non-retryable branch become a bareraise, in bothwrap_model_callandawrap_model_call— and aligns theretry_ondocstring with the wording #38884 established.ModelFallbackMiddlewaredoesn't useretry_onand is out of scope.This is a behavior change for anyone relying on
on_failureto absorb non-retryable errors, but it is the same change maintainers already accepted on the tool side, and #38884 states this contract for both.Tests mirror the structure of the #38845 tests: sync and async cases asserting a non-matching exception propagates on the first attempt and is not routed through
on_failure, including theon_failure="continue"path where the message is fabricated today.Release note
ModelRetryMiddlewarenow propagates exceptions that do not matchretry_oninstead of converting them into an errorAIMessage, matchingToolRetryMiddleware's behavior and the documentedretry_oncontract. Non-retryable exceptions are no longer handled byon_failure.This contribution was developed with assistance from an AI coding agent (Claude Code); all changes were reviewed and verified by the author.