[PR #493] [CLOSED] feat(deepagents): add handleToolErrors option to recover from tool exceptions #507

Closed
opened 2026-06-05 17:23:29 -04:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagentsjs/pull/493
Author: @JadenKim-dev
Created: 4/28/2026
Status: Closed

Base: mainHead: feat/handle-tool-errors


📝 Commits (1)

  • e04d0e4 feat(deepagents): add handleToolErrors option to catch tool exceptions as ToolMessage

📊 Changes

6 files changed (+266 additions, -1 deletions)

View changed files

📝 libs/deepagents/src/agent.test.ts (+107 -1)
📝 libs/deepagents/src/agent.ts (+8 -0)
📝 libs/deepagents/src/middleware/index.ts (+3 -0)
libs/deepagents/src/middleware/tool-error-handler.test.ts (+111 -0)
libs/deepagents/src/middleware/tool-error-handler.ts (+32 -0)
📝 libs/deepagents/src/types.ts (+5 -0)

📄 Description

Problem

When an tool throws (timeout, connection error, validation error),
LangGraph JS propagates it as a "Multiple errors occurred during superstep N"
crash. The agent never sees the error in a ToolMessage and cannot recover.

Solution

Adds opt-in handleToolErrors to createDeepAgent. When enabled, exceptions
thrown inside tool functions are caught and returned as a ToolMessage with
status: "error" instead of crashing the graph.

Usage

ts const agent = createDeepAgent({ tools: [myTool], handleToolErrors: true, }) ​

The LLM sees the error message and can decide whether to retry with different
arguments.

Implementation

A new createToolErrorHandlerMiddleware() is inserted as the outermost
wrapToolCall layer in the middleware stack, so it catches any exception
thrown anywhere in the tool execution chain. NodeInterrupt (HITL breakpoints)
and aborted AbortSignal are always re-thrown. Defaults to false to preserve
existing behavior.

Closes #485


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/deepagentsjs/pull/493 **Author:** [@JadenKim-dev](https://github.com/JadenKim-dev) **Created:** 4/28/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/handle-tool-errors` --- ### 📝 Commits (1) - [`e04d0e4`](https://github.com/langchain-ai/deepagentsjs/commit/e04d0e4be7920131f3a52ce318a106da886c6ffb) feat(deepagents): add handleToolErrors option to catch tool exceptions as ToolMessage ### 📊 Changes **6 files changed** (+266 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `libs/deepagents/src/agent.test.ts` (+107 -1) 📝 `libs/deepagents/src/agent.ts` (+8 -0) 📝 `libs/deepagents/src/middleware/index.ts` (+3 -0) ➕ `libs/deepagents/src/middleware/tool-error-handler.test.ts` (+111 -0) ➕ `libs/deepagents/src/middleware/tool-error-handler.ts` (+32 -0) 📝 `libs/deepagents/src/types.ts` (+5 -0) </details> ### 📄 Description ## Problem When an tool throws (timeout, connection error, validation error), LangGraph JS propagates it as a "Multiple errors occurred during superstep N" crash. The agent never sees the error in a ToolMessage and cannot recover. ## Solution Adds opt-in `handleToolErrors` to `createDeepAgent`. When enabled, exceptions thrown inside tool functions are caught and returned as a `ToolMessage` with `status: "error"` instead of crashing the graph. ## Usage ​```ts const agent = createDeepAgent({ tools: [myTool], handleToolErrors: true, }) ​``` The LLM sees the error message and can decide whether to retry with different arguments. ## Implementation A new `createToolErrorHandlerMiddleware()` is inserted as the outermost `wrapToolCall` layer in the middleware stack, so it catches any exception thrown anywhere in the tool execution chain. `NodeInterrupt` (HITL breakpoints) and aborted `AbortSignal` are always re-thrown. Defaults to `false` to preserve existing behavior. Closes #485 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-06-05 17:23:29 -04:00
yindo closed this issue 2026-06-05 17:23:30 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#507