Python Error

RecursionError

RecursionError is raised when the maximum recursion depth is exceeded (default 1000). It's a subclass of RuntimeError and typically indicates infinite recursion or an algorithm that recurses too deeply.

Common causes

  • Recursive function without a proper base case
  • Circular references in __repr__ or __str__ methods
  • __getattr__ that triggers itself recursively
  • Recursive data structure traversal without cycle detection
  • Algorithm that requires more depth than the limit allows

How to fix it

  • Add or fix the base case in recursive functions
  • Convert recursion to iteration using a stack or queue
  • Increase limit with sys.setrecursionlimit() (use cautiously)
  • Add visited tracking for graph/tree traversals
  • Use functools.lru_cache to memoize and reduce recursion depth

Example

RecursionError example
# Error example
def countdown(n):
    print(n)
    countdown(n - 1)  # No base case!

countdown(10)
# RecursionError: maximum recursion depth exceeded

# Fix: add base case
def countdown(n):
    if n <= 0:
        return
    print(n)
    countdown(n - 1)

# Fix: convert to iteration
def countdown(n):
    while n > 0:
        print(n)
        n -= 1

# Fix: memoization for expensive recursion
from functools import lru_cache

@lru_cache(maxsize=None)
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)

Track RecursionError with Checkend

Checkend automatically captures RecursionError errors in your Python application with full context:

  • Complete traceback with syntax highlighting
  • Request context (URL, params, headers)
  • Automatic grouping of similar errors
  • Instant notifications when errors occur

Stop debugging in production

Get full error context and fix issues faster with self-hosted error tracking.