python type_error ai_generated partial

pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.CallableSchema

ID: python/pydantic-json-schema-mode

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2024-10-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 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)
  2. 85% success
    class M(BaseModel):
        handler_name: str  # resolved at runtime via registry

Dead Ends

Common approaches that don't work:

  1. 50% fail

    Loses validation; JSON schema becomes empty object with no guidance.

  2. 60% fail

    Breaks OpenAPI docs and client SDK generation for the whole app.

  3. 70% fail

    Security risk and still no schema for the callable's shape.