Build a LangGraph With a Conditional Edge
Build build_graph() returning a compiled LangGraph that:
- Starts at a node
work, which appends its name tologand incrementscount. - Routes conditionally: back to
workwhilecount < 3, otherwise to a nodefinishthat appends"finish"tolog. - Ends after
finish.
What you need from LangGraph:
- State is the
TypedDictevery node receives. A node returns only the keys it changed. - Reducers decide how an update merges. By default a returned key replaces the old value. In the
Statedefined above,logisAnnotated[list, operator.add], so a returned list appends;counthas no reducer, so it replaces. Mixing those up is the most common LangGraph bug. - Nodes are functions
state -> partial update. - Edges connect nodes.
add_edgeis unconditional;add_conditional_edgescalls a function that returns the name of the next node. That is how you branch and how you loop.
The loop must terminate. A cycle with no exit condition runs until LangGraph's recursion limit throws.