How to Fix: RecursionError: maximum recursion depth exceeded

3D illustration of an infinite mirror reflection cracking, representing the RecursionError maximum depth exceeded.

This error means a function is calling itself over and over again, in an infinite loop, until it hits a safety limit set by Python (usually 1,000 calls deep). This is when you encounter a RecursionError.

This is called Infinite Recursion.

⚡ Quick Fix: RecursionError: maximum recursion depth exceeded — Python Base Case Fix and sys.setrecursionlimit() for Deep Recursive Algorithms

Your recursive function has no stop condition — it calls itself forever until Python’s 1,000-call safety limit fires and crashes the program.

# WRONG — no base case, function calls itself infinitely
def count_down(n):
    print(n)
    count_down(n - 1)    # RecursionError: maximum recursion depth exceeded

count_down(5)

# RIGHT — base case stops the recursion before the limit hits
def count_down(n):
    if n < 0:            # base case: stop here
        return
    print(n)
    count_down(n - 1)    # recursive call only runs when n >= 0

count_down(5)            # Output: 5, 4, 3, 2, 1, 0

# SPECIAL CASE — valid deep recursion on large datasets (trees, graphs)
import sys
sys.setrecursionlimit(5000)   # raise the limit only when the algorithm is correct

The breakdown below covers the missing base case and the legitimate deep recursion scenario — including when to convert recursion to an iterative loop instead.

The Cause

A “recursive function” is a function that calls itself. This is a powerful technique, but it must have a “base case”—a condition that tells it when to stop.

Problem Code (Missing Base Case): Let’s write a function to count down to 0.

def count_down(n):
    print(n)
    count_down(n - 1) # Function calls itself

count_down(5)
# Output:
# 5
# 4
# 3
# ...
# 0
# -1
# -2
# ... (CRASH!) RecursionError

It never knew when to stop!

The Fix: Add a “Base Case”

We need to add an if statement that says, “When you reach 0, just return and stop calling yourself.”

def count_down(n):
    # BASE CASE:
    if n < 0:
        return
    
    print(n)
    count_down(n - 1)
count_down(5)
# Output:
# 5
# 4
# 3
# 2
# 1
# 0
# (No crash)

If you get this error, look at your function and ask: “What is my stop condition?”


RecursionError: maximum recursion depth exceeded — Base Case, Stack Limit, and the Iterative Alternative

RecursionError: maximum recursion depth exceeded fires at 1,000 calls by default. Python stops there to protect against stack overflow — not because 1,000 is a magic number, but because an infinite loop would consume all available memory without it.

Every recursive function needs two things: a recursive call that moves toward the stopping point, and a base case that returns without calling itself. If either is missing or wrong, the function never terminates.

Check your base case first. Ask: does the condition actually get reached given the input I’m passing? A count_down that decrements by 1 reaches n < 0 in exactly n+1 steps. A Fibonacci function with no base case for n == 0 or n == 1 recurses forever. Add a print(n) at the top of the function to watch the value change — if it moves away from the base case instead of toward it, the recursive call is wrong.

Raise sys.setrecursionlimit() only when the algorithm is correct and the input genuinely requires deeper nesting — traversing a large file system tree, processing deeply nested JSON, or implementing graph traversal on a dataset with thousands of nodes. Raising the limit on a broken recursive function just delays the crash.

import sys
sys.setrecursionlimit(5000)

Convert to an iterative loop when the recursion depth scales with input size. A while loop with an explicit stack replaces most recursive tree and graph traversals, runs faster, uses less memory, and never hits a depth limit.

Iterative countdown — no recursion, no depth limit

def count_down(n):
while n >= 0:
print(n)
n -= 1

count_down(5) # Output: 5, 4, 3, 2, 1, 0

Similar Posts

Leave a Reply