{
  "id": "python/pydantic-orm-mode-from-attributes",
  "signature": "pydantic_core._pydantic_core.ValidationError: 1 validation error for UserOut\nid\n  Input should be a valid integer [type=int_type, input_value=<sqlalchemy.orm.attributes.InstrumentedAttribute object>, input_type=InstrumentedAttribute]",
  "signature_zh": "pydantic_core._pydantic_core.ValidationError：1 个验证错误（UserOut）\nid\n  输入应为有效的整数 [type=int_type, input_value=<sqlalchemy.orm.attributes.InstrumentedAttribute object>, input_type=InstrumentedAttribute]",
  "regex": "pydantic_core\\._pydantic_core\\.ValidationError:\\ 1\\ validation\\ error\\ for\\ UserOut\\\nid\\\n\\ \\ Input\\ should\\ be\\ a\\ valid\\ integer\\ \\[type=int_type,\\ input_value=<sqlalchemy\\.orm\\.attributes\\.InstrumentedAttribute\\ object>,\\ input_type=InstrumentedAttribute\\]",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Passing a SQLAlchemy model class (not an instance) to model_validate, or from_attributes not enabled so Pydantic reads the class attribute instead of the instance.",
  "root_cause_type": "generic",
  "root_cause_zh": "将 SQLAlchemy 模型类（而非实例）传给 model_validate，或未启用 from_attributes，导致 Pydantic 读取的是类属性而非实例属性。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "SQLAlchemy instances store state in _sa_instance_state; __dict__ includes internal keys and misses lazy relations.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "You're validating the class, not an instance; attributes are InstrumentedAttribute descriptors.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "In Pydantic v2 this raises a deprecation warning and is ignored unless from_attributes is set.",
      "fail_rate": 0.55,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "from pydantic import BaseModel, ConfigDict\nclass UserOut(BaseModel):\n    model_config = ConfigDict(from_attributes=True)\n    id: int\n    name: str\n\nUserOut.model_validate(db_user)  # db_user is a SQLAlchemy instance",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "UserOut(id=db_user.id, name=db_user.name)",
      "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.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-04-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}