INVALID_CHAT_HISTORY¶
当 call_model
图节点收到格式错误的消息列表时,预构建的 create_react_agent 会引发此错误。具体来说,当存在带有 tool_calls
的 AIMessage
(LLM 请求调用工具),但没有相应的 ToolMessage
(要返回给 LLM 的工具调用结果)时,消息列表的格式就是错误的。
您看到此错误可能有以下几个原因:
- 您在调用图时手动传递了一个格式错误的消息列表,例如
graph.invoke({'messages': [AIMessage(..., tool_calls=[...])]})
- 图在从
tools
节点接收更新(即 ToolMessages 列表)之前被中断,并且您使用非 None 或非 ToolMessage 的输入调用了它,例如graph.invoke({'messages': [HumanMessage(...)]}, config)
。
这种中断可能是由以下方式之一触发的:
- 您在
create_react_agent
中手动设置了interrupt_before = ['tools']
- 其中一个工具引发了 ToolNode (
"tools"
) 未处理的错误
故障排除¶
要解决此问题,您可以执行以下操作之一:
- 不要使用格式错误的消息列表来调用图
-
在发生中断(手动或由于错误)的情况下,您可以:
-
提供与现有工具调用匹配的 ToolMessages,并调用
graph.invoke({'messages': [ToolMessage(...)]})
。注意:这会将消息附加到历史记录中,并从 START 节点开始运行图。 -
手动更新状态并从中断处恢复图
1. get the list of most recent messages from the graph state with `graph.get_state(config)` 2. modify the list of messages to either remove unanswered tool calls from AIMessages
或者添加带有与未应答工具调用相匹配的 tool_call_ids 的 ToolMessages 3. 使用修改后的消息列表调用 graph.update_state(config, {'messages': ...})
4. 恢复图,例如调用 graph.invoke(None, config)