{
  "id": "python/pydantic-computed-field-serialization",
  "signature": "pydantic.errors.PydanticUserError: @computed_field should be used with @property. This error is raised when a @computed_field is applied to a non-property.",
  "signature_zh": "pydantic.errors.PydanticUserError：@computed_field 应与 @property 搭配使用。当 @computed_field 应用于非属性时，会引发此错误。",
  "regex": "pydantic\\.errors\\.PydanticUserError:\\ @computed_field\\ should\\ be\\ used\\ with\\ @property\\.\\ This\\ error\\ is\\ raised\\ when\\ a\\ @computed_field\\ is\\ applied\\ to\\ a\\ non\\-property\\.",
  "domain": "python",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "Decorator order was reversed: @computed_field placed above a plain method without @property, or @property placed above @computed_field.",
  "root_cause_type": "generic",
  "root_cause_zh": "装饰器顺序颠倒：@computed_field 放在了没有 @property 的普通方法之上，或 @property 放在了 @computed_field 之上。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Field no longer appears in model_dump / JSON output.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "staticmethod returns a descriptor; validation raises because it is not a property.",
      "fail_rate": 0.55,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Recomputes only at construction; stale if inputs mutate.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "from pydantic import BaseModel, computed_field\nclass M(BaseModel):\n    a: int\n    b: int\n    @computed_field\n    @property\n    def total(self) -> int:\n        return self.a + self.b",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "from pydantic import model_serializer\nclass M(BaseModel):\n    a: int\n    @model_serializer\n    def ser(self):\n        return {'a': self.a, 'double': self.a * 2}",
      "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.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-07-08",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}