Langgraph条件边未正常工作:节点3/4无连接可视化问题求助
LangGraph可视化节点缺失问题排查
作为LangGraph新手,我尝试创建包含node_1、node_2、node_3、node_4四个节点的简单图,边规则如下:
node_1直接连接node_2node_2通过条件边连接node_3和node_4node_3、node_4最终连接至END
但可视化时发现node_3和node_4无连接,似乎被遗漏。以下是代码、当前图和期望图:
原代码
from typing_extensions import TypedDict from typing import Annotated from langgraph.graph import StateGraph,START,END from operator import add class State(TypedDict): user_input: str node_output: Annotated[list[int],add] count = 0 def first_node(state): print("I'm here") global count count+=1 return {'node_output':[count]} def second_node(TypedDict): print("He sent me here") global count count+=2 return {'node_output':[count]} def third_node(TypedDict): print('I was decided') global count count+=3 return {'node_output':[count]} def fourth_node(TypedDict): print("I'm here on chance") global count count+=4 return {'node_output':[count]} #starting graph building builder1 = StateGraph(State) builder1.add_node('node_1',first_node) builder1.add_node('node_2',second_node) builder1.add_node('node_3',third_node) builder1.add_node('node_4',fourth_node) builder1.add_edge(START,'node_1') builder1.add_edge('node_1','node_2') builder1.add_conditional_edges('node_2',decide_mood) builder1.add_edge('node_3',END) builder1.add_edge('node_4',END) my_new_graph = builder1.compile() display(Image(my_new_graph.get_graph().draw_mermaid_png()))
当前可视化图

期望可视化图

问题排查与修复
1. 缺失条件路由函数decide_mood
代码中调用builder1.add_conditional_edges('node_2',decide_mood),但从未定义decide_mood函数。LangGraph无法识别无效的条件边,导致node_3、node_4未被纳入图的有效连接链路中,可视化时自然不会显示。
2. 节点函数参数错误
second_node、third_node、fourth_node的参数写为TypedDict,这是类型定义而非变量名,正确参数应为state(对应自定义的State类型),参数错误可能导致节点逻辑执行异常。
3. 缺失Image导入
代码中使用display(Image(...))但未导入Image类,需补充对应库的导入。
修复后的完整代码
from typing_extensions import TypedDict from typing import Annotated from langgraph.graph import StateGraph, START, END from operator import add from IPython.display import Image # 补充Image导入 class State(TypedDict): user_input: str node_output: Annotated[list[int], add] count = 0 def first_node(state): print("I'm here") global count count += 1 return {'node_output': [count]} # 修正参数为state def second_node(state): print("He sent me here") global count count += 2 return {'node_output': [count]} # 修正参数为state def third_node(state): print('I was decided') global count count += 3 return {'node_output': [count]} # 修正参数为state def fourth_node(state): print("I'm here on chance") global count count += 4 return {'node_output': [count]} # 定义条件路由函数,示例根据count值判断,可按需修改逻辑 def decide_mood(state): global count # 示例条件:count为奇数时到node_3,偶数到node_4 if count % 2 == 1: return 'node_3' else: return 'node_4' # 构建图 builder1 = StateGraph(State) builder1.add_node('node_1', first_node) builder1.add_node('node_2', second_node) builder1.add_node('node_3', third_node) builder1.add_node('node_4', fourth_node) builder1.add_edge(START, 'node_1') builder1.add_edge('node_1', 'node_2') # 条件边需指定目标节点映射,或函数返回节点名 builder1.add_conditional_edges( 'node_2', decide_mood, {'node_3': 'node_3', 'node_4': 'node_4'} ) builder1.add_edge('node_3', END) builder1.add_edge('node_4', END) my_new_graph = builder1.compile() display(Image(my_new_graph.get_graph().draw_mermaid_png()))
修复后重新编译运行,可视化图会正确显示node_2到node_3、node_4的条件边,以及node_3、node_4到END的连接,与期望图一致。
内容的提问来源于stack exchange,提问作者Abhishek K M
相关产品推荐
相关产品推荐

