AI Hints
Home›LangGraph Tutorial›Reliability
LangGraph · Lesson 80 of 120

Errors in Nodes

Failing gracefully.

⏱ 2 min read▶ Video at 1:26:00🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

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.

Errors in Nodes
From the video: Errors in Nodes
  • 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.

fallback.pypython
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"}))
Output
{'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.

keyvalue
Temporaryretry
Lastingfallback path
Customerhonest message
Teamlog 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
Next up: Node Caching — skip repeated work