# pydantic.errors.PydanticSchemaGenerationError: 无法为 <class 'decimal.Decimal'> 生成 pydantic-core schema。请在 model_config 中设置 arbitrary_types_allowed=True 或实现 __get_pydantic_core_schema__。

- **ID:** `python/pydantic-arbitrary-types-schema-generation`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

字段类型是 Pydantic v2 无法识别的自定义类或第三方类型，且未注册对应的 core schema。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   from pydantic import BaseModel, ConfigDict
class Foo(BaseModel):
    model_config = ConfigDict(arbitrary_types_allowed=True)
    value: MyCustomClass
   ```
2. **** (90% 成功率)
   ```
   from pydantic import GetCoreSchemaHandler
from pydantic_core import core_schema
class MyCustomClass:
    @classmethod
    def __get_pydantic_core_schema__(cls, source, handler):
        return core_schema.no_info_plain_validator_function(cls.validate)
   ```
3. **** (85% 成功率)
   ```
   from typing import Annotated
from pydantic import BeforeValidator
MyType = Annotated[decimal.Decimal, BeforeValidator(lambda v: decimal.Decimal(str(v)))]
   ```

## 无效尝试

- **** — Pydantic v2 uses model_config = ConfigDict(...); the inner Config class is deprecated and ignored in strict v2 builds. (55% 失败率)
- **** — Disables validation entirely, so invalid instances pass silently and errors surface later in business logic. (70% 失败率)
- **** — NewType is a static-typing alias; at runtime it is just the underlying type and does not register a schema. (85% 失败率)
