pythonlangchainagentlanggraphlangchain-agents

Conditional Edge in Langgraph is not working as expected


I'm new to Langgraph, trying to create a simple graph with four nodes i.e 'node_1', 'node_2', 'node_3', 'node_4'.

the edges are defined as : node_1--> node_2 direct edge, from node_2 conditional edge for node_3 and node_4, ultimately node_3 and node_4 will connect to END.

when i visualise, the graph looks odd seems like node_3 & node_4 left out no connection/edge for them.

please find code and graph below

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()))

enter image description here

I was expecting the conditional edges to look like below as in dotted line, i'm not sure if i'm doing something wrong. please help.

enter image description here


Solution

  • Try this:

    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, display # For visualization
    
    
    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]}
    
    def decide_mood(state):
        current_count = state['node_output'][-1]
        if current_count % 2 == 0:
            print(f"Deciding mood: current_count {current_count} is even. Going to 'to_node_3'.")
            return "node_3"
        else:
            print(f"Deciding mood: current_count {current_count} is odd. Going to 'to_node_4'.")
            return "node_4"
        
    #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,{
            "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()))
    

    Output:

    enter image description here