AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

On_tool_error Callback Missing Caller'S **Kwargs

The on_tool_error callback does not receive the arbitrary keyword arguments (e.g., tenant, trace_id) that were passed to tool.invoke, while on_tool_start and on_tool_end do. This inconsistency causes loss of critical context during error handling.

highConfidence 95%Langchain-CoreAffected Vlangchain-Core 1.6.5

Origin Analysis

In langchain_core/tools/base.py, the on_tool_error invocation (lines 1131 and 1261) is called as handler.on_tool_error(error_to_raise, tool_call_id=tool_call_id) without forwarding **kwargs, unlike the adjacent on_tool_end call which forwards **kwargs.
1. Create a custom BaseCallbackHandler that records kwargs in on_tool_start, on_tool_end, and on_tool_error. 2. Define a StructuredTool that raises ToolException. 3. Invoke the tool with an extra keyword argument (e.g., tenant='acme') and a callback handler. 4. Observe that on_tool_start and on_tool_end receive the tenant kwarg, but on_tool_error does not.

Fixing Code Block

Edge Case Audit

If callers pass a mutable object in kwargs, it is shared across callbacks and could lead to race conditions in concurrent execution. Additionally, if a custom handler overrides on_tool_error with a signature that does not accept **kwargs, it will break; however, standard handler signatures include **kwargs. Rollback advice: keep a fallback to the original call signature if TypeError occurs, or wrap the forwarding in a try-except and log a warning.

Ecosystem Topology