Nodes
Functions that update state.
A node is just a Python function. It receives the current state, does some work, and returns a dictionary of updates. Nothing more. That makes nodes easy to write, and easy to test.

- A Python function
- State in, updates out
- Easy to test
Nodes are plain functions
Here are two nodes for an order. One applies a member discount. The other adds delivery, which is free over twenty five pounds. Because nodes are plain functions, we can call them directly, without any graph. Run it. Each function returns just its update.
def apply_discount(state):
if state["member"]:
return {"total": round(state["total"] * 0.9, 2)}
return {}
def add_delivery(state):
fee = 0 if state["total"] > 25 else 2.99
return {"delivery": fee}
state = {"total": 30.0, "member": True}
print(apply_discount(state))
print(add_delivery({"total": 20.0}))
print(apply_discount({"total": 20.0, "member": False})){'total': 27.0}
{'delivery': 2.99}
{}Node rules
A node takes the state, and returns a dictionary. Return only the keys you change. Returning an empty dictionary means, no change. Keep each node to one job, and give it a name that says what it does.
| key | value |
|---|---|
| Input | the state |
| Output | a dict of updates |
| Empty dict | no change |
| One job | clear name |
Summary
Nodes are plain functions that return updates. You can test them without the graph. Next, the edges that connect them.
Key takeaways- Function(state)
- Returns updates
- Test directly