> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# INVALID_CHAT_HISTORY

此错误在预构建的 [`createAgent`](https://reference.langchain.com/javascript/langchain/index/createAgent) 中触发，当 `callModel` 图节点接收到格式错误的消息列表时。具体来说，当存在带有 `tool_calls`（LLM 请求调用工具）的 `AIMessage`，但没有对应的 [`ToolMessage`](https://reference.langchain.com/javascript/langchain-core/messages/ToolMessage)（返回给 LLM 的工具调用结果）时，即为格式错误。

您看到此错误可能有几个原因：

1. 您在调用图时手动传递了格式错误的消息列表，例如 `graph.invoke({messages: [new AIMessage({..., tool_calls: [...]})]})`
2. 图在接收到来自 `tools` 节点的更新（即 [`ToolMessage`](https://reference.langchain.com/javascript/langchain-core/messages/ToolMessage) 列表）之前被中断，并且您使用非空或非 ToolMessage 的输入调用了它，例如 `graph.invoke({messages: [new HumanMessage(...)]}, config)`。此中断可能由以下方式之一触发：
   * 您在 `createAgent` 中手动设置了 `interruptBefore: ['tools']`
   * 其中一个工具引发了 [`ToolNode`](https://reference.langchain.com/javascript/langchain-langgraph/prebuilt/ToolNode)（`"tools"`）未处理的错误

## 故障排除

要解决此问题，您可以执行以下操作之一：

1. 不要使用格式错误的消息列表调用图
2. 在中断（手动或由于错误）的情况下，您可以：
   * 提供与现有工具调用匹配的 `ToolMessage` 对象，并调用 `graph.invoke({messages: [new ToolMessage(...)]})`。**注意**：这将把消息附加到历史记录中，并从 START 节点运行图。
   * 手动更新状态并从中断处恢复图：
     1. 使用 `graph.getState(config)` 从图状态获取最近的消息列表
     2. 修改消息列表，要么从 AIMessages 中移除未应答的工具调用，要么添加 `toolCallId` 与未应答工具调用匹配的 `ToolMessage` 对象
     3. 使用修改后的消息列表调用 `graph.updateState(config, {messages: ...})`
     4. 恢复图，例如调用 `graph.invoke(null, config)`

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/langgraph/errors/INVALID_CHAT_HISTORY.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
