AI / LangGraph LangChain Interview questions
How do you handle errors in LangGraph?
Error handling in LangGraph is explicit — errors in nodes are not automatically caught or retried. If a node raises an unhandled exception, the graph execution stops and the exception propagates to the caller. This is intentional: LangGraph wants you to be explicit about failure modes rather than silently swallowing errors.
Approach 1: try/except inside node functions — the most common pattern. Catch the error, add a diagnostic message to state, and route to an error-recovery node:
def call_tool(state: AgentState) -> dict:
try:
result = tool.invoke(state["tool_input"])
return {"messages": [ToolMessage(content=result, ...)]}
except Exception as e:
return {"messages": [ToolMessage(content=f"Error: {e}", ...)]}
Approach 2: error recovery edges — route to a dedicated error handler node using a conditional edge that inspects whether the last message signals an error:
def should_retry(state) -> str:
last = state["messages"][-1].content
if last.startswith("Error:"):
return "error_handler"
return "continue"
graph.add_conditional_edges("tool_node", should_retry, {"error_handler": "error_handler", "continue": "agent"})
For transient external service errors (rate limits, timeouts), wrap the relevant LangChain component with .with_retry() before using it inside a node.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
