UNPKG

langchain

Version:
60 lines • 2.27 kB
import { AgentMiddleware, ToolCallRequest } from "./types.js"; import { MessageContent } from "@langchain/core/messages"; import { ClientTool, ServerTool } from "@langchain/core/tools"; /** * Handler called when tool execution throws. * * Return content to surface the error to the model as an error `ToolMessage`. * Return nothing to propagate the original error. */ export type ToolErrorHandler = (error: unknown, request: ToolCallRequest) => MessageContent | void | Promise<MessageContent | void>; /** * Configuration for {@link toolErrorMiddleware}. */ export interface ToolErrorMiddlewareConfig { /** * Handler called when tool execution throws. Return content to convert the * error into an error `ToolMessage`, or return nothing to propagate it. */ onError: ToolErrorHandler; /** * Optional tools or tool names to handle errors for. Handles all tools when * omitted. */ tools?: Array<ClientTool | ServerTool | string>; } /** * Converts selected tool execution errors into error `ToolMessage`s. * * Handling is opt-in: {@link ToolErrorMiddlewareConfig.onError} must return * content for an error to be sent to the model. Returning nothing propagates * the original error unchanged. LangGraph control-flow signals always * propagate and are never passed to `onError`. * * Prefer returning content that identifies the error type without including * the raw error message, which may contain sensitive or internal details. * * This middleware does not retry. To retry before handling an error, place * `toolRetryMiddleware({ onFailure: "error" })` after this middleware. * * @example * ```ts * import { createAgent, toolErrorMiddleware } from "langchain"; * * const agent = createAgent({ * model, * tools: [searchTool], * middleware: [ * toolErrorMiddleware({ * onError: (error, request) => { * if (error instanceof TypeError) { * return `Tool '${request.toolCall.name}' failed with TypeError.`; * } * }, * }), * ], * }); * ``` */ export declare function toolErrorMiddleware(config: ToolErrorMiddlewareConfig): AgentMiddleware<undefined, undefined, unknown, readonly (ServerTool | ClientTool)[], readonly []>; //# sourceMappingURL=toolError.d.ts.map