# TypeError: Object of type ObjectId is not JSON serializable

- **ID:** `python/fastapi-jsonable-encoder-error`
- **Domain:** python
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Returning MongoDB ObjectId or similar BSON types directly from a FastAPI route without converting them.

## Version Compatibility

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

## Workarounds

1. **** (90% success)
   ```
   Use Pydantic's Json type: from pydantic import Json\nclass Item(BaseModel):\n    id: Json[Any]
   ```
2. **** (95% success)
   ```
   Convert ObjectId to str in the route: item['_id'] = str(item['_id'])
   ```

## Dead Ends

- **** — Converting ObjectId to str manually works but loses the original type information. (60% fail)
- **** — Using json.dumps with a custom default function may not integrate with FastAPI's response model. (70% fail)
