[GH-ISSUE #68] [Feature Request] Add error recovery handling for tool schema validation errors (like Python's handle_tool_errors) #20

Closed
opened 2026-02-16 06:16:50 -05:00 by yindo · 4 comments
Owner

Originally created by @antonnak on GitHub (Nov 25, 2025).
Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/68

When a tool call fails with a schema validation error (e.g., "Received tool input did not match expected schema"), the exception breaks the agent loop entirely. The agent cannot see the error and retry with corrected parameters.

Example:
Error in middleware "FilesystemMiddleware": Error invoking tool 'write_file' with kwargs... with error: Error: Received tool input did not match expected schema

This is different from Python LangChain, which has handle_tool_errors=True parameter that converts tool errors into ToolMessage responses, allowing the agent to observe the error and self-correct.

Add built-in error handling support, similar to Python's handle_tool_errors

Originally created by @antonnak on GitHub (Nov 25, 2025). Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/68 When a tool call fails with a schema validation error (e.g., "Received tool input did not match expected schema"), the exception breaks the agent loop entirely. The agent cannot see the error and retry with corrected parameters. Example: Error in middleware "FilesystemMiddleware": Error invoking tool 'write_file' with kwargs... with error: Error: Received tool input did not match expected schema This is different from Python LangChain, which has `handle_tool_errors=True` parameter that converts tool errors into ToolMessage responses, allowing the agent to observe the error and self-correct. Add built-in error handling support, similar to Python's handle_tool_errors
yindo closed this issue 2026-02-16 06:16:50 -05:00
Author
Owner

@KashiMoonactive commented on GitHub (Dec 7, 2025):

Happened to me as well. I tried to see if adding this feature to the JS library will resolve it but it didn't.
Eventually I saw that my LLM reached finish_reason: "max_tokens" in response_metadata object, so I configured for the maximum output tokens for my LLM and it worked.

@KashiMoonactive commented on GitHub (Dec 7, 2025): Happened to me as well. I tried to see if adding this feature to the JS library will resolve it but it didn't. Eventually I saw that my LLM reached `finish_reason: "max_tokens"` in `response_metadata` object, so I configured for the maximum output tokens for my LLM and it worked.
Author
Owner

@rtoscani commented on GitHub (Dec 12, 2025):

@antonnak I used this middleware as a workaround on createDeepAgent

export function createToolErrorHandlingMiddleware() {
	return createMiddleware({
		name: 'toolErrorHandlingMiddleware',
		wrapToolCall: async (request, handler) => {
			try {
				// Execute the tool through the provided handler
				const result = await handler(request)
				return result
			} catch (error) {
				// Safely extract the tool name
				const toolName = String(request.tool.name || 'unknown')
				const toolCallId = request.toolCall.id || 'unknown'

				// Convert the caught error into a readable string
				const errorMessage = getErrorMessage(error)

				// Return a ToolMessage with error details instead of throwing an exception
				// This prevents the agent execution from being interrupted by the error
				return new ToolMessage({
					content: `TOOL_ERROR: The tool "${toolName}" failed with the following error:

ERROR_MESSAGE: ${errorMessage}

INSTRUCTIONS: Please review the error and retry the operation with corrected parameters or try an alternative approach.`,
					tool_call_id: toolCallId,
					name: toolName,
				})
			}
		},
	})
}
@rtoscani commented on GitHub (Dec 12, 2025): @antonnak I used this middleware as a workaround on `createDeepAgent` ```js export function createToolErrorHandlingMiddleware() { return createMiddleware({ name: 'toolErrorHandlingMiddleware', wrapToolCall: async (request, handler) => { try { // Execute the tool through the provided handler const result = await handler(request) return result } catch (error) { // Safely extract the tool name const toolName = String(request.tool.name || 'unknown') const toolCallId = request.toolCall.id || 'unknown' // Convert the caught error into a readable string const errorMessage = getErrorMessage(error) // Return a ToolMessage with error details instead of throwing an exception // This prevents the agent execution from being interrupted by the error return new ToolMessage({ content: `TOOL_ERROR: The tool "${toolName}" failed with the following error: ERROR_MESSAGE: ${errorMessage} INSTRUCTIONS: Please review the error and retry the operation with corrected parameters or try an alternative approach.`, tool_call_id: toolCallId, name: toolName, }) } }, }) } ```
Author
Owner

@antonnak commented on GitHub (Dec 22, 2025):

thank you @rtoscani . kind of what I ended up doing as well. I just thought there should be a more native approach than stacking middleware

@antonnak commented on GitHub (Dec 22, 2025): thank you @rtoscani . kind of what I ended up doing as well. I just thought there should be a more native approach than stacking middleware
Author
Owner

@christian-bromann commented on GitHub (Jan 23, 2026):

Yep, as we introduced wrapToolCall this is our recommendation for handling tool errors now. Let me know if you have any further questions. Thanks @rtoscani for providing this insight!

@christian-bromann commented on GitHub (Jan 23, 2026): Yep, as we introduced `wrapToolCall ` this is our recommendation for handling tool errors now. Let me know if you have any further questions. Thanks @rtoscani for providing this insight!
yindo changed title from [Feature Request] Add error recovery handling for tool schema validation errors (like Python's handle_tool_errors) to [GH-ISSUE #68] [Feature Request] Add error recovery handling for tool schema validation errors (like Python's handle_tool_errors) 2026-06-05 17:21:00 -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#20