Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

目前仅在 langchainjs(JavaScript/TypeScript)中使用。
此错误在工具调用操作期间传递了不匹配、不足或过多的 ToolMessage 对象时发生。 该错误源于一个基本要求:带有 tool_calls 的助手消息后面必须跟有对应每个 tool_call_id 的工具消息。 当模型返回带有工具调用的 AIMessage 时,您必须为每个工具调用提供恰好一个对应的 ToolMessage,且 tool_call_id 值必须匹配。

常见原因

  • 响应不足:如果模型请求两次工具执行但您只提供一个响应消息,模型会拒绝不完整的消息链
  • 重复响应:为同一个工具调用 ID 提供多个 ToolMessage 对象会导致拒绝,ID 不匹配也是如此
  • 孤立的工具消息:在没有前置包含工具调用的 AIMessage 的情况下发送 ToolMessage 违反了协议要求
以下是一个有问题的模式示例:
// 模型请求两个工具调用
responseMessage.tool_calls // 返回 2 个调用

// 但只提供了一个 ToolMessage
chatHistory.push({
  role: "tool",
  content: toolResponse,
  tool_call_id: responseMessage.tool_calls[0].id
});

await modelWithTools.invoke(chatHistory); // 失败并报 INVALID_TOOL_RESULTS

故障排除

要解决此错误:
  • 计数匹配对:确保前一个 AIMessage 中每个工具调用对应一个 ToolMessage
  • 验证 ID:确认每个 ToolMessage.tool_call_id 匹配实际的工具调用标识符