python
data_error
ai_generated
true
pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.IsInstanceSchema (<class 'decimal.Decimal'>)
ID: python/pydantic-json-schema-generation-error
80%Fix Rate
86%Confidence
0Evidence
2024-06-21First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
Root Cause
A field uses an arbitrary Python type (via arbitrary_types_allowed or a custom class) that has no defined JSON Schema representation, so model_json_schema() fails.
generic中文
字段使用了没有定义 JSON Schema 表示的任意 Python 类型(通过 arbitrary_types_allowed 或自定义类),导致 model_json_schema() 失败。
Workarounds
-
90% success
from typing import Annotated from pydantic import BaseModel, WithJsonSchema class Model(BaseModel): amount: Annotated[Decimal, WithJsonSchema({'type': 'string', 'format': 'decimal'})] -
88% success
class Model(BaseModel): amount: float # or str; convert to Decimal in business logic
Dead Ends
Common approaches that don't work:
-
70% fail
Returns an empty schema, breaking OpenAPI docs and client generation downstream.
-
80% fail
This allows the type at validation time but does not teach Pydantic how to serialize it to JSON Schema.