fix: catch tool errors in wrapToolCall middleware to prevent crash#437
Open
Himanshu Kumar (himanshu231204) wants to merge 2 commits into
Open
Conversation
…d promise rejection crash Any tool error during agent execution crashed the entire run via unhandled promise rejection in the langchain tool-call transformer. Root cause: when wrapToolCall middleware is present, tool errors get wrapped in MiddlewareError by the composition layer. ToolNode.#handleError then re-throws them as fatal because handleToolErrors !== true. The createToolCallTransformer.rejects output promises that nobody catches, leading to UnhandledPromiseRejection -> process crash. Fix: catch tool execution errors inside the wrapToolCall middleware and convert them to ToolMessages before they reach the composition layer. GraphInterrupt and AbortError are re-thrown as control flow. Also add defensive error handling to addFrontmatterWarning so validation failures don't crash the run. Closes langchain-ai#426
Contributor
Author
|
Colin Francis (@colifran) Ready for review when you get a chance. Thanks! |
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.
Related Issue
Closes #426
Problem
Any tool error during agent execution crashes the entire run via unhandled promise rejection in the langchain tool-call transformer.
Root Cause
When
wrapToolCallmiddleware is present, tool errors get wrapped inMiddlewareErrorby the composition layer.ToolNode.#handleErrorthen re-throws them as fatal becausehandleToolErrors !== true. ThecreateToolCallTransformerrejects output promises that nobody catches, leading toUnhandledPromiseRejectionand a process crash.Crash Path
Changes
src/agent/okf-middleware.tsRestructured
wrapToolCall— Catch tool execution errors inside the middleware and convert them toToolMessages before they reach the composition layer.GraphInterruptandAbortErrorare re-thrown as control flow.Defensive
addFrontmatterWarning— WrappedvalidatePersistedFilein try/catch so frontmatter validation failures do not crash the run.Added helper functions —
isGraphInterruptLike(),isAborted(),toolErrorToMessage().test/index-middleware.test.ts5 new tests covering error resilience scenarios:
ToolMessageinstead of crashingToolMessageGraphInterrupterrors without conversionAbortErrorwithout conversionaddFrontmatterWarningerrors and returns original resultTest Results
Checklist