Errors in Nodes
Failing gracefully.
Some failures won't go away with a retry. The service is down for the day. Instead of crashing the whole graph, a node can catch the error, record it in the state, and let the graph take a fallback path.

- Some failures last
- Don't crash
- Record it, take a fallback
A fallback path
The live stock node catches the error, and writes it to the state, instead of raising. A routing function checks for an error. If there is one, the graph goes to a fallback node, which answers from yesterday's stock list. Run it. The live check fails, and the customer still gets a useful, honest answer.
from typing import TypedDict
from langgraph.graph import StateGraph, START
class State(TypedDict):
title: str
error: str
answer: str
def live_stock(s):
try:
raise TimeoutError("stock service down") # pretend the call failed
except TimeoutError as e:
return {"error": str(e)}
def fallback(s):
return {"answer": f"{s['title']}: 3 copies yesterday (live check unavailable)"}
b = StateGraph(State); b.add_node(live_stock); b.add_node(fallback); b.add_edge(START, "live_stock")
b.add_conditional_edges("live_stock", lambda s: "fallback" if s.get("error") else "__end__")
print(b.compile().invoke({"title": "Dune"})){'title': 'Dune', 'error': 'stock service down', 'answer': 'Dune: 3 copies yesterday
(live check unavailable)'}Error handling patterns
Retries for temporary errors. Fallback paths for lasting ones. Honest messages for the customer. And log every error, so you can fix the cause. Never let a raw stack trace reach the customer.
| key | value |
|---|---|
| Temporary | retry |
| Lasting | fallback path |
| Customer | honest message |
| Team | log the error |
Summary
Catch lasting errors in the node, record them in state, and route to a fallback. Next, skipping repeated work with node caching.
Key takeaways- Catch in the node
- Error in state
- Route to a fallback