Skip to content

fix(prebuilt): handle plain ToolException in default tool error handler#6959

Open
Balaji Seshadri (gitbalaji) wants to merge 1 commit intolangchain-ai:mainfrom
gitbalaji:fix/tool-exception-default-handler
Open

fix(prebuilt): handle plain ToolException in default tool error handler#6959
Balaji Seshadri (gitbalaji) wants to merge 1 commit intolangchain-ai:mainfrom
gitbalaji:fix/tool-exception-default-handler

Conversation

@gitbalaji

Summary

Fixes #6449.

_default_handle_tool_errors only caught ToolInvocationError (a subclass of ToolException). A plain ToolException — as raised by MCP adapters (langchain_mcp_adapters) and other tool wrappers — was re-raised instead of being returned as an error ToolMessage, crashing the graph.

Before:

def _default_handle_tool_errors(e: Exception) -> str:
    if isinstance(e, ToolInvocationError):
        return e.message
    raise e  # ← ToolException from MCP falls through here

After:

def _default_handle_tool_errors(e: Exception) -> str:
    if isinstance(e, ToolException):  # catches both ToolException and ToolInvocationError
        return str(e)
    raise e

ToolInvocationError calls super().__init__(self.message), so str(e) is identical to e.message — no behaviour change for the existing path.

Test plan

  • Added test_tool_node_default_handler_catches_tool_exception — verifies a plain ToolException from an async tool is returned as an error ToolMessage with the default handler
  • Existing test_tool_node_error_handling and test_tool_node_handle_tool_errors_false still pass
  • make format — clean
  • make lint — clean (Success: no issues found in 5 source files)

🤖 Generated with Claude Code

@gitbalaji Balaji Seshadri (gitbalaji) force-pushed the fix/tool-exception-default-handler branch from d832605 to 067c89d Compare February 27, 2026 15:43
_default_handle_tool_errors only caught ToolInvocationError (a subclass
of ToolException). A plain ToolException — as raised by MCP adapters and
other tool wrappers — was re-raised instead of being returned as an error
ToolMessage, crashing the graph.

Fix: check isinstance(e, ToolException) instead of ToolInvocationError.
Since ToolInvocationError calls super().__init__(self.message), str(e) is
equivalent to e.message for both types, so no behaviour changes for the
existing ToolInvocationError path.

Fixes langchain-ai#6449.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@gitbalaji Balaji Seshadri (gitbalaji) force-pushed the fix/tool-exception-default-handler branch from 067c89d to 3ce743c Compare March 1, 2026 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants