{
  "id": "python/pydantic-decimal-max-digits",
  "signature": "pydantic_core._pydantic_core.ValidationError: 1 validation error for Price\nvalue\n  Decimal input should have no more than 2 decimal places [type=decimal_max_places, input_value='1.2345', input_type=str]",
  "signature_zh": "pydantic_core._pydantic_core.ValidationError：1 个验证错误（Price）\nvalue\n  Decimal 输入的小数位数不应超过 2 位 [type=decimal_max_places, input_value='1.2345', input_type=str]",
  "regex": "pydantic_core\\._pydantic_core\\.ValidationError:\\ 1\\ validation\\ error\\ for\\ Price\\\nvalue\\\n\\ \\ Decimal\\ input\\ should\\ have\\ no\\ more\\ than\\ 2\\ decimal\\ places\\ \\[type=decimal_max_places,\\ input_value='1\\.2345',\\ input_type=str\\]",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "A Decimal field declared with max_digits/decimal_places received a value with more fractional digits than allowed.",
  "root_cause_type": "generic",
  "root_cause_zh": "声明了 max_digits/decimal_places 的 Decimal 字段接收到了小数位数超过限制的值。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Introduces binary floating-point rounding; 1.2345 becomes 1.2344999...",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Defeats the purpose and allows inconsistent money precision downstream.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Silently zeroes real prices, causing financial data corruption.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.92,
      "how": "from decimal import Decimal, ROUND_HALF_UP\nv = Decimal('1.2345').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)\nPrice(value=v)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "from pydantic import BaseModel, field_validator, Field\nfrom decimal import Decimal, ROUND_HALF_UP\nclass Price(BaseModel):\n    value: Decimal = Field(max_digits=10, decimal_places=2)\n    @field_validator('value', mode='before')\n    @classmethod\n    def quantize(cls, v):\n        return Decimal(str(v)).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)",
      "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-09-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}