Lesson 3 of 11
Chapter 18 . LangGraph . Lesson 3 of 11

Conditional routing

A router function reads the state and returns the name of the next node. Decisions live in that one function.

01Real-world example

Illustration: A CI result router: passed tests go to the dashboard, failures become Jira bugs, flaky tests move to quarantine.
A CI result router: passed tests go to the dashboard, failures become Jira bugs, flaky tests move to quarantine.

02The graph

Conditional routing as a graph
route_by_status() reads the status and returns the name of the next node.

03How the code does it

  • route_by_status() is a plain function: it reads state["status"] and returns the name of the next node.
  • add_conditional_edges("read_result", route_by_status, [...]) lists every possible destination, which also lets LangGraph draw the fork.
  • Anything that is not passed or failed falls through to quarantine, because of the .get(..., "quarantine") default.

Key points

  • The router returns a node's name
  • The diamond is a decision, not a node
  • This is where an agent makes choices

04Run it

terminal
cd chapter_18_LangGraph/src/chapters
python 003_Conditional_Edge.py
output
cart_total      passed  -> logged as green
checkout_pay    failed  -> Jira bug filed for checkout_pay
login_redirect  flaky   -> login_redirect moved to quarantine, rerun nightly