python
type_error
ai_generated
true
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'numpy.ndarray'>. 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-type-not-allowed
80%Fix Rate
89%Confidence
0Evidence
2024-07-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
Root Cause
A field uses a third-party class that Pydantic does not know how to validate. Pydantic v2 requires either arbitrary_types_allowed or a custom core schema for unknown types.
generic中文
字段使用了 Pydantic 无法验证的第三方类。Pydantic v2 对未知类型要求设置 arbitrary_types_allowed 或自定义核心模式。
Workarounds
-
90% success
from pydantic import BaseModel, ConfigDict import numpy as np class Model(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) arr: np.ndarray -
88% success
from pydantic import field_serializer class Model(BaseModel): arr: np.ndarray @field_serializer('arr') def ser(self, v): return v.tolist()
Dead Ends
Common approaches that don't work:
-
85% fail
Pydantic still cannot build a schema for it; the error reappears at class definition or import time.
-
60% fail
Disables all validation and serialization for that field, losing type safety and JSON output.