Pydantic AI · Chapter 14 of 23
Graph workflows
By the end of this chapter you can branch a graph on its state, run an agent inside a node, stop a run at a node that waits for a person, save the run and resume it from that node in another program, watch a run node by node, and say exactly what a saved run needs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Graphs
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.
Why you need this
Chapter 13's claim graph checks and files, in order, every time. Sneha now wants the rest of the real process in it. A claim above ₹50,000 must wait for the regional manager (rule 3.4), which takes a night. Farmers do not send neat records: they write "Hail destroyed 3 acres of my grapes", so the first step needs a model to read the message. And the auditor wants to see each step the run took, with what it knew at that moment.
So the graph must branch on what it has found, have one step that runs an agent, stop at the step that waits for a person, and continue tomorrow from that step without redoing the others. Chapter 12 did this for an agent's tool call. Here you do it for a workflow, and you will see that a graph run is even simpler to save.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
branches.py: branches
The state, a dataclass Case with a default for every field, in this order: farmer_id, crop, cause (text, empty to start), acres (0), loss_date and message (text, empty), amount (0), decision (None) and path (an empty list). Every node first adds its own name to the state's path.
Check: if the cause is not in khetbazaar.COVERED, end with Rejected: CAUSE is not covered (policy 3.5), with the cause; otherwise set the state's amount to int(acres * 24000) and go to Review when it is above 50000, else to File. Review: with no decision, end with Waiting for the regional manager; with the decision approve, go to File; with any other, end with Rejected by the regional manager. File: call khetbazaar.file_claim; if its ok is false, end with Rejected: and its reason, otherwise with Filed, the claim_id, for, and ₹ with the amount (Filed KC4019 for ₹36000).
Build the graph as graph, starting at Check. Under if __name__ == "__main__":, run these cases in order and print for each the path joined by a space, a greater-than sign and a space, then a colon, a space and the output: F1018, onion, hail, 1.5 acres, 2026-10-22; F1014, grape, hail, 3 acres, 2026-10-22, decision approve; F1019, onion, unseasonal rain, 4 acres, 2026-10-21, decision reject; F1009, tomato, hail, 4 acres, 2026-10-22. The check runs other cases too.
- Task 2
agent_node.py: an agent inside a node
Import Case, Check, Review and File from branches.py. Make a Pydantic model ClaimReport (farmer_id, crop, cause, acres as a float, loss_date) and an agent for openai-chat:lab-chat with output type ClaimReport and the instructions Read the farmer's crop loss message. Today is 2026-10-23.
Node Intake adds Intake to the path, runs the agent on the state's message, copies the report's five fields into the state, and goes to Check. Build the graph as graph, starting at Intake, with all four nodes.
Under if __name__ == "__main__":, for each message run the graph with a new Case holding only the message, and print the path and the output as in task 1. The messages: Hail damaged 1.5 acres of my onion on 22 Oct. I am F1021. and Hail destroyed 3 acres of my grapes on 22 Oct. I am F1014.
- Task 3
resume.py: resume after a stop
Build a graph with Intake (from agent_node.py) and Check, Review and File (from branches.py) whose start leads to Intake or to Review, chosen by the node you pass in, so it can begin at either.
With the words start and a message on the command line, run from Intake with a new Case holding the message and print the path line as in task 2. If the output is Waiting for the regional manager, save {"node": "Review", "state": the state as a dictionary} as JSON to pending.json in this folder and print Saved: pending.json.
With the word approve or reject, load pending.json, rebuild the Case from its state, put the word in the decision, run from Review, and print the path line. Resuming must not run Intake again. Try it with Pest attack destroyed 3 acres of my pomegranate on 21 Oct. I am F1016.; the check also tries a small claim, which must not be saved, and a rejection.
- Task 4 · interview question
Interview: snapshots.py, what the persistence stores
Run agent_node.py's graph step by step with graph.iter, from Intake, on a Case with the message Hail destroyed 3 acres of my grapes on 22 Oct. I am F1014. and the decision approve.
Before each node runs, add a snapshot {"node": the node's class name, "state": a copy of the state as a dictionary} to a list, and print the node's name, a colon, a space, amount, a space and the state's amount. When the run ends, print End: and the output. Write the list to history.json in this folder as JSON.
The interviewer will ask: what does a snapshot store, and what not? After a crash at File, where do you resume, and what could go wrong?
What you will learn
- Branching on state: a node that can go to several nodes.
- An agent inside a node: await agent.run(...), and the trap.
- Stopping and resuming: saving the state and the next node.
- A graph that can start at any of several nodes.
- Watching a run node by node with graph.iter.
More in Graphs
Build "Graph workflows" yourself
KhetBazaar, a Nashik agritech startup, answers farmers on WhatsApp from a bike. You build its farmer assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.