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

LangGraph Agent执行时出现间歇性UnboundLocalError问题求助

基于LangGraph的Agent间歇性UnboundLocalError问题

在基于LangGraph的Agent系统中,主Agent调用自定义Agent作为工具时出现间歇性错误,并非每次触发,但发生频率较高,已造成严重影响。

报错信息

An error occurred: local variable 'fut' referenced before assignment
Traceback (most recent call last):
  ...
  File "/Users/yosef/uni/Uni chat store/llm/agents.py", line 326, in run
    return self.build().graph.invoke({'messages':  [HumanMessage(content=messages)]})['messages'][-1].content
  File "/Users/yosef/uni/Uni chat store/venv/lib/python3.10/site-packages/langgraph/pregel/__init__.py", line 1448, in invoke
    for chunk in self.stream(
  File "/Users/yosef/uni/Uni chat store/venv/lib/python3.10/site-packages/langgraph/pregel/__init__.py", line 980, in stream
    del fut, task
UnboundLocalError: local variable 'fut' referenced before assignment

Agent类代码实现

class AgentState(TypedDict):
    messages: Annotated[list[AnyMessage], operator.add]

class Agent:
    def __init__(self, model, tools, system="", print_tool_result=False):
        self.system = system
        graph = StateGraph(AgentState)
        graph.add_node("llm", self.call_openai)
        graph.add_node("action", self.take_action)
        graph.add_conditional_edges(
            "llm",
            self.exists_action,
            {True: "action", False: END}
        )
        graph.add_edge("action", "llm")
        graph.set_entry_point("llm")
        self.graph = graph.compile()
        self.graph.step_timeout = 10
        self.tools = {t.name: t for t in tools}
        self.model = model.bind_tools(tools)
        self.print_tool_result = print_tool_result

    def exists_action(self, state: AgentState):
        result = state['messages'][-1]
        return len(result.tool_calls) > 0

    def call_openai(self, state: AgentState):
        messages = state['messages']
        if self.system:
            messages = [SystemMessage(content=self.system)] + messages
        message = self.model.invoke(messages)
        return {'messages': [message]}

    def take_action(self, state: AgentState):
        tool_calls = state['messages'][-1].tool_calls
        results = []
        for t in tool_calls:
            print(f"Calling: {t}")
            if not t['name'] in self.tools:      # check for bad tool name from LLM
                print("\n ....bad tool name....")
                result = "bad tool name, retry"  # instruct LLM to retry if bad
            else:
                result = self.tools[t['name']].invoke(input=t['args'])
                if self.print_tool_result:
                    # print in blue
                    print(f"\033[94m{result}\033[0m")
                    
            results.append(ToolMessage(tool_call_id=t['id'], name=t['name'], content=str(result)))
        print("Back to the model!")
        return {'messages': results}

已尝试的解决方案

  • 升级至最新版本的LangGraph
  • 将print语句替换为logging

内容的提问来源于stack exchange,提问作者Yosef Schwartz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 14:40:11