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.
More Related questions...