Skip to content

Conditional edges

A conditional edge calls a routing function after a node finishes. The function reads the updated state and returns the next node name, END, or a list of destinations.1

from typing import Literal
from langgraph.graph import END
def route_refund(state: RefundState) -> Literal["approve", "review", END]:
if state["policy_violation"]:
return "review"
if state["eligible"]:
return "approve"
return END
builder.add_conditional_edges("check_policy", route_refund)

Remember the order:

  1. Node returns an update.
  2. LangGraph applies the update.
  3. Router reads the new state.
  4. Router returns the destination.

Every loop needs a tested stop condition. Otherwise the graph eventually reaches its recursion limit instead of completing normally.

  1. LangChain, Graph API overview.