Skip to content

Commit 59295e3

Browse files
fix(langchain): add missing section on handling tool errors (#1622)
The section was missing examples.
1 parent 8655cdd commit 59295e3

File tree

2 files changed

+67
-19
lines changed

2 files changed

+67
-19
lines changed

src/oss/javascript/migrate/langchain-v1.mdx

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,36 +396,51 @@ The `tools` argument to `createAgent` accepts:
396396
- LangChain tool instances
397397
- Objects that represent built-in provider tools
398398

399-
Use middleware `wrapToolCall` to centralize error handling and logging for tools.
399+
#### Handling tool errors
400+
401+
You can now configure the handling of tool errors with middleware implementing the `wrapToolCall` method.
400402

401403
<CodeGroup>
402404
```typescript v1 (new)
403-
import { createAgent, createMiddleware } from "langchain";
405+
import { createAgent, createMiddleware, ToolMessage } from "langchain";
404406

405-
const errorHandling = createMiddleware({
406-
name: "ToolErrors",
407+
const handleToolErrors = createMiddleware({
408+
name: "HandleToolErrors",
407409
wrapToolCall: async (request, handler) => {
408410
try {
409411
return await handler(request);
410-
} catch (err) {
411-
return `Error executing ${request.toolName}: ${String(err)}`;
412+
} catch (error) {
413+
// Only handle errors that occur during tool execution due to invalid inputs
414+
// that pass schema validation but fail at runtime (e.g., invalid SQL syntax).
415+
// Do NOT handle:
416+
// - Network failures (use tool retry middleware instead)
417+
// - Incorrect tool implementation errors (should bubble up)
418+
// - Schema mismatch errors (already auto-handled by the framework)
419+
//
420+
// Return a custom error message to the model
421+
return new ToolMessage({
422+
content: `Tool error: Please check your input and try again. (${error})`,
423+
tool_call_id: request.toolCall.id!,
424+
});
412425
}
413426
},
414427
});
415428

416429
const agent = createAgent({
417430
model: "claude-sonnet-4-5-20250929",
418431
tools: [checkWeather, searchWeb],
419-
middleware: [errorHandling],
432+
middleware: [handleToolErrors],
420433
});
421434
```
422435
```typescript v0 (old)
423-
import { createReactAgent } from "@langchain/langgraph/prebuilts";
436+
import { createReactAgent, ToolNode } from "@langchain/langgraph/prebuilts";
424437

425438
const agent = createReactAgent({
426439
model: "claude-sonnet-4-5-20250929",
427-
tools: [checkWeather, searchWeb],
428-
// Error handling commonly implemented inside tool code or post hooks
440+
tools: new ToolNode(
441+
[checkWeather, searchWeb],
442+
{ handleToolErrors: true } // [!code highlight]
443+
),
429444
});
430445
```
431446
</CodeGroup>

src/oss/python/migrate/langchain-v1.mdx

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -592,19 +592,52 @@ You can now configure the handling of tool errors with middleware implementing t
592592

593593
<CodeGroup>
594594
```python v1 (new)
595+
from langchain.agents import create_agent
596+
from langchain.agents.middleware import wrap_tool_call
597+
from langchain.messages import ToolMessage
598+
595599

596600
@wrap_tool_call
597-
def retry_on_error(request, handler):
598-
max_retries = 3
599-
for attempt in range(max_retries):
600-
try:
601-
return handler(request)
602-
except Exception:
603-
if attempt == max_retries - 1:
604-
raise
601+
def handle_tool_errors(request, handler):
602+
"""Handle tool execution errors with custom messages."""
603+
try:
604+
return handler(request)
605+
except Exception as e:
606+
# Only handle errors that occur during tool execution due to invalid inputs
607+
# that pass schema validation but fail at runtime (e.g., invalid SQL syntax).
608+
# Do NOT handle:
609+
# - Network failures (use tool retry middleware instead)
610+
# - Incorrect tool implementation errors (should bubble up)
611+
# - Schema mismatch errors (already auto-handled by the framework)
612+
#
613+
# Return a custom error message to the model
614+
return ToolMessage(
615+
content=f"Tool error: Please check your input and try again. ({str(e)})",
616+
tool_call_id=request.tool_call["id"]
617+
)
618+
619+
agent = create_agent(
620+
model="claude-sonnet-4-5-20250929",
621+
tools=[check_weather, search_web],
622+
middleware=[handle_tool_errors]
623+
)
605624
```
606625
```python v0 (old)
607-
# Example coming soon
626+
from langgraph.prebuilt import create_react_agent, ToolNode
627+
from langchain.messages import ToolMessage
628+
629+
630+
def handle_tool_error(error: Exception) -> str:
631+
"""Custom error handler function."""
632+
return f"Tool error: Please check your input and try again. ({str(error)})"
633+
634+
agent = create_react_agent(
635+
model="claude-sonnet-4-5-20250929",
636+
tools=ToolNode(
637+
[check_weather, search_web],
638+
handle_tool_errors=handle_tool_error # [!code highlight]
639+
)
640+
)
608641
```
609642
</CodeGroup>
610643

0 commit comments

Comments
 (0)