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
相关产品推荐
相关产品推荐

