Skip to content
Latchkey

Python "RecursionError" during serialization in CI

Recursive serialization (json, pickle, repr, deepcopy) walks an object graph. A reference cycle or very deep nesting overruns the interpreter recursion limit and raises RecursionError.

What this error means

A test fails with "RecursionError: maximum recursion depth exceeded" inside json.dumps, pickle.dumps, copy.deepcopy, or a custom __repr__.

python
RecursionError: maximum recursion depth exceeded while calling a Python object

Common causes

A reference cycle in the object graph

Two objects reference each other (or a list contains itself), so serialization never terminates.

Legitimately deep nesting

A tree or linked structure is deeper than the default recursion limit of 1000.

How to fix it

Break the cycle or convert to an iterative walk

  1. Find the cycle (objgraph or a manual visited-set walk) and replace one direction with an id or weakref.
  2. For json, implement a custom encoder that skips already-seen objects.
  3. Only as a last resort, raise the limit with sys.setrecursionlimit for genuinely deep but finite data.
Python
import json

class CycleSafeEncoder(json.JSONEncoder):
    def default(self, obj):
        return getattr(obj, "id", repr(obj))

How to prevent it

  • Avoid back-references in data classes that get serialized.
  • Use weakref or ids for parent pointers.
  • Test serialization of representative nested structures.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →