[Python] Why the Condition in the Langraph is not working

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to build a langraph and call the relevant node as per the condition, this is my code

    class PersonDetails(TypedDict):
    name: str
    age: int

    # Nodes
    def greet(state: PersonDetails):
    print(f"Hello, {state['name']}!")
    return state

    def check_age(state: PersonDetails):
    age = state['age']
    if age >= 21:
    return "can_drink"
    elif age >= 16:
    return "can_drive"
    else:
    return "minor"
    # return state

    def can_drink(state: PersonDetails):
    print("You can legally drink ")
    return state

    def can_drive(state: PersonDetails):
    print("You can drive ")
    return state

    def minor(state: PersonDetails):
    print("You're a minor ")
    return state

    # Build graph
    graph = StateGraph(PersonDetails)
    graph.add_node("greet", greet)
    graph.add_node("check_age", RunnableLambda(check_age)) # must wrap conditional node
    graph.add_node("can_drink", can_drink)
    graph.add_node("can_drive", can_drive)
    graph.add_node("minor", minor)

    # Edges
    graph.add_edge(START, "greet")
    graph.add_edge("greet", "check_age")
    # graph.add_edge("check_age", END)

    # Conditional branching
    graph.add_conditional_edges(
    "check_age",
    {
    "can_drink": "can_drink",
    "can_drive": "can_drive",
    "minor": "minor"
    }
    )

    graph.add_edge("can_drink", END)
    graph.add_edge("can_drive", END)
    graph.add_edge("minor", END)

    # Compile graph
    app = graph.compile()


    but when i run this code, i am getting error like this


    TypeError: Expected a Runnable, callable or dict.Instead got an unsupported type: <class 'str'>
    ​

    not able to know what exactly i am doing wrong, can anyone guide me on this

    Continue reading...

Compartilhe esta Página