python
type_error
ai_generated
partial
pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.CallableSchema
ID: python/pydantic-json-schema-mode
80%Fix Rate
82%Confidence
0Evidence
2024-10-11First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
Root Cause
A field is typed as Callable, which has no JSON schema representation. Calling model_json_schema() raises.
generic中文
字段类型为 Callable,无法映射到 JSON schema。调用 model_json_schema() 会抛错。
Workarounds
-
80% success
from pydantic import BaseModel, Field from typing import Callable class M(BaseModel): model_config = {'json_schema_extra': {'examples': []}} handler: Callable = Field(exclude=True) -
85% success
class M(BaseModel): handler_name: str # resolved at runtime via registry
Dead Ends
Common approaches that don't work:
-
50% fail
Loses validation; JSON schema becomes empty object with no guidance.
-
60% fail
Breaks OpenAPI docs and client SDK generation for the whole app.
-
70% fail
Security risk and still no schema for the callable's shape.