Why This Happens
JSON is tree-structured and cannot represent circular references. This often occurs with ORM objects that have bidirectional relationships.
The Problem
import json
a = {'name': 'A'}
b = {'name': 'B', 'ref': a}
a['ref'] = b
json.dumps(a)The Fix
import json
a = {'name': 'A'}
b = {'name': 'B', 'ref': a}
# Select specific fields instead:
json.dumps({'name': a['name']})Step-by-Step Fix
- 1
Remove circular references
Break cycles before serialization.
- 2
Select specific fields
Build a dict with only needed fields.
- 3
Use a custom encoder
Track seen objects and handle cycles.
Got the actual stack trace?
Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.
Explain my errorBugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free