{
  "id": "python/fastapi-json-encode-error",
  "signature": "TypeError: Object of type datetime is not JSON serializable",
  "signature_zh": "类型错误：datetime 类型的对象不可 JSON 序列化",
  "regex": "TypeError:\\ Object\\ of\\ type\\ datetime\\ is\\ not\\ JSON\\ serializable",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "FastAPI 路径操作返回了 Python datetime 对象，但 JSON 编码器无法处理",
  "root_cause_type": "generic",
  "root_cause_zh": "FastAPI 路径操作返回了 Python datetime 对象，但 JSON 编码器无法处理",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "手动将 datetime 转为字符串但忽略时区",
      "why_fails": "可能导致时区信息丢失",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "使用 json.dumps 并设置默认参数",
      "why_fails": "FastAPI 使用自己的 JSON 编码器，自定义可能被覆盖",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "使用 Pydantic 模型并定义字段类型",
      "success_rate": 0.95,
      "how": "from pydantic import BaseModel\nclass Item(BaseModel):\n    created_at: datetime\n    class Config:\n        json_encoders = {datetime: lambda v: v.isoformat()}",
      "condition": "",
      "sources": []
    },
    {
      "action": "在返回前手动转换",
      "success_rate": 0.9,
      "how": "return {'created_at': datetime.now().isoformat()}",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-12-01",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}