> ## Documentation Index
> Fetch the complete documentation index at: https://nvd-54.mintlify.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) 列表）之前被中断，
   并且你使用非 null 或非 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. 修改消息列表以从 AIMessage 中删除未回答的工具调用

或添加 `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>
