0
0
LangChainframework~10 mins

Conditional routing in graphs in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Conditional routing in graphs
Start at Entry Node
Evaluate Condition on Node
Follow Yes Edge
Next Node
End Node
The graph starts at an entry node, evaluates a condition, and routes to the next node based on Yes/No outcomes until reaching the end.
Execution Sample
LangChain
graph = {
  'start': lambda x: 'node1' if x > 0 else 'node2',
  'node1': lambda x: 'end',
  'node2': lambda x: 'end'
}

current = 'start'
while current != 'end':
  current = graph[current](5)
This code routes through a graph starting at 'start', choosing next nodes based on a condition until reaching 'end'.
Execution Table
StepCurrent NodeInput ValueCondition EvaluatedNext NodeAction Taken
1start55 > 0 is Truenode1Follow 'Yes' edge to node1
2node15No condition (direct to end)endMove to end node
3end-Reached end node-Stop routing
💡 Reached 'end' node, routing stops.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
currentstartnode1endend
input_value5555
Key Moments - 3 Insights
Why does the routing stop when current node is 'end'?
Because the execution_table row 3 shows 'end' node reached, which is the termination condition for routing.
What happens if the condition at 'start' node is False?
The routing follows the 'No' edge to 'node2' instead of 'node1', as shown by the condition evaluation in execution_table row 1.
Why do we evaluate conditions at each node?
To decide which path to take next, enabling dynamic routing based on input, as shown in execution_table steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 1?
Anode1
Bnode2
Cstart
Dend
💡 Hint
Check the 'current node' column in execution_table row 2.
At which step does the routing reach the 'end' node?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Current Node' column in execution_table to find when 'end' appears.
If the input value was -1, which node would be visited after 'start'?
Anode1
Bnode2
Cend
Dstart
💡 Hint
Refer to the condition 'x > 0' in execution_table row 1 and think about the False branch.
Concept Snapshot
Conditional routing in graphs:
- Start at an entry node
- Evaluate a condition at each node
- Follow edges based on condition outcome (Yes/No)
- Continue until reaching an end node
- Enables dynamic path selection based on input
Full Transcript
Conditional routing in graphs means starting at a specific node and deciding which path to take next by checking a condition at that node. If the condition is true, you follow one path; if false, another. This continues until you reach an end node, where routing stops. For example, starting at 'start', if the input is greater than zero, you go to 'node1'; otherwise, you go to 'node2'. Both 'node1' and 'node2' lead to 'end', which stops the routing. This method helps in making decisions step-by-step in a graph based on changing inputs.