> ## 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

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

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

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

## 故障排除

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

1. 不要使用格式错误的消息列表调用图
2. 在中断（手动或由于错误）的情况下，您可以：
   * 提供与现有工具调用匹配的 [`ToolMessage`](https://reference.langchain.com/python/langchain-core/messages/tool/ToolMessage) 对象，并调用 `graph.invoke({'messages': [ToolMessage(...)]})`。
     **注意**：这会将消息附加到历史记录中，并从 START 节点运行图。
   * 手动更新状态并从中断处恢复图：
     1. 使用 `graph.get_state(config)` 从图状态获取最近的消息列表
     2. 修改消息列表，要么从 AIMessages 中移除未应答的工具调用，要么添加 `tool_call_ids` 与未应答工具调用匹配的 [`ToolMessage`](https://reference.langchain.com/python/langchain-core/messages/tool/ToolMessage) 对象
     3. 使用修改后的消息列表调用 `graph.update_state(config, {'messages': ...})`
     4. 恢复图，例如调用 `graph.invoke(None, 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>
