GraphRecursionError
langchain.graphs.langgraph.GraphRecursionError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
result = graph.run(input_data)
File "/usr/local/lib/python3.9/site-packages/langchain/graphs/langgraph.py", line 210, in run
raise GraphRecursionError(f"Exceeded max recursion steps: {self.max_steps}")
langchain.graphs.langgraph.GraphRecursionError: Exceeded max recursion steps: 1000 Why it happens
LangChain's langgraph executes graph-based chains with recursion to resolve dependencies. If the graph contains cycles or the recursion depth exceeds the configured max_steps limit, the GraphRecursionError is raised to prevent infinite loops and stack overflow.
Detection
Monitor the recursion depth or step count during graph execution and catch GraphRecursionError to log the graph structure causing excessive recursion before failure.
Causes & fixes
The graph contains cyclic dependencies causing infinite recursion.
Analyze and remove cycles in the graph definition or add cycle detection logic before execution.
The max_steps parameter is set too low for a large or complex graph.
Increase the max_steps parameter in the langgraph configuration to allow deeper recursion.
Improper graph construction causing unintended repeated node calls.
Review graph node connections and ensure each node is called only as intended without redundant loops.
Code: broken vs fixed
from langchain.graphs.langgraph import LangGraph
graph = LangGraph(max_steps=1000)
# This will raise GraphRecursionError if recursion exceeds 1000 steps
result = graph.run(input_data) # triggers error here import os
from langchain.graphs.langgraph import LangGraph
# Use environment variable for max steps and increase limit
max_steps = int(os.environ.get('LANGCHAIN_MAX_STEPS', '5000'))
graph = LangGraph(max_steps=max_steps)
result = graph.run(input_data) # fixed by increasing max_steps
print(result) Workaround
Wrap the graph.run call in try/except GraphRecursionError, then log the graph nodes and edges to identify cycles and manually break them or limit recursion.
Prevention
Design acyclic graphs or implement cycle detection before execution, and configure max_steps according to graph complexity to avoid infinite recursion.