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

此错误在预构建的 [`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)或[提交 issue](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
