You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LangGraph自定义human_assistance工具中断后如何恢复流执行?

LangGraph人工干预工具调用后的恢复执行问题

背景与实现代码

我正在用LangGraph构建聊天机器人,自定义了human_assistance工具以支持执行过程中的人工干预,工具代码如下:

@tool
def human_assistance(query: str) -> str:
    human_response = interrupt({"query": query})
    return human_response["data"]

图流响应的获取逻辑:

def stream_graph_updates(user_input: str):
    for event in graph.stream({"messages": [{"role": "user", "content": user_input}]}, config):
        for value in event.values():
            print("Assistant:", value["messages"][-1].content)

while True:
    user_input = input("User: ")
    if user_input.lower() in ["quit", "exit", "q"]:
        print("Goodbye!")
        break
    stream_graph_updates(user_input)

运行错误

输入请求后,助手触发人工协助调用,但出现以下错误:

Traceback (most recent call last):
  ...
TypeError: tuple indices must be integers or slices, not str
...
BadRequestError: status_code: 400, body: {
  'message': "invalid request: An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: human_assistance_we461bfewgfe"
}

尝试的恢复代码

我明白需要返回工具响应来完成tool_call,尝试手动恢复图执行,但不确定正确方式:

human_response = (
    "We, the experts are here to help! We'd recommend you check out LangGraph to build your agent."
    " It's much more reliable and extensible than simple autonomous agents."
)

human_command = Command(resume={"data": human_response})

events = graph.stream(human_command, config)
for event in events:
    if "messages" in event:
        event["messages"][-1].pretty_print()

疑问

  • 如何正确发送Command(resume=...)以完成工具调用?
  • 如何获取并匹配LangGraph期望响应中的tool_call_id?
  • 工具中断后恢复执行的正确流程是什么?恢复时如何对应到特定的工具调用ID?

内容的提问来源于stack exchange,提问作者Hossein Safari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 08:40:12