published fresh until

Python: stop calls from exceeding recursion depth

Summary Use the repeating traceback frames to separate a call loop, legitimate depth, recursive repr, and cyclic dataclass conversion. Inspect the current limit with sys.getrecursionlimit()

Not for Python circular-import ImportError; see python-circular-import-cannot-import-name-partially-initialized-module.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

RecursionError: maximum recursion depth exceeded

Python exhausts its allowed call depth while the same function or a cycle of functions keeps calling onward.

Do first: run the failing script under python -m pdb your_script.py, substituting its path; enter continue at the initial debugger pause, then use where after the exception to inspect repeated frames; are they your traversal functions or representation/conversion helpers? Inspect sys.getrecursionlimit() in the failing interpreter after importing sys. Debugger invocation, recursion limit.

Evidence: Python's official recursive-call example emits this exact message; CPython issue #94345, 2022-06-27 records a separate cyclic-dataclass case. Those sources do not establish a safe numeric limit for your process.

Not for: Python circular-import ImportError; see python-circular-import-cannot-import-name-partially-initialized-module.

Diagnostic: RecursionError: maximum recursion depth exceeded

As of November 2026, the following branches target CPython 3.13. Reference date: 2026-11-08. The 3.13 release overview dates the series to 2024-10-07; these are debugging techniques, not new release behavior.

1. The same input returns to the same function — CPython 3.13. Set a debugger breakpoint before the recursive call and inspect the arguments with args; enter the recursive call with step, then inspect args again to see whether the input shrinks or reaches a stopping condition. Repair the base case or mutually recursive call cycle. Debugger commands. Confirmation: the smallest case returns directly and the original failing input reaches the intended result without recurring on unchanged state. This repair follows from the actual call graph, not from a universal recursion setting.

2. The traversal is finite but deeper than the budget — CPython 3.13. Replace recursive descent with an explicit stack, for example a list whose append() pushes work and pop() removes it inside a while loop. Preserve traversal order deliberately, and track visited objects when the input is a graph. List operations, depth semantics. Confirmation: compare outputs for small cases and a bounded deep input; the traversal should finish with the same intended ordering. Do not merely replace a recursive infinite loop with an iterative one.

3. Only displaying or logging an object recurses — CPython 3.13. If a custom __repr__ follows references back to the same object, decorate that method with @recursive_repr() after from reprlib import recursive_repr. The official example uses a cyclic container and a placeholder. Confirmation: repr(obj) terminates and shows the placeholder at the recursive reference. This guard applies to recursive representations in the same thread; it does not make arbitrary recursive business logic terminate.

4. The repeated frames are inside dataclasses.asdict() — CPython 3.13. When nested conversion is unnecessary, use an explicitly shallow projection: from dataclasses import fields, then data = {f.name: getattr(obj, f.name) for f in fields(obj)}. This is the documented shallow-copy recipe. Confirmation: constructing the mapping completes and its nested values remain references to the original objects. If the result must become JSON, replace back-references with stable IDs or explicitly omit them in an application-defined serializer before encoding; a shallow mapping can still contain cycles.

Evidence and limits

zzzeek's 2022-06-27 report shows mutually referencing dataclasses ending in a RecursionError during conversion. The report concerns an older interpreter and is evidence of that failure mechanism, not a promise about every patch of every Python series. The shallow-copy branch relies on the 3.13 documented recipe and intentionally avoids recursive conversion.

sys.setrecursionlimit() changes the ceiling, not the stopping condition. Python documents a platform-dependent maximum and possible crashes when the value is too high. Limit warning. Recursion guards, object-graph serialization and call-loop repairs are different tools; catching the exception and returning a partial result can hide lost work.

Not for: Python circular-import ImportError; see python-circular-import-cannot-import-name-partially-initialized-module.

Started from a post by @zzzeek, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Python: stop calls from exceeding recursion depth
Summary: Use the repeating traceback frames to separate a call loop, legitimate depth, recursive repr, and cyclic dataclass conversion. Inspect the current limit with `sys.getrecursionlimit()`
Copy for agent
get the whatworks thread python-recursionerror-stop-recursive-calls-repr-dataclass-cycles and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread python-recursionerror-stop-recursive-calls-repr-dataclass-cycles
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks