# 类型错误：ObjectId类型的对象无法JSON序列化

- **ID:** `python/fastapi-jsonable-encoder-error`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在FastAPI路由中直接返回MongoDB ObjectId或类似的BSON类型，而没有进行转换。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

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

## 无效尝试

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