Skip to content
You're viewing docs for v2.7. See the latest version →

Annotated Handlers

Type annotations to use with __get_pydantic_core_schema__ and __get_pydantic_json_schema__.

GetJsonSchemaHandler

Handler to call into the next JSON schema generation function.

Attributes

mode

Type: JsonSchemaMode

Methods

call

def __call__(core_schema: CoreSchemaOrField) -> JsonSchemaValue

Call the inner handler and get the JsonSchemaValue it returns. This will call the next JSON schema modifying function up until it calls into pydantic.json_schema.GenerateJsonSchema, which will raise a pydantic.errors.PydanticInvalidForJsonSchema error if it cannot generate a JSON schema.

Returns

JsonSchemaValue — The JSON schema generated by the inner JSON schema modify JsonSchemaValue — functions.

Parameters

core_schema : CoreSchemaOrField

A pydantic_core.core_schema.CoreSchema.

resolve_ref_schema

def resolve_ref_schema(maybe_ref_json_schema: JsonSchemaValue) -> JsonSchemaValue

Get the real schema for a \{"$ref": ...\} schema. If the schema given is not a $ref schema, it will be returned as is. This means you don’t have to check before calling this function.

Returns

JsonSchemaValue — A JsonSchemaValue that has no $ref.

Parameters

maybe_ref_json_schema : JsonSchemaValue

A JsonSchemaValue which may be a $ref schema.

Raises
  • LookupError — If the ref is not found.

GetCoreSchemaHandler

Handler to call into the next CoreSchema schema generation function.

Attributes

field_name

Get the name of the closest field to this validator.

Type: str | None

Methods

call

def __call__(source_type: Any) -> core_schema.CoreSchema

Call the inner handler and get the CoreSchema it returns. This will call the next CoreSchema modifying function up until it calls into Pydantic’s internal schema generation machinery, which will raise a pydantic.errors.PydanticSchemaGenerationError error if it cannot generate a CoreSchema for the given source type.

Returns

core_schema.CoreSchema — The pydantic-core CoreSchema generated.

Parameters

source_type : Any

The input type.

generate_schema

def generate_schema(source_type: Any) -> core_schema.CoreSchema

Generate a schema unrelated to the current context. Use this function if e.g. you are handling schema generation for a sequence and want to generate a schema for its items. Otherwise, you may end up doing something like applying a min_length constraint that was intended for the sequence itself to its items!

Returns

core_schema.CoreSchema — The pydantic-core CoreSchema generated.

Parameters

source_type : Any

The input type.

resolve_ref_schema

def resolve_ref_schema(
    maybe_ref_schema: core_schema.CoreSchema,
) -> core_schema.CoreSchema

Get the real schema for a definition-ref schema. If the schema given is not a definition-ref schema, it will be returned as is. This means you don’t have to check before calling this function.

Returns

core_schema.CoreSchema — A concrete CoreSchema.

Parameters

maybe_ref_schema : core_schema.CoreSchema

A CoreSchema, ref-based or not.

Raises
  • LookupError — If the ref is not found.

JsonSchemaMode

A type alias that represents the mode of a JSON schema; either ‘validation’ or ‘serialization’.

For some types, the inputs to validation differ from the outputs of serialization. For example, computed fields will only be present when serializing, and should not be provided when validating. This flag provides a way to indicate whether you want the JSON schema required for validation inputs, or that will be matched by serialization outputs.

Default: Literal['validation', 'serialization']

JsonSchemaValue

A type alias for a JSON schema value. This is a dictionary of string keys to arbitrary JSON values.

Default: Dict[str, Any]

CoreSchemaOrField

Default: Union[core_schema.CoreSchema, core_schema.ModelField, core_schema.DataclassField, core_schema.TypedDictField, core_schema.ComputedField]