# ValueError: Circular reference detected

- **ID:** `python/flask-jsonify-circular-reference`
- **Domain:** python
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Attempting to serialize a Python object with circular references (e.g., self-referential ORM models or graph structures) using Flask's jsonify, which lacks a resolver for cycles.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.x | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   Use a custom JSON encoder that tracks visited objects, or convert to a serializable dict by breaking cycles first. Example: `def serialize(obj, seen=None): if id(obj) in seen: return None; seen.add(id(obj)); return {k: serialize(v, seen) for k, v in obj.__dict__.items()}`
   ```
2. **** (85% success)
   ```
   Use a library like `flask-marshmallow` or `marshmallow` with `fields.Nested` and `exclude` to avoid cycles.
   ```

## Dead Ends

- **** — json.dumps with default=str only converts non-serializable objects to strings, but circular references cause infinite recursion before reaching that fallback. (95% fail)
- **** — Using Flask's app.json_encoder with a custom encoder does not automatically handle circular references; it still recurses infinitely unless explicitly checked. (90% fail)
