python
type_error
ai_generated
true
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'decimal.Decimal'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.
ID: python/pydantic-arbitrary-types-schema-generation
80%Fix Rate
89%Confidence
0Evidence
2024-02-08First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
Root Cause
The field type is a custom class (or a third-party type) that Pydantic v2 does not know how to validate, and no core schema has been registered.
generic中文
字段类型是 Pydantic v2 无法识别的自定义类或第三方类型,且未注册对应的 core schema。
Workarounds
-
95% success
from pydantic import BaseModel, ConfigDict class Foo(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) value: MyCustomClass -
90% success
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) -
85% success
from typing import Annotated from pydantic import BeforeValidator MyType = Annotated[decimal.Decimal, BeforeValidator(lambda v: decimal.Decimal(str(v)))]
Dead Ends
Common approaches that don't work:
-
55% fail
Pydantic v2 uses model_config = ConfigDict(...); the inner Config class is deprecated and ignored in strict v2 builds.
-
70% fail
Disables validation entirely, so invalid instances pass silently and errors surface later in business logic.
-
85% fail
NewType is a static-typing alias; at runtime it is just the underlying type and does not register a schema.