Skip to content

Core

plateforme.core.schema.core

This module provides utilities for managing core schema within the Plateforme framework. It extends Pydantic's core schema features.

GetCoreSchemaHandler

Handler to call into the next CoreSchema schema generation function.

field_name property

field_name: str | None

Get the name of the closest field to this validator.

generate_schema

generate_schema(__source_type: Any) -> 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!

Parameters:

Name Type Description Default
__source_type Any

The input type.

required

Returns:

Name Type Description
CoreSchema CoreSchema

The pydantic-core CoreSchema generated.

Source code in .venv/lib/python3.12/site-packages/pydantic/annotated_handlers.py
def generate_schema(self, __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!

    Args:
        __source_type: The input type.

    Returns:
        CoreSchema: The `pydantic-core` CoreSchema generated.
    """
    raise NotImplementedError

resolve_ref_schema

resolve_ref_schema(
    __maybe_ref_schema: CoreSchema,
) -> 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.

Parameters:

Name Type Description Default
__maybe_ref_schema CoreSchema

A CoreSchema, ref-based or not.

required

Raises:

Type Description
LookupError

If the ref is not found.

Returns:

Type Description
CoreSchema

A concrete CoreSchema.

Source code in .venv/lib/python3.12/site-packages/pydantic/annotated_handlers.py
def resolve_ref_schema(self, __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.

    Args:
        __maybe_ref_schema: A `CoreSchema`, `ref`-based or not.

    Raises:
        LookupError: If the `ref` is not found.

    Returns:
        A concrete `CoreSchema`.
    """
    raise NotImplementedError

GetJsonSchemaHandler

Handler to call into the next JSON schema generation function.

Attributes:

Name Type Description
mode JsonSchemaMode

Json schema mode, can be validation or serialization.

resolve_ref_schema

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.

Parameters:

Name Type Description Default
__maybe_ref_json_schema JsonSchemaValue

A JsonSchemaValue which may be a $ref schema.

required

Raises:

Type Description
LookupError

If the ref is not found.

Returns:

Name Type Description
JsonSchemaValue JsonSchemaValue

A JsonSchemaValue that has no $ref.

Source code in .venv/lib/python3.12/site-packages/pydantic/annotated_handlers.py
def resolve_ref_schema(self, __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.

    Args:
        __maybe_ref_json_schema: A JsonSchemaValue which may be a `$ref` schema.

    Raises:
        LookupError: If the ref is not found.

    Returns:
        JsonSchemaValue: A JsonSchemaValue that has no `$ref`.
    """
    raise NotImplementedError

PydanticOmit

Bases: builtins.Exception

PydanticUseDefault

Bases: builtins.Exception

Some

value property

value: _T

ValidationError

Bases: builtins.ValueError

ValidationError is the exception raised by pydantic-core when validation fails, it contains a list of errors which detail why validation failed.

title property

title: str

error_count method descriptor

error_count() -> int

Returns:

Type Description
int

The number of errors in the validation error.

errors method descriptor

errors(
    *,
    include_url: bool = True,
    include_context: bool = True,
    include_input: bool = True,
) -> list[ErrorDetails]

Details about each error in the validation error.

Parameters:

Name Type Description Default
include_url bool

Whether to include a URL to documentation on the error each error.

True
include_context bool

Whether to include the context of each error.

True
include_input bool

Whether to include the input value of each error.

True

Returns:

Type Description
list[ErrorDetails]

A list of ErrorDetails for each error in the validation error.

from_exception_data staticmethod

from_exception_data(
    title: str,
    line_errors: list[InitErrorDetails],
    input_type: Literal["python", "json"] = "python",
    hide_input: bool = False,
) -> ValidationError

Python constructor for a Validation Error.

The API for constructing validation errors will probably change in the future, hence the static method rather than __init__.

Parameters:

Name Type Description Default
title str

The title of the error, as used in the heading of str(validation_error)

required
line_errors list[InitErrorDetails]

A list of InitErrorDetails which contain information about errors that occurred during validation.

required
input_type Literal['python', 'json']

Whether the error is for a Python object or JSON.

'python'
hide_input bool

Whether to hide the input value in the error message.

False

json method descriptor

json(
    *,
    indent: int | None = None,
    include_url: bool = True,
    include_context: bool = True,
    include_input: bool = True,
) -> str

Same as errors() but returns a JSON string.

Parameters:

Name Type Description Default
indent int | None

The number of spaces to indent the JSON by, or None for no indentation - compact JSON.

None
include_url bool

Whether to include a URL to documentation on the error each error.

True
include_context bool

Whether to include the context of each error.

True
include_input bool

Whether to include the input value of each error.

True

Returns:

Type Description
str

a JSON string.

CoreConfig

Bases: TypedDict

Base class for schema configuration options.

Attributes:

Name Type Description
title str

The name of the configuration.

strict bool

Whether the configuration should strictly adhere to specified rules.

extra_fields_behavior ExtraBehavior

The behavior for handling extra fields.

typed_dict_total bool

Whether the TypedDict should be considered total. Default is True.

from_attributes bool

Whether to use attributes for models, dataclasses, and tagged union keys.

loc_by_alias bool

Whether to use the used alias (or first alias for "field required" errors) instead of field_names to construct error locs. Default is True.

revalidate_instances Literal['always', 'never', 'subclass-instances']

Whether instances of models and dataclasses should re-validate. Default is 'never'.

validate_default bool

Whether to validate default values during validation. Default is False.

populate_by_name bool

Whether an aliased field may be populated by its name as given by the model attribute, as well as the alias. (Replaces 'allow_population_by_field_name' in Pydantic v1.) Default is False.

str_max_length int

The maximum length for string fields.

str_min_length int

The minimum length for string fields.

str_strip_whitespace bool

Whether to strip whitespace from string fields.

str_to_lower bool

Whether to convert string fields to lowercase.

str_to_upper bool

Whether to convert string fields to uppercase.

allow_inf_nan bool

Whether to allow infinity and NaN values for float fields. Default is True.

ser_json_timedelta Literal['iso8601', 'float']

The serialization option for timedelta values. Default is 'iso8601'.

ser_json_bytes Literal['utf8', 'base64', 'hex']

The serialization option for bytes values. Default is 'utf8'.

ser_json_inf_nan Literal['null', 'constants']

The serialization option for infinity and NaN values in float fields. Default is 'null'.

hide_input_in_errors bool

Whether to hide input data from ValidationError representation.

validation_error_cause bool

Whether to add user-python excs to the cause of a ValidationError. Requires exceptiongroup backport pre Python 3.11.

coerce_numbers_to_str bool

Whether to enable coercion of any Number type to str (not applicable in strict mode).

regex_engine Literal['rust-regex', 'python-re']

The regex engine to use for regex pattern validation. Default is 'rust-regex'. See StringSchema.

ValidationInfo

Bases: Protocol

Argument passed to validation functions.

context property

context: Any | None

Current validation context.

config property

config: CoreConfig | None

The CoreConfig that applies to this validation.

mode property

mode: Literal['python', 'json']

The type of input data we are currently validating

data property

data: Dict[str, Any]

The data being validated for this model.

field_name property

field_name: str | None

The name of the current field being validated if this validator is attached to a model field.

ValidationMode

Bases: IntEnum

An enumeration of schema validation modes.

DISABLED class-attribute instance-attribute

DISABLED = 0

The validation is disabled.

DEFAULT class-attribute instance-attribute

DEFAULT = 1

The validation is enabled.

STRICT class-attribute instance-attribute

STRICT = 2

The validation is strictly enforced.

RecursionState dataclass

RecursionState(
    state: frozenset[str],
    mode: Literal["lax", "omit", "raise"],
)

A data class to represent the recursion context state for validation.

This class is used to store the recursion state for validation. It contains the set of recursive guard values and the mode for handling recursion errors.

Attributes:

Name Type Description
state frozenset[str]

The recursive guard frozen set values that are used to prevent infinite recursion. It initializes as an empty set.

mode Literal['lax', 'omit', 'raise']

The mode for handling recursion errors. It can be either 'raise' to raise a validation error or 'omit' to skip the validation and omit the source value. Defaults to 'omit'.

any_schema

any_schema(
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> AnySchema

Returns a schema that matches any value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.any_schema()
v = SchemaValidator(schema)
assert v.validate_python(1) == 1

Parameters:

Name Type Description Default
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def any_schema(*, ref: str | None = None, metadata: Any = None, serialization: SerSchema | None = None) -> AnySchema:
    """
    Returns a schema that matches any value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.any_schema()
    v = SchemaValidator(schema)
    assert v.validate_python(1) == 1
    ```

    Args:
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(type='any', ref=ref, metadata=metadata, serialization=serialization)

arguments_parameter

arguments_parameter(
    name: str,
    schema: CoreSchema,
    *,
    mode: Literal[
        "positional_only",
        "positional_or_keyword",
        "keyword_only",
    ]
    | None = None,
    alias: str
    | list[str | int]
    | list[list[str | int]]
    | None = None,
) -> ArgumentsParameter

Returns a schema that matches an argument parameter, e.g.:

from pydantic_core import SchemaValidator, core_schema

param = core_schema.arguments_parameter(
    name='a', schema=core_schema.str_schema(), mode='positional_only'
)
schema = core_schema.arguments_schema([param])
v = SchemaValidator(schema)
assert v.validate_python(('hello',)) == (('hello',), {})

Parameters:

Name Type Description Default
name str

The name to use for the argument parameter

required
schema CoreSchema

The schema to use for the argument parameter

required
mode Literal['positional_only', 'positional_or_keyword', 'keyword_only'] | None

The mode to use for the argument parameter

None
alias str | list[str | int] | list[list[str | int]] | None

The alias to use for the argument parameter

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def arguments_parameter(
    name: str,
    schema: CoreSchema,
    *,
    mode: Literal['positional_only', 'positional_or_keyword', 'keyword_only'] | None = None,
    alias: str | list[str | int] | list[list[str | int]] | None = None,
) -> ArgumentsParameter:
    """
    Returns a schema that matches an argument parameter, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    param = core_schema.arguments_parameter(
        name='a', schema=core_schema.str_schema(), mode='positional_only'
    )
    schema = core_schema.arguments_schema([param])
    v = SchemaValidator(schema)
    assert v.validate_python(('hello',)) == (('hello',), {})
    ```

    Args:
        name: The name to use for the argument parameter
        schema: The schema to use for the argument parameter
        mode: The mode to use for the argument parameter
        alias: The alias to use for the argument parameter
    """
    return _dict_not_none(name=name, schema=schema, mode=mode, alias=alias)

arguments_schema

arguments_schema(
    arguments: list[ArgumentsParameter],
    *,
    populate_by_name: bool | None = None,
    var_args_schema: CoreSchema | None = None,
    var_kwargs_schema: CoreSchema | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> ArgumentsSchema

Returns a schema that matches an arguments schema, e.g.:

from pydantic_core import SchemaValidator, core_schema

param_a = core_schema.arguments_parameter(
    name='a', schema=core_schema.str_schema(), mode='positional_only'
)
param_b = core_schema.arguments_parameter(
    name='b', schema=core_schema.bool_schema(), mode='positional_only'
)
schema = core_schema.arguments_schema([param_a, param_b])
v = SchemaValidator(schema)
assert v.validate_python(('hello', True)) == (('hello', True), {})

Parameters:

Name Type Description Default
arguments list[ArgumentsParameter]

The arguments to use for the arguments schema

required
populate_by_name bool | None

Whether to populate by name

None
var_args_schema CoreSchema | None

The variable args schema to use for the arguments schema

None
var_kwargs_schema CoreSchema | None

The variable kwargs schema to use for the arguments schema

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def arguments_schema(
    arguments: list[ArgumentsParameter],
    *,
    populate_by_name: bool | None = None,
    var_args_schema: CoreSchema | None = None,
    var_kwargs_schema: CoreSchema | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> ArgumentsSchema:
    """
    Returns a schema that matches an arguments schema, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    param_a = core_schema.arguments_parameter(
        name='a', schema=core_schema.str_schema(), mode='positional_only'
    )
    param_b = core_schema.arguments_parameter(
        name='b', schema=core_schema.bool_schema(), mode='positional_only'
    )
    schema = core_schema.arguments_schema([param_a, param_b])
    v = SchemaValidator(schema)
    assert v.validate_python(('hello', True)) == (('hello', True), {})
    ```

    Args:
        arguments: The arguments to use for the arguments schema
        populate_by_name: Whether to populate by name
        var_args_schema: The variable args schema to use for the arguments schema
        var_kwargs_schema: The variable kwargs schema to use for the arguments schema
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='arguments',
        arguments_schema=arguments,
        populate_by_name=populate_by_name,
        var_args_schema=var_args_schema,
        var_kwargs_schema=var_kwargs_schema,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

bool_schema

bool_schema(
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> BoolSchema

Returns a schema that matches a bool value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.bool_schema()
v = SchemaValidator(schema)
assert v.validate_python('True') is True

Parameters:

Name Type Description Default
strict bool | None

Whether the value should be a bool or a value that can be converted to a bool

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def bool_schema(
    strict: bool | None = None, ref: str | None = None, metadata: Any = None, serialization: SerSchema | None = None
) -> BoolSchema:
    """
    Returns a schema that matches a bool value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.bool_schema()
    v = SchemaValidator(schema)
    assert v.validate_python('True') is True
    ```

    Args:
        strict: Whether the value should be a bool or a value that can be converted to a bool
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(type='bool', strict=strict, ref=ref, metadata=metadata, serialization=serialization)

bytes_schema

bytes_schema(
    *,
    max_length: int | None = None,
    min_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> BytesSchema

Returns a schema that matches a bytes value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.bytes_schema(max_length=10, min_length=2)
v = SchemaValidator(schema)
assert v.validate_python(b'hello') == b'hello'

Parameters:

Name Type Description Default
max_length int | None

The value must be at most this length

None
min_length int | None

The value must be at least this length

None
strict bool | None

Whether the value should be a bytes or a value that can be converted to a bytes

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def bytes_schema(
    *,
    max_length: int | None = None,
    min_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> BytesSchema:
    """
    Returns a schema that matches a bytes value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.bytes_schema(max_length=10, min_length=2)
    v = SchemaValidator(schema)
    assert v.validate_python(b'hello') == b'hello'
    ```

    Args:
        max_length: The value must be at most this length
        min_length: The value must be at least this length
        strict: Whether the value should be a bytes or a value that can be converted to a bytes
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='bytes',
        max_length=max_length,
        min_length=min_length,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

call_schema

call_schema(
    arguments: CoreSchema,
    function: Callable[..., Any],
    *,
    function_name: str | None = None,
    return_schema: CoreSchema | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> CallSchema

Returns a schema that matches an arguments schema, then calls a function, e.g.:

from pydantic_core import SchemaValidator, core_schema

param_a = core_schema.arguments_parameter(
    name='a', schema=core_schema.str_schema(), mode='positional_only'
)
param_b = core_schema.arguments_parameter(
    name='b', schema=core_schema.bool_schema(), mode='positional_only'
)
args_schema = core_schema.arguments_schema([param_a, param_b])

schema = core_schema.call_schema(
    arguments=args_schema,
    function=lambda a, b: a + str(not b),
    return_schema=core_schema.str_schema(),
)
v = SchemaValidator(schema)
assert v.validate_python((('hello', True))) == 'helloFalse'

Parameters:

Name Type Description Default
arguments CoreSchema

The arguments to use for the arguments schema

required
function Callable[..., Any]

The function to use for the call schema

required
function_name str | None

The function name to use for the call schema, if not provided function.__name__ is used

None
return_schema CoreSchema | None

The return schema to use for the call schema

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def call_schema(
    arguments: CoreSchema,
    function: Callable[..., Any],
    *,
    function_name: str | None = None,
    return_schema: CoreSchema | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> CallSchema:
    """
    Returns a schema that matches an arguments schema, then calls a function, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    param_a = core_schema.arguments_parameter(
        name='a', schema=core_schema.str_schema(), mode='positional_only'
    )
    param_b = core_schema.arguments_parameter(
        name='b', schema=core_schema.bool_schema(), mode='positional_only'
    )
    args_schema = core_schema.arguments_schema([param_a, param_b])

    schema = core_schema.call_schema(
        arguments=args_schema,
        function=lambda a, b: a + str(not b),
        return_schema=core_schema.str_schema(),
    )
    v = SchemaValidator(schema)
    assert v.validate_python((('hello', True))) == 'helloFalse'
    ```

    Args:
        arguments: The arguments to use for the arguments schema
        function: The function to use for the call schema
        function_name: The function name to use for the call schema, if not provided `function.__name__` is used
        return_schema: The return schema to use for the call schema
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='call',
        arguments_schema=arguments,
        function=function,
        function_name=function_name,
        return_schema=return_schema,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

callable_schema

callable_schema(
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> CallableSchema

Returns a schema that checks if a value is callable, equivalent to python's callable method, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.callable_schema()
v = SchemaValidator(schema)
v.validate_python(min)

Parameters:

Name Type Description Default
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def callable_schema(
    *, ref: str | None = None, metadata: Any = None, serialization: SerSchema | None = None
) -> CallableSchema:
    """
    Returns a schema that checks if a value is callable, equivalent to python's `callable` method, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.callable_schema()
    v = SchemaValidator(schema)
    v.validate_python(min)
    ```

    Args:
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(type='callable', ref=ref, metadata=metadata, serialization=serialization)

chain_schema

chain_schema(
    steps: list[CoreSchema],
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> ChainSchema

Returns a schema that chains the provided validation schemas, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(v: str, info: core_schema.ValidationInfo) -> str:
    assert 'hello' in v
    return v + ' world'

fn_schema = core_schema.with_info_plain_validator_function(function=fn)
schema = core_schema.chain_schema(
    [fn_schema, fn_schema, fn_schema, core_schema.str_schema()]
)
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello world world world'

Parameters:

Name Type Description Default
steps list[CoreSchema]

The schemas to chain

required
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def chain_schema(
    steps: list[CoreSchema], *, ref: str | None = None, metadata: Any = None, serialization: SerSchema | None = None
) -> ChainSchema:
    """
    Returns a schema that chains the provided validation schemas, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(v: str, info: core_schema.ValidationInfo) -> str:
        assert 'hello' in v
        return v + ' world'

    fn_schema = core_schema.with_info_plain_validator_function(function=fn)
    schema = core_schema.chain_schema(
        [fn_schema, fn_schema, fn_schema, core_schema.str_schema()]
    )
    v = SchemaValidator(schema)
    assert v.validate_python('hello') == 'hello world world world'
    ```

    Args:
        steps: The schemas to chain
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(type='chain', steps=steps, ref=ref, metadata=metadata, serialization=serialization)

computed_field

computed_field(
    property_name: str,
    return_schema: CoreSchema,
    *,
    alias: str | None = None,
    metadata: Any = None,
) -> ComputedField

ComputedFields are properties of a model or dataclass that are included in serialization.

Parameters:

Name Type Description Default
property_name str

The name of the property on the model or dataclass

required
return_schema CoreSchema

The schema used for the type returned by the computed field

required
alias str | None

The name to use in the serialized output

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def computed_field(
    property_name: str, return_schema: CoreSchema, *, alias: str | None = None, metadata: Any = None
) -> ComputedField:
    """
    ComputedFields are properties of a model or dataclass that are included in serialization.

    Args:
        property_name: The name of the property on the model or dataclass
        return_schema: The schema used for the type returned by the computed field
        alias: The name to use in the serialized output
        metadata: Any other information you want to include with the schema, not used by pydantic-core
    """
    return _dict_not_none(
        type='computed-field', property_name=property_name, return_schema=return_schema, alias=alias, metadata=metadata
    )

custom_error_schema

custom_error_schema(
    schema: CoreSchema,
    custom_error_type: str,
    *,
    custom_error_message: str | None = None,
    custom_error_context: dict[str, Any] | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> CustomErrorSchema

Returns a schema that matches a custom error value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.custom_error_schema(
    schema=core_schema.int_schema(),
    custom_error_type='MyError',
    custom_error_message='Error msg',
)
v = SchemaValidator(schema)
v.validate_python(1)

Parameters:

Name Type Description Default
schema CoreSchema

The schema to use for the custom error schema

required
custom_error_type str

The custom error type to use for the custom error schema

required
custom_error_message str | None

The custom error message to use for the custom error schema

None
custom_error_context dict[str, Any] | None

The custom error context to use for the custom error schema

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def custom_error_schema(
    schema: CoreSchema,
    custom_error_type: str,
    *,
    custom_error_message: str | None = None,
    custom_error_context: dict[str, Any] | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> CustomErrorSchema:
    """
    Returns a schema that matches a custom error value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.custom_error_schema(
        schema=core_schema.int_schema(),
        custom_error_type='MyError',
        custom_error_message='Error msg',
    )
    v = SchemaValidator(schema)
    v.validate_python(1)
    ```

    Args:
        schema: The schema to use for the custom error schema
        custom_error_type: The custom error type to use for the custom error schema
        custom_error_message: The custom error message to use for the custom error schema
        custom_error_context: The custom error context to use for the custom error schema
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='custom-error',
        schema=schema,
        custom_error_type=custom_error_type,
        custom_error_message=custom_error_message,
        custom_error_context=custom_error_context,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

dataclass_args_schema

dataclass_args_schema(
    dataclass_name: str,
    fields: list[DataclassField],
    *,
    computed_fields: List[ComputedField] | None = None,
    populate_by_name: bool | None = None,
    collect_init_only: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
    extra_behavior: ExtraBehavior | None = None,
) -> DataclassArgsSchema

Returns a schema for validating dataclass arguments, e.g.:

from pydantic_core import SchemaValidator, core_schema

field_a = core_schema.dataclass_field(
    name='a', schema=core_schema.str_schema(), kw_only=False
)
field_b = core_schema.dataclass_field(
    name='b', schema=core_schema.bool_schema(), kw_only=False
)
schema = core_schema.dataclass_args_schema('Foobar', [field_a, field_b])
v = SchemaValidator(schema)
assert v.validate_python({'a': 'hello', 'b': True}) == ({'a': 'hello', 'b': True}, None)

Parameters:

Name Type Description Default
dataclass_name str

The name of the dataclass being validated

required
fields list[DataclassField]

The fields to use for the dataclass

required
computed_fields List[ComputedField] | None

Computed fields to use when serializing the dataclass

None
populate_by_name bool | None

Whether to populate by name

None
collect_init_only bool | None

Whether to collect init only fields into a dict to pass to __post_init__

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
extra_behavior ExtraBehavior | None

How to handle extra fields

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def dataclass_args_schema(
    dataclass_name: str,
    fields: list[DataclassField],
    *,
    computed_fields: List[ComputedField] | None = None,
    populate_by_name: bool | None = None,
    collect_init_only: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
    extra_behavior: ExtraBehavior | None = None,
) -> DataclassArgsSchema:
    """
    Returns a schema for validating dataclass arguments, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    field_a = core_schema.dataclass_field(
        name='a', schema=core_schema.str_schema(), kw_only=False
    )
    field_b = core_schema.dataclass_field(
        name='b', schema=core_schema.bool_schema(), kw_only=False
    )
    schema = core_schema.dataclass_args_schema('Foobar', [field_a, field_b])
    v = SchemaValidator(schema)
    assert v.validate_python({'a': 'hello', 'b': True}) == ({'a': 'hello', 'b': True}, None)
    ```

    Args:
        dataclass_name: The name of the dataclass being validated
        fields: The fields to use for the dataclass
        computed_fields: Computed fields to use when serializing the dataclass
        populate_by_name: Whether to populate by name
        collect_init_only: Whether to collect init only fields into a dict to pass to `__post_init__`
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
        extra_behavior: How to handle extra fields
    """
    return _dict_not_none(
        type='dataclass-args',
        dataclass_name=dataclass_name,
        fields=fields,
        computed_fields=computed_fields,
        populate_by_name=populate_by_name,
        collect_init_only=collect_init_only,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
        extra_behavior=extra_behavior,
    )

dataclass_field

dataclass_field(
    name: str,
    schema: CoreSchema,
    *,
    kw_only: bool | None = None,
    init: bool | None = None,
    init_only: bool | None = None,
    validation_alias: str
    | list[str | int]
    | list[list[str | int]]
    | None = None,
    serialization_alias: str | None = None,
    serialization_exclude: bool | None = None,
    metadata: Any = None,
    frozen: bool | None = None,
) -> DataclassField

Returns a schema for a dataclass field, e.g.:

from pydantic_core import SchemaValidator, core_schema

field = core_schema.dataclass_field(
    name='a', schema=core_schema.str_schema(), kw_only=False
)
schema = core_schema.dataclass_args_schema('Foobar', [field])
v = SchemaValidator(schema)
assert v.validate_python({'a': 'hello'}) == ({'a': 'hello'}, None)

Parameters:

Name Type Description Default
name str

The name to use for the argument parameter

required
schema CoreSchema

The schema to use for the argument parameter

required
kw_only bool | None

Whether the field can be set with a positional argument as well as a keyword argument

None
init bool | None

Whether the field should be validated during initialization

None
init_only bool | None

Whether the field should be omitted from __dict__ and passed to __post_init__

None
validation_alias str | list[str | int] | list[list[str | int]] | None

The alias(es) to use to find the field in the validation data

None
serialization_alias str | None

The alias to use as a key when serializing

None
serialization_exclude bool | None

Whether to exclude the field when serializing

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
frozen bool | None

Whether the field is frozen

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def dataclass_field(
    name: str,
    schema: CoreSchema,
    *,
    kw_only: bool | None = None,
    init: bool | None = None,
    init_only: bool | None = None,
    validation_alias: str | list[str | int] | list[list[str | int]] | None = None,
    serialization_alias: str | None = None,
    serialization_exclude: bool | None = None,
    metadata: Any = None,
    frozen: bool | None = None,
) -> DataclassField:
    """
    Returns a schema for a dataclass field, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    field = core_schema.dataclass_field(
        name='a', schema=core_schema.str_schema(), kw_only=False
    )
    schema = core_schema.dataclass_args_schema('Foobar', [field])
    v = SchemaValidator(schema)
    assert v.validate_python({'a': 'hello'}) == ({'a': 'hello'}, None)
    ```

    Args:
        name: The name to use for the argument parameter
        schema: The schema to use for the argument parameter
        kw_only: Whether the field can be set with a positional argument as well as a keyword argument
        init: Whether the field should be validated during initialization
        init_only: Whether the field should be omitted  from `__dict__` and passed to `__post_init__`
        validation_alias: The alias(es) to use to find the field in the validation data
        serialization_alias: The alias to use as a key when serializing
        serialization_exclude: Whether to exclude the field when serializing
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        frozen: Whether the field is frozen
    """
    return _dict_not_none(
        type='dataclass-field',
        name=name,
        schema=schema,
        kw_only=kw_only,
        init=init,
        init_only=init_only,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        serialization_exclude=serialization_exclude,
        metadata=metadata,
        frozen=frozen,
    )

dataclass_schema

dataclass_schema(
    cls: Type[Any],
    schema: CoreSchema,
    fields: List[str],
    *,
    cls_name: str | None = None,
    post_init: bool | None = None,
    revalidate_instances: Literal[
        "always", "never", "subclass-instances"
    ]
    | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
    frozen: bool | None = None,
    slots: bool | None = None,
    config: CoreConfig | None = None,
) -> DataclassSchema

Returns a schema for a dataclass. As with ModelSchema, this schema can only be used as a field within another schema, not as the root type.

Parameters:

Name Type Description Default
cls Type[Any]

The dataclass type, used to perform subclass checks

required
schema CoreSchema

The schema to use for the dataclass fields

required
fields List[str]

Fields of the dataclass, this is used in serialization and in validation during re-validation and while validating assignment

required
cls_name str | None

The name to use in error locs, etc; this is useful for generics (default: cls.__name__)

None
post_init bool | None

Whether to call __post_init__ after validation

None
revalidate_instances Literal['always', 'never', 'subclass-instances'] | None

whether instances of models and dataclasses (including subclass instances) should re-validate defaults to config.revalidate_instances, else 'never'

None
strict bool | None

Whether to require an exact instance of cls

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
frozen bool | None

Whether the dataclass is frozen

None
slots bool | None

Whether slots=True on the dataclass, means each field is assigned independently, rather than simply setting __dict__, default false

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def dataclass_schema(
    cls: Type[Any],
    schema: CoreSchema,
    fields: List[str],
    *,
    cls_name: str | None = None,
    post_init: bool | None = None,
    revalidate_instances: Literal['always', 'never', 'subclass-instances'] | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
    frozen: bool | None = None,
    slots: bool | None = None,
    config: CoreConfig | None = None,
) -> DataclassSchema:
    """
    Returns a schema for a dataclass. As with `ModelSchema`, this schema can only be used as a field within
    another schema, not as the root type.

    Args:
        cls: The dataclass type, used to perform subclass checks
        schema: The schema to use for the dataclass fields
        fields: Fields of the dataclass, this is used in serialization and in validation during re-validation
            and while validating assignment
        cls_name: The name to use in error locs, etc; this is useful for generics (default: `cls.__name__`)
        post_init: Whether to call `__post_init__` after validation
        revalidate_instances: whether instances of models and dataclasses (including subclass instances)
            should re-validate defaults to config.revalidate_instances, else 'never'
        strict: Whether to require an exact instance of `cls`
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
        frozen: Whether the dataclass is frozen
        slots: Whether `slots=True` on the dataclass, means each field is assigned independently, rather than
            simply setting `__dict__`, default false
    """
    return _dict_not_none(
        type='dataclass',
        cls=cls,
        fields=fields,
        cls_name=cls_name,
        schema=schema,
        post_init=post_init,
        revalidate_instances=revalidate_instances,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
        frozen=frozen,
        slots=slots,
        config=config,
    )

date_schema

date_schema(
    *,
    strict: bool | None = None,
    le: date | None = None,
    ge: date | None = None,
    lt: date | None = None,
    gt: date | None = None,
    now_op: Literal["past", "future"] | None = None,
    now_utc_offset: int | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> DateSchema

Returns a schema that matches a date value, e.g.:

from datetime import date
from pydantic_core import SchemaValidator, core_schema

schema = core_schema.date_schema(le=date(2020, 1, 1), ge=date(2019, 1, 1))
v = SchemaValidator(schema)
assert v.validate_python(date(2019, 6, 1)) == date(2019, 6, 1)

Parameters:

Name Type Description Default
strict bool | None

Whether the value should be a date or a value that can be converted to a date

None
le date | None

The value must be less than or equal to this date

None
ge date | None

The value must be greater than or equal to this date

None
lt date | None

The value must be strictly less than this date

None
gt date | None

The value must be strictly greater than this date

None
now_op Literal['past', 'future'] | None

The value must be in the past or future relative to the current date

None
now_utc_offset int | None

The value must be in the past or future relative to the current date with this utc offset

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def date_schema(
    *,
    strict: bool | None = None,
    le: date | None = None,
    ge: date | None = None,
    lt: date | None = None,
    gt: date | None = None,
    now_op: Literal['past', 'future'] | None = None,
    now_utc_offset: int | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> DateSchema:
    """
    Returns a schema that matches a date value, e.g.:

    ```py
    from datetime import date
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.date_schema(le=date(2020, 1, 1), ge=date(2019, 1, 1))
    v = SchemaValidator(schema)
    assert v.validate_python(date(2019, 6, 1)) == date(2019, 6, 1)
    ```

    Args:
        strict: Whether the value should be a date or a value that can be converted to a date
        le: The value must be less than or equal to this date
        ge: The value must be greater than or equal to this date
        lt: The value must be strictly less than this date
        gt: The value must be strictly greater than this date
        now_op: The value must be in the past or future relative to the current date
        now_utc_offset: The value must be in the past or future relative to the current date with this utc offset
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='date',
        strict=strict,
        le=le,
        ge=ge,
        lt=lt,
        gt=gt,
        now_op=now_op,
        now_utc_offset=now_utc_offset,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

datetime_schema

datetime_schema(
    *,
    strict: bool | None = None,
    le: datetime | None = None,
    ge: datetime | None = None,
    lt: datetime | None = None,
    gt: datetime | None = None,
    now_op: Literal["past", "future"] | None = None,
    tz_constraint: Literal["aware", "naive"]
    | int
    | None = None,
    now_utc_offset: int | None = None,
    microseconds_precision: Literal[
        "truncate", "error"
    ] = "truncate",
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> DatetimeSchema

Returns a schema that matches a datetime value, e.g.:

from datetime import datetime
from pydantic_core import SchemaValidator, core_schema

schema = core_schema.datetime_schema()
v = SchemaValidator(schema)
now = datetime.now()
assert v.validate_python(str(now)) == now

Parameters:

Name Type Description Default
strict bool | None

Whether the value should be a datetime or a value that can be converted to a datetime

None
le datetime | None

The value must be less than or equal to this datetime

None
ge datetime | None

The value must be greater than or equal to this datetime

None
lt datetime | None

The value must be strictly less than this datetime

None
gt datetime | None

The value must be strictly greater than this datetime

None
now_op Literal['past', 'future'] | None

The value must be in the past or future relative to the current datetime

None
tz_constraint Literal['aware', 'naive'] | int | None

The value must be timezone aware or naive, or an int to indicate required tz offset TODO: use of a tzinfo where offset changes based on the datetime is not yet supported

None
now_utc_offset int | None

The value must be in the past or future relative to the current datetime with this utc offset

None
microseconds_precision Literal['truncate', 'error']

The behavior when seconds have more than 6 digits or microseconds is too large

'truncate'
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def datetime_schema(
    *,
    strict: bool | None = None,
    le: datetime | None = None,
    ge: datetime | None = None,
    lt: datetime | None = None,
    gt: datetime | None = None,
    now_op: Literal['past', 'future'] | None = None,
    tz_constraint: Literal['aware', 'naive'] | int | None = None,
    now_utc_offset: int | None = None,
    microseconds_precision: Literal['truncate', 'error'] = 'truncate',
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> DatetimeSchema:
    """
    Returns a schema that matches a datetime value, e.g.:

    ```py
    from datetime import datetime
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.datetime_schema()
    v = SchemaValidator(schema)
    now = datetime.now()
    assert v.validate_python(str(now)) == now
    ```

    Args:
        strict: Whether the value should be a datetime or a value that can be converted to a datetime
        le: The value must be less than or equal to this datetime
        ge: The value must be greater than or equal to this datetime
        lt: The value must be strictly less than this datetime
        gt: The value must be strictly greater than this datetime
        now_op: The value must be in the past or future relative to the current datetime
        tz_constraint: The value must be timezone aware or naive, or an int to indicate required tz offset
            TODO: use of a tzinfo where offset changes based on the datetime is not yet supported
        now_utc_offset: The value must be in the past or future relative to the current datetime with this utc offset
        microseconds_precision: The behavior when seconds have more than 6 digits or microseconds is too large
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='datetime',
        strict=strict,
        le=le,
        ge=ge,
        lt=lt,
        gt=gt,
        now_op=now_op,
        tz_constraint=tz_constraint,
        now_utc_offset=now_utc_offset,
        microseconds_precision=microseconds_precision,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

decimal_schema

decimal_schema(
    *,
    allow_inf_nan: bool = None,
    multiple_of: Decimal | None = None,
    le: Decimal | None = None,
    ge: Decimal | None = None,
    lt: Decimal | None = None,
    gt: Decimal | None = None,
    max_digits: int | None = None,
    decimal_places: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> DecimalSchema

Returns a schema that matches a decimal value, e.g.:

from decimal import Decimal
from pydantic_core import SchemaValidator, core_schema

schema = core_schema.decimal_schema(le=0.8, ge=0.2)
v = SchemaValidator(schema)
assert v.validate_python('0.5') == Decimal('0.5')

Parameters:

Name Type Description Default
allow_inf_nan bool

Whether to allow inf and nan values

None
multiple_of Decimal | None

The value must be a multiple of this number

None
le Decimal | None

The value must be less than or equal to this number

None
ge Decimal | None

The value must be greater than or equal to this number

None
lt Decimal | None

The value must be strictly less than this number

None
gt Decimal | None

The value must be strictly greater than this number

None
max_digits int | None

The maximum number of decimal digits allowed

None
decimal_places int | None

The maximum number of decimal places allowed

None
strict bool | None

Whether the value should be a float or a value that can be converted to a float

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def decimal_schema(
    *,
    allow_inf_nan: bool = None,
    multiple_of: Decimal | None = None,
    le: Decimal | None = None,
    ge: Decimal | None = None,
    lt: Decimal | None = None,
    gt: Decimal | None = None,
    max_digits: int | None = None,
    decimal_places: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> DecimalSchema:
    """
    Returns a schema that matches a decimal value, e.g.:

    ```py
    from decimal import Decimal
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.decimal_schema(le=0.8, ge=0.2)
    v = SchemaValidator(schema)
    assert v.validate_python('0.5') == Decimal('0.5')
    ```

    Args:
        allow_inf_nan: Whether to allow inf and nan values
        multiple_of: The value must be a multiple of this number
        le: The value must be less than or equal to this number
        ge: The value must be greater than or equal to this number
        lt: The value must be strictly less than this number
        gt: The value must be strictly greater than this number
        max_digits: The maximum number of decimal digits allowed
        decimal_places: The maximum number of decimal places allowed
        strict: Whether the value should be a float or a value that can be converted to a float
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='decimal',
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        max_digits=max_digits,
        decimal_places=decimal_places,
        multiple_of=multiple_of,
        allow_inf_nan=allow_inf_nan,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

definition_reference_schema

definition_reference_schema(
    schema_ref: str,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> DefinitionReferenceSchema

Returns a schema that points to a schema stored in "definitions", this is useful for nested recursive models and also when you want to define validators separately from the main schema, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema_definition = core_schema.definition_reference_schema('list-schema')
schema = core_schema.definitions_schema(
    schema=schema_definition,
    definitions=[
        core_schema.list_schema(items_schema=schema_definition, ref='list-schema'),
    ],
)
v = SchemaValidator(schema)
assert v.validate_python([()]) == [[]]

Parameters:

Name Type Description Default
schema_ref str

The schema ref to use for the definition reference schema

required
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def definition_reference_schema(
    schema_ref: str, ref: str | None = None, metadata: Any = None, serialization: SerSchema | None = None
) -> DefinitionReferenceSchema:
    """
    Returns a schema that points to a schema stored in "definitions", this is useful for nested recursive
    models and also when you want to define validators separately from the main schema, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema_definition = core_schema.definition_reference_schema('list-schema')
    schema = core_schema.definitions_schema(
        schema=schema_definition,
        definitions=[
            core_schema.list_schema(items_schema=schema_definition, ref='list-schema'),
        ],
    )
    v = SchemaValidator(schema)
    assert v.validate_python([()]) == [[]]
    ```

    Args:
        schema_ref: The schema ref to use for the definition reference schema
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='definition-ref', schema_ref=schema_ref, ref=ref, metadata=metadata, serialization=serialization
    )

definitions_schema

definitions_schema(
    schema: CoreSchema, definitions: list[CoreSchema]
) -> DefinitionsSchema

Build a schema that contains both an inner schema and a list of definitions which can be used within the inner schema.

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.definitions_schema(
    core_schema.list_schema(core_schema.definition_reference_schema('foobar')),
    [core_schema.int_schema(ref='foobar')],
)
v = SchemaValidator(schema)
assert v.validate_python([1, 2, '3']) == [1, 2, 3]

Parameters:

Name Type Description Default
schema CoreSchema

The inner schema

required
definitions list[CoreSchema]

List of definitions which can be referenced within inner schema

required
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def definitions_schema(schema: CoreSchema, definitions: list[CoreSchema]) -> DefinitionsSchema:
    """
    Build a schema that contains both an inner schema and a list of definitions which can be used
    within the inner schema.

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.definitions_schema(
        core_schema.list_schema(core_schema.definition_reference_schema('foobar')),
        [core_schema.int_schema(ref='foobar')],
    )
    v = SchemaValidator(schema)
    assert v.validate_python([1, 2, '3']) == [1, 2, 3]
    ```

    Args:
        schema: The inner schema
        definitions: List of definitions which can be referenced within inner schema
    """
    return DefinitionsSchema(type='definitions', schema=schema, definitions=definitions)

dict_schema

dict_schema(
    keys_schema: CoreSchema | None = None,
    values_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> DictSchema

Returns a schema that matches a dict value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.dict_schema(
    keys_schema=core_schema.str_schema(), values_schema=core_schema.int_schema()
)
v = SchemaValidator(schema)
assert v.validate_python({'a': '1', 'b': 2}) == {'a': 1, 'b': 2}

Parameters:

Name Type Description Default
keys_schema CoreSchema | None

The value must be a dict with keys that match this schema

None
values_schema CoreSchema | None

The value must be a dict with values that match this schema

None
min_length int | None

The value must be a dict with at least this many items

None
max_length int | None

The value must be a dict with at most this many items

None
strict bool | None

Whether the keys and values should be validated with strict mode

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def dict_schema(
    keys_schema: CoreSchema | None = None,
    values_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> DictSchema:
    """
    Returns a schema that matches a dict value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.dict_schema(
        keys_schema=core_schema.str_schema(), values_schema=core_schema.int_schema()
    )
    v = SchemaValidator(schema)
    assert v.validate_python({'a': '1', 'b': 2}) == {'a': 1, 'b': 2}
    ```

    Args:
        keys_schema: The value must be a dict with keys that match this schema
        values_schema: The value must be a dict with values that match this schema
        min_length: The value must be a dict with at least this many items
        max_length: The value must be a dict with at most this many items
        strict: Whether the keys and values should be validated with strict mode
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='dict',
        keys_schema=keys_schema,
        values_schema=values_schema,
        min_length=min_length,
        max_length=max_length,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

float_schema

float_schema(
    *,
    allow_inf_nan: bool | None = None,
    multiple_of: float | None = None,
    le: float | None = None,
    ge: float | None = None,
    lt: float | None = None,
    gt: float | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> FloatSchema

Returns a schema that matches a float value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.float_schema(le=0.8, ge=0.2)
v = SchemaValidator(schema)
assert v.validate_python('0.5') == 0.5

Parameters:

Name Type Description Default
allow_inf_nan bool | None

Whether to allow inf and nan values

None
multiple_of float | None

The value must be a multiple of this number

None
le float | None

The value must be less than or equal to this number

None
ge float | None

The value must be greater than or equal to this number

None
lt float | None

The value must be strictly less than this number

None
gt float | None

The value must be strictly greater than this number

None
strict bool | None

Whether the value should be a float or a value that can be converted to a float

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def float_schema(
    *,
    allow_inf_nan: bool | None = None,
    multiple_of: float | None = None,
    le: float | None = None,
    ge: float | None = None,
    lt: float | None = None,
    gt: float | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> FloatSchema:
    """
    Returns a schema that matches a float value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.float_schema(le=0.8, ge=0.2)
    v = SchemaValidator(schema)
    assert v.validate_python('0.5') == 0.5
    ```

    Args:
        allow_inf_nan: Whether to allow inf and nan values
        multiple_of: The value must be a multiple of this number
        le: The value must be less than or equal to this number
        ge: The value must be greater than or equal to this number
        lt: The value must be strictly less than this number
        gt: The value must be strictly greater than this number
        strict: Whether the value should be a float or a value that can be converted to a float
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='float',
        allow_inf_nan=allow_inf_nan,
        multiple_of=multiple_of,
        le=le,
        ge=ge,
        lt=lt,
        gt=gt,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

format_ser_schema

format_ser_schema(
    formatting_string: str,
    *,
    when_used: WhenUsed = "json-unless-none",
) -> FormatSerSchema

Returns a schema for serialization using python's format method.

Parameters:

Name Type Description Default
formatting_string str

String defining the format to use

required
when_used WhenUsed

Same meaning as for [general_function_plain_ser_schema], but with a different default

'json-unless-none'
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def format_ser_schema(formatting_string: str, *, when_used: WhenUsed = 'json-unless-none') -> FormatSerSchema:
    """
    Returns a schema for serialization using python's `format` method.

    Args:
        formatting_string: String defining the format to use
        when_used: Same meaning as for [general_function_plain_ser_schema], but with a different default
    """
    if when_used == 'json-unless-none':
        # just to avoid extra elements in schema, and to use the actual default defined in rust
        when_used = None  # type: ignore
    return _dict_not_none(type='format', formatting_string=formatting_string, when_used=when_used)

frozenset_schema

frozenset_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> FrozenSetSchema

Returns a schema that matches a frozenset of a given schema, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.frozenset_schema(
    items_schema=core_schema.int_schema(), min_length=0, max_length=10
)
v = SchemaValidator(schema)
assert v.validate_python(frozenset(range(3))) == frozenset({0, 1, 2})

Parameters:

Name Type Description Default
items_schema CoreSchema | None

The value must be a frozenset with items that match this schema

None
min_length int | None

The value must be a frozenset with at least this many items

None
max_length int | None

The value must be a frozenset with at most this many items

None
strict bool | None

The value must be a frozenset with exactly this many items

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def frozenset_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> FrozenSetSchema:
    """
    Returns a schema that matches a frozenset of a given schema, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.frozenset_schema(
        items_schema=core_schema.int_schema(), min_length=0, max_length=10
    )
    v = SchemaValidator(schema)
    assert v.validate_python(frozenset(range(3))) == frozenset({0, 1, 2})
    ```

    Args:
        items_schema: The value must be a frozenset with items that match this schema
        min_length: The value must be a frozenset with at least this many items
        max_length: The value must be a frozenset with at most this many items
        strict: The value must be a frozenset with exactly this many items
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='frozenset',
        items_schema=items_schema,
        min_length=min_length,
        max_length=max_length,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

generator_schema

generator_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> GeneratorSchema

Returns a schema that matches a generator value, e.g.:

from typing import Iterator
from pydantic_core import SchemaValidator, core_schema

def gen() -> Iterator[int]:
    yield 1

schema = core_schema.generator_schema(items_schema=core_schema.int_schema())
v = SchemaValidator(schema)
v.validate_python(gen())

Unlike other types, validated generators do not raise ValidationErrors eagerly, but instead will raise a ValidationError when a violating value is actually read from the generator. This is to ensure that "validated" generators retain the benefit of lazy evaluation.

Parameters:

Name Type Description Default
items_schema CoreSchema | None

The value must be a generator with items that match this schema

None
min_length int | None

The value must be a generator that yields at least this many items

None
max_length int | None

The value must be a generator that yields at most this many items

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization IncExSeqOrElseSerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def generator_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> GeneratorSchema:
    """
    Returns a schema that matches a generator value, e.g.:

    ```py
    from typing import Iterator
    from pydantic_core import SchemaValidator, core_schema

    def gen() -> Iterator[int]:
        yield 1

    schema = core_schema.generator_schema(items_schema=core_schema.int_schema())
    v = SchemaValidator(schema)
    v.validate_python(gen())
    ```

    Unlike other types, validated generators do not raise ValidationErrors eagerly,
    but instead will raise a ValidationError when a violating value is actually read from the generator.
    This is to ensure that "validated" generators retain the benefit of lazy evaluation.

    Args:
        items_schema: The value must be a generator with items that match this schema
        min_length: The value must be a generator that yields at least this many items
        max_length: The value must be a generator that yields at most this many items
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='generator',
        items_schema=items_schema,
        min_length=min_length,
        max_length=max_length,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

int_schema

int_schema(
    *,
    multiple_of: int | None = None,
    le: int | None = None,
    ge: int | None = None,
    lt: int | None = None,
    gt: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> IntSchema

Returns a schema that matches a int value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.int_schema(multiple_of=2, le=6, ge=2)
v = SchemaValidator(schema)
assert v.validate_python('4') == 4

Parameters:

Name Type Description Default
multiple_of int | None

The value must be a multiple of this number

None
le int | None

The value must be less than or equal to this number

None
ge int | None

The value must be greater than or equal to this number

None
lt int | None

The value must be strictly less than this number

None
gt int | None

The value must be strictly greater than this number

None
strict bool | None

Whether the value should be a int or a value that can be converted to a int

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def int_schema(
    *,
    multiple_of: int | None = None,
    le: int | None = None,
    ge: int | None = None,
    lt: int | None = None,
    gt: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> IntSchema:
    """
    Returns a schema that matches a int value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.int_schema(multiple_of=2, le=6, ge=2)
    v = SchemaValidator(schema)
    assert v.validate_python('4') == 4
    ```

    Args:
        multiple_of: The value must be a multiple of this number
        le: The value must be less than or equal to this number
        ge: The value must be greater than or equal to this number
        lt: The value must be strictly less than this number
        gt: The value must be strictly greater than this number
        strict: Whether the value should be a int or a value that can be converted to a int
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='int',
        multiple_of=multiple_of,
        le=le,
        ge=ge,
        lt=lt,
        gt=gt,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

is_instance_schema

is_instance_schema(
    cls: Any,
    *,
    cls_repr: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> IsInstanceSchema

Returns a schema that checks if a value is an instance of a class, equivalent to python's isinstance method, e.g.:

from pydantic_core import SchemaValidator, core_schema

class A:
    pass

schema = core_schema.is_instance_schema(cls=A)
v = SchemaValidator(schema)
v.validate_python(A())

Parameters:

Name Type Description Default
cls Any

The value must be an instance of this class

required
cls_repr str | None

If provided this string is used in the validator name instead of repr(cls)

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def is_instance_schema(
    cls: Any,
    *,
    cls_repr: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> IsInstanceSchema:
    """
    Returns a schema that checks if a value is an instance of a class, equivalent to python's `isinstance` method, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    class A:
        pass

    schema = core_schema.is_instance_schema(cls=A)
    v = SchemaValidator(schema)
    v.validate_python(A())
    ```

    Args:
        cls: The value must be an instance of this class
        cls_repr: If provided this string is used in the validator name instead of `repr(cls)`
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='is-instance', cls=cls, cls_repr=cls_repr, ref=ref, metadata=metadata, serialization=serialization
    )

is_subclass_schema

is_subclass_schema(
    cls: Type[Any],
    *,
    cls_repr: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> IsInstanceSchema

Returns a schema that checks if a value is a subtype of a class, equivalent to python's issubclass method, e.g.:

from pydantic_core import SchemaValidator, core_schema

class A:
    pass

class B(A):
    pass

schema = core_schema.is_subclass_schema(cls=A)
v = SchemaValidator(schema)
v.validate_python(B)

Parameters:

Name Type Description Default
cls Type[Any]

The value must be a subclass of this class

required
cls_repr str | None

If provided this string is used in the validator name instead of repr(cls)

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def is_subclass_schema(
    cls: Type[Any],
    *,
    cls_repr: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> IsInstanceSchema:
    """
    Returns a schema that checks if a value is a subtype of a class, equivalent to python's `issubclass` method, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    class A:
        pass

    class B(A):
        pass

    schema = core_schema.is_subclass_schema(cls=A)
    v = SchemaValidator(schema)
    v.validate_python(B)
    ```

    Args:
        cls: The value must be a subclass of this class
        cls_repr: If provided this string is used in the validator name instead of `repr(cls)`
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='is-subclass', cls=cls, cls_repr=cls_repr, ref=ref, metadata=metadata, serialization=serialization
    )

json_or_python_schema

json_or_python_schema(
    json_schema: CoreSchema,
    python_schema: CoreSchema,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> JsonOrPythonSchema

Returns a schema that uses the Json or Python schema depending on the input:

from pydantic_core import SchemaValidator, ValidationError, core_schema

v = SchemaValidator(
    core_schema.json_or_python_schema(
        json_schema=core_schema.int_schema(),
        python_schema=core_schema.int_schema(strict=True),
    )
)

assert v.validate_json('"123"') == 123

try:
    v.validate_python('123')
except ValidationError:
    pass
else:
    raise AssertionError('Validation should have failed')

Parameters:

Name Type Description Default
json_schema CoreSchema

The schema to use for Json inputs

required
python_schema CoreSchema

The schema to use for Python inputs

required
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def json_or_python_schema(
    json_schema: CoreSchema,
    python_schema: CoreSchema,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> JsonOrPythonSchema:
    """
    Returns a schema that uses the Json or Python schema depending on the input:

    ```py
    from pydantic_core import SchemaValidator, ValidationError, core_schema

    v = SchemaValidator(
        core_schema.json_or_python_schema(
            json_schema=core_schema.int_schema(),
            python_schema=core_schema.int_schema(strict=True),
        )
    )

    assert v.validate_json('"123"') == 123

    try:
        v.validate_python('123')
    except ValidationError:
        pass
    else:
        raise AssertionError('Validation should have failed')
    ```

    Args:
        json_schema: The schema to use for Json inputs
        python_schema: The schema to use for Python inputs
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='json-or-python',
        json_schema=json_schema,
        python_schema=python_schema,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

json_schema

json_schema(
    schema: CoreSchema | None = None,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> JsonSchema

Returns a schema that matches a JSON value, e.g.:

from pydantic_core import SchemaValidator, core_schema

dict_schema = core_schema.model_fields_schema(
    {
        'field_a': core_schema.model_field(core_schema.str_schema()),
        'field_b': core_schema.model_field(core_schema.bool_schema()),
    },
)

class MyModel:
    __slots__ = (
        '__dict__',
        '__pydantic_fields_set__',
        '__pydantic_extra__',
        '__pydantic_private__',
    )
    field_a: str
    field_b: bool

json_schema = core_schema.json_schema(schema=dict_schema)
schema = core_schema.model_schema(cls=MyModel, schema=json_schema)
v = SchemaValidator(schema)
m = v.validate_python('{"field_a": "hello", "field_b": true}')
assert isinstance(m, MyModel)

Parameters:

Name Type Description Default
schema CoreSchema | None

The schema to use for the JSON schema

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def json_schema(
    schema: CoreSchema | None = None,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> JsonSchema:
    """
    Returns a schema that matches a JSON value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    dict_schema = core_schema.model_fields_schema(
        {
            'field_a': core_schema.model_field(core_schema.str_schema()),
            'field_b': core_schema.model_field(core_schema.bool_schema()),
        },
    )

    class MyModel:
        __slots__ = (
            '__dict__',
            '__pydantic_fields_set__',
            '__pydantic_extra__',
            '__pydantic_private__',
        )
        field_a: str
        field_b: bool

    json_schema = core_schema.json_schema(schema=dict_schema)
    schema = core_schema.model_schema(cls=MyModel, schema=json_schema)
    v = SchemaValidator(schema)
    m = v.validate_python('{"field_a": "hello", "field_b": true}')
    assert isinstance(m, MyModel)
    ```

    Args:
        schema: The schema to use for the JSON schema
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(type='json', schema=schema, ref=ref, metadata=metadata, serialization=serialization)

lax_or_strict_schema

lax_or_strict_schema(
    lax_schema: CoreSchema,
    strict_schema: CoreSchema,
    *,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> LaxOrStrictSchema

Returns a schema that uses the lax or strict schema, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(v: str, info: core_schema.ValidationInfo) -> str:
    assert 'hello' in v
    return v + ' world'

lax_schema = core_schema.int_schema(strict=False)
strict_schema = core_schema.int_schema(strict=True)

schema = core_schema.lax_or_strict_schema(
    lax_schema=lax_schema, strict_schema=strict_schema, strict=True
)
v = SchemaValidator(schema)
assert v.validate_python(123) == 123

schema = core_schema.lax_or_strict_schema(
    lax_schema=lax_schema, strict_schema=strict_schema, strict=False
)
v = SchemaValidator(schema)
assert v.validate_python('123') == 123

Parameters:

Name Type Description Default
lax_schema CoreSchema

The lax schema to use

required
strict_schema CoreSchema

The strict schema to use

required
strict bool | None

Whether the strict schema should be used

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def lax_or_strict_schema(
    lax_schema: CoreSchema,
    strict_schema: CoreSchema,
    *,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> LaxOrStrictSchema:
    """
    Returns a schema that uses the lax or strict schema, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(v: str, info: core_schema.ValidationInfo) -> str:
        assert 'hello' in v
        return v + ' world'

    lax_schema = core_schema.int_schema(strict=False)
    strict_schema = core_schema.int_schema(strict=True)

    schema = core_schema.lax_or_strict_schema(
        lax_schema=lax_schema, strict_schema=strict_schema, strict=True
    )
    v = SchemaValidator(schema)
    assert v.validate_python(123) == 123

    schema = core_schema.lax_or_strict_schema(
        lax_schema=lax_schema, strict_schema=strict_schema, strict=False
    )
    v = SchemaValidator(schema)
    assert v.validate_python('123') == 123
    ```

    Args:
        lax_schema: The lax schema to use
        strict_schema: The strict schema to use
        strict: Whether the strict schema should be used
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='lax-or-strict',
        lax_schema=lax_schema,
        strict_schema=strict_schema,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

list_schema

list_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> ListSchema

Returns a schema that matches a list value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.list_schema(core_schema.int_schema(), min_length=0, max_length=10)
v = SchemaValidator(schema)
assert v.validate_python(['4']) == [4]

Parameters:

Name Type Description Default
items_schema CoreSchema | None

The value must be a list of items that match this schema

None
min_length int | None

The value must be a list with at least this many items

None
max_length int | None

The value must be a list with at most this many items

None
strict bool | None

The value must be a list with exactly this many items

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization IncExSeqOrElseSerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def list_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> ListSchema:
    """
    Returns a schema that matches a list value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.list_schema(core_schema.int_schema(), min_length=0, max_length=10)
    v = SchemaValidator(schema)
    assert v.validate_python(['4']) == [4]
    ```

    Args:
        items_schema: The value must be a list of items that match this schema
        min_length: The value must be a list with at least this many items
        max_length: The value must be a list with at most this many items
        strict: The value must be a list with exactly this many items
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='list',
        items_schema=items_schema,
        min_length=min_length,
        max_length=max_length,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

literal_schema

literal_schema(
    expected: list[Any],
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> LiteralSchema

Returns a schema that matches a literal value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.literal_schema(['hello', 'world'])
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello'

Parameters:

Name Type Description Default
expected list[Any]

The value must be one of these values

required
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def literal_schema(
    expected: list[Any], *, ref: str | None = None, metadata: Any = None, serialization: SerSchema | None = None
) -> LiteralSchema:
    """
    Returns a schema that matches a literal value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.literal_schema(['hello', 'world'])
    v = SchemaValidator(schema)
    assert v.validate_python('hello') == 'hello'
    ```

    Args:
        expected: The value must be one of these values
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(type='literal', expected=expected, ref=ref, metadata=metadata, serialization=serialization)

model_field

model_field(
    schema: CoreSchema,
    *,
    validation_alias: str
    | list[str | int]
    | list[list[str | int]]
    | None = None,
    serialization_alias: str | None = None,
    serialization_exclude: bool | None = None,
    frozen: bool | None = None,
    metadata: Any = None,
) -> ModelField

Returns a schema for a model field, e.g.:

from pydantic_core import core_schema

field = core_schema.model_field(schema=core_schema.int_schema())

Parameters:

Name Type Description Default
schema CoreSchema

The schema to use for the field

required
validation_alias str | list[str | int] | list[list[str | int]] | None

The alias(es) to use to find the field in the validation data

None
serialization_alias str | None

The alias to use as a key when serializing

None
serialization_exclude bool | None

Whether to exclude the field when serializing

None
frozen bool | None

Whether the field is frozen

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def model_field(
    schema: CoreSchema,
    *,
    validation_alias: str | list[str | int] | list[list[str | int]] | None = None,
    serialization_alias: str | None = None,
    serialization_exclude: bool | None = None,
    frozen: bool | None = None,
    metadata: Any = None,
) -> ModelField:
    """
    Returns a schema for a model field, e.g.:

    ```py
    from pydantic_core import core_schema

    field = core_schema.model_field(schema=core_schema.int_schema())
    ```

    Args:
        schema: The schema to use for the field
        validation_alias: The alias(es) to use to find the field in the validation data
        serialization_alias: The alias to use as a key when serializing
        serialization_exclude: Whether to exclude the field when serializing
        frozen: Whether the field is frozen
        metadata: Any other information you want to include with the schema, not used by pydantic-core
    """
    return _dict_not_none(
        type='model-field',
        schema=schema,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        serialization_exclude=serialization_exclude,
        frozen=frozen,
        metadata=metadata,
    )

model_fields_schema

model_fields_schema(
    fields: Dict[str, ModelField],
    *,
    model_name: str | None = None,
    computed_fields: list[ComputedField] | None = None,
    strict: bool | None = None,
    extras_schema: CoreSchema | None = None,
    extra_behavior: ExtraBehavior | None = None,
    populate_by_name: bool | None = None,
    from_attributes: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> ModelFieldsSchema

Returns a schema that matches a typed dict, e.g.:

from pydantic_core import SchemaValidator, core_schema

wrapper_schema = core_schema.model_fields_schema(
    {'a': core_schema.model_field(core_schema.str_schema())}
)
v = SchemaValidator(wrapper_schema)
print(v.validate_python({'a': 'hello'}))
#> ({'a': 'hello'}, None, {'a'})

Parameters:

Name Type Description Default
fields Dict[str, ModelField]

The fields to use for the typed dict

required
model_name str | None

The name of the model, used for error messages, defaults to "Model"

None
computed_fields list[ComputedField] | None

Computed fields to use when serializing the model, only applies when directly inside a model

None
strict bool | None

Whether the typed dict is strict

None
extras_schema CoreSchema | None

The extra validator to use for the typed dict

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
extra_behavior ExtraBehavior | None

The extra behavior to use for the typed dict

None
populate_by_name bool | None

Whether the typed dict should populate by name

None
from_attributes bool | None

Whether the typed dict should be populated from attributes

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def model_fields_schema(
    fields: Dict[str, ModelField],
    *,
    model_name: str | None = None,
    computed_fields: list[ComputedField] | None = None,
    strict: bool | None = None,
    extras_schema: CoreSchema | None = None,
    extra_behavior: ExtraBehavior | None = None,
    populate_by_name: bool | None = None,
    from_attributes: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> ModelFieldsSchema:
    """
    Returns a schema that matches a typed dict, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    wrapper_schema = core_schema.model_fields_schema(
        {'a': core_schema.model_field(core_schema.str_schema())}
    )
    v = SchemaValidator(wrapper_schema)
    print(v.validate_python({'a': 'hello'}))
    #> ({'a': 'hello'}, None, {'a'})
    ```

    Args:
        fields: The fields to use for the typed dict
        model_name: The name of the model, used for error messages, defaults to "Model"
        computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model
        strict: Whether the typed dict is strict
        extras_schema: The extra validator to use for the typed dict
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        extra_behavior: The extra behavior to use for the typed dict
        populate_by_name: Whether the typed dict should populate by name
        from_attributes: Whether the typed dict should be populated from attributes
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='model-fields',
        fields=fields,
        model_name=model_name,
        computed_fields=computed_fields,
        strict=strict,
        extras_schema=extras_schema,
        extra_behavior=extra_behavior,
        populate_by_name=populate_by_name,
        from_attributes=from_attributes,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

model_schema

model_schema(
    cls: Type[Any],
    schema: CoreSchema,
    *,
    custom_init: bool | None = None,
    root_model: bool | None = None,
    post_init: str | None = None,
    revalidate_instances: Literal[
        "always", "never", "subclass-instances"
    ]
    | None = None,
    strict: bool | None = None,
    frozen: bool | None = None,
    extra_behavior: ExtraBehavior | None = None,
    config: CoreConfig | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> ModelSchema

A model schema generally contains a typed-dict schema. It will run the typed dict validator, then create a new class and set the dict and fields set returned from the typed dict validator to __dict__ and __pydantic_fields_set__ respectively.

Example:

from pydantic_core import CoreConfig, SchemaValidator, core_schema

class MyModel:
    __slots__ = (
        '__dict__',
        '__pydantic_fields_set__',
        '__pydantic_extra__',
        '__pydantic_private__',
    )

schema = core_schema.model_schema(
    cls=MyModel,
    config=CoreConfig(str_max_length=5),
    schema=core_schema.model_fields_schema(
        fields={'a': core_schema.model_field(core_schema.str_schema())},
    ),
)
v = SchemaValidator(schema)
assert v.isinstance_python({'a': 'hello'}) is True
assert v.isinstance_python({'a': 'too long'}) is False

Parameters:

Name Type Description Default
cls Type[Any]

The class to use for the model

required
schema CoreSchema

The schema to use for the model

required
custom_init bool | None

Whether the model has a custom init method

None
root_model bool | None

Whether the model is a RootModel

None
post_init str | None

The call after init to use for the model

None
revalidate_instances Literal['always', 'never', 'subclass-instances'] | None

whether instances of models and dataclasses (including subclass instances) should re-validate defaults to config.revalidate_instances, else 'never'

None
strict bool | None

Whether the model is strict

None
frozen bool | None

Whether the model is frozen

None
extra_behavior ExtraBehavior | None

The extra behavior to use for the model, used in serialization

None
config CoreConfig | None

The config to use for the model

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def model_schema(
    cls: Type[Any],
    schema: CoreSchema,
    *,
    custom_init: bool | None = None,
    root_model: bool | None = None,
    post_init: str | None = None,
    revalidate_instances: Literal['always', 'never', 'subclass-instances'] | None = None,
    strict: bool | None = None,
    frozen: bool | None = None,
    extra_behavior: ExtraBehavior | None = None,
    config: CoreConfig | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> ModelSchema:
    """
    A model schema generally contains a typed-dict schema.
    It will run the typed dict validator, then create a new class
    and set the dict and fields set returned from the typed dict validator
    to `__dict__` and `__pydantic_fields_set__` respectively.

    Example:

    ```py
    from pydantic_core import CoreConfig, SchemaValidator, core_schema

    class MyModel:
        __slots__ = (
            '__dict__',
            '__pydantic_fields_set__',
            '__pydantic_extra__',
            '__pydantic_private__',
        )

    schema = core_schema.model_schema(
        cls=MyModel,
        config=CoreConfig(str_max_length=5),
        schema=core_schema.model_fields_schema(
            fields={'a': core_schema.model_field(core_schema.str_schema())},
        ),
    )
    v = SchemaValidator(schema)
    assert v.isinstance_python({'a': 'hello'}) is True
    assert v.isinstance_python({'a': 'too long'}) is False
    ```

    Args:
        cls: The class to use for the model
        schema: The schema to use for the model
        custom_init: Whether the model has a custom init method
        root_model: Whether the model is a `RootModel`
        post_init: The call after init to use for the model
        revalidate_instances: whether instances of models and dataclasses (including subclass instances)
            should re-validate defaults to config.revalidate_instances, else 'never'
        strict: Whether the model is strict
        frozen: Whether the model is frozen
        extra_behavior: The extra behavior to use for the model, used in serialization
        config: The config to use for the model
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='model',
        cls=cls,
        schema=schema,
        custom_init=custom_init,
        root_model=root_model,
        post_init=post_init,
        revalidate_instances=revalidate_instances,
        strict=strict,
        frozen=frozen,
        extra_behavior=extra_behavior,
        config=config,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

model_ser_schema

model_ser_schema(
    cls: Type[Any], schema: CoreSchema
) -> ModelSerSchema

Returns a schema for serialization using a model.

Parameters:

Name Type Description Default
cls Type[Any]

The expected class type, used to generate warnings if the wrong type is passed

required
schema CoreSchema

Internal schema to use to serialize the model dict

required
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def model_ser_schema(cls: Type[Any], schema: CoreSchema) -> ModelSerSchema:
    """
    Returns a schema for serialization using a model.

    Args:
        cls: The expected class type, used to generate warnings if the wrong type is passed
        schema: Internal schema to use to serialize the model dict
    """
    return ModelSerSchema(type='model', cls=cls, schema=schema)

multi_host_url_schema

multi_host_url_schema(
    *,
    max_length: int | None = None,
    allowed_schemes: list[str] | None = None,
    host_required: bool | None = None,
    default_host: str | None = None,
    default_port: int | None = None,
    default_path: str | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> MultiHostUrlSchema

Returns a schema that matches a URL value with possibly multiple hosts, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.multi_host_url_schema()
v = SchemaValidator(schema)
print(v.validate_python('redis://localhost,0.0.0.0,127.0.0.1'))
#> redis://localhost,0.0.0.0,127.0.0.1

Parameters:

Name Type Description Default
max_length int | None

The maximum length of the URL

None
allowed_schemes list[str] | None

The allowed URL schemes

None
host_required bool | None

Whether the URL must have a host

None
default_host str | None

The default host to use if the URL does not have a host

None
default_port int | None

The default port to use if the URL does not have a port

None
default_path str | None

The default path to use if the URL does not have a path

None
strict bool | None

Whether to use strict URL parsing

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def multi_host_url_schema(
    *,
    max_length: int | None = None,
    allowed_schemes: list[str] | None = None,
    host_required: bool | None = None,
    default_host: str | None = None,
    default_port: int | None = None,
    default_path: str | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> MultiHostUrlSchema:
    """
    Returns a schema that matches a URL value with possibly multiple hosts, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.multi_host_url_schema()
    v = SchemaValidator(schema)
    print(v.validate_python('redis://localhost,0.0.0.0,127.0.0.1'))
    #> redis://localhost,0.0.0.0,127.0.0.1
    ```

    Args:
        max_length: The maximum length of the URL
        allowed_schemes: The allowed URL schemes
        host_required: Whether the URL must have a host
        default_host: The default host to use if the URL does not have a host
        default_port: The default port to use if the URL does not have a port
        default_path: The default path to use if the URL does not have a path
        strict: Whether to use strict URL parsing
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='multi-host-url',
        max_length=max_length,
        allowed_schemes=allowed_schemes,
        host_required=host_required,
        default_host=default_host,
        default_port=default_port,
        default_path=default_path,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

no_info_after_validator_function

no_info_after_validator_function(
    function: NoInfoValidatorFunction,
    schema: CoreSchema,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> AfterValidatorFunctionSchema

Returns a schema that calls a validator function after validating, no info argument is provided, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(v: str) -> str:
    return v + 'world'

func_schema = core_schema.no_info_after_validator_function(fn, core_schema.str_schema())
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

v = SchemaValidator(schema)
assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}

Parameters:

Name Type Description Default
function NoInfoValidatorFunction

The validator function to call after the schema is validated

required
schema CoreSchema

The schema to validate before the validator function

required
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def no_info_after_validator_function(
    function: NoInfoValidatorFunction,
    schema: CoreSchema,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> AfterValidatorFunctionSchema:
    """
    Returns a schema that calls a validator function after validating, no `info` argument is provided, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(v: str) -> str:
        return v + 'world'

    func_schema = core_schema.no_info_after_validator_function(fn, core_schema.str_schema())
    schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

    v = SchemaValidator(schema)
    assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}
    ```

    Args:
        function: The validator function to call after the schema is validated
        schema: The schema to validate before the validator function
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='function-after',
        function={'type': 'no-info', 'function': function},
        schema=schema,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

no_info_before_validator_function

no_info_before_validator_function(
    function: NoInfoValidatorFunction,
    schema: CoreSchema,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> BeforeValidatorFunctionSchema

Returns a schema that calls a validator function before validating, no info argument is provided, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(v: bytes) -> str:
    return v.decode() + 'world'

func_schema = core_schema.no_info_before_validator_function(
    function=fn, schema=core_schema.str_schema()
)
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

v = SchemaValidator(schema)
assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}

Parameters:

Name Type Description Default
function NoInfoValidatorFunction

The validator function to call

required
schema CoreSchema

The schema to validate the output of the validator function

required
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def no_info_before_validator_function(
    function: NoInfoValidatorFunction,
    schema: CoreSchema,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> BeforeValidatorFunctionSchema:
    """
    Returns a schema that calls a validator function before validating, no `info` argument is provided, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(v: bytes) -> str:
        return v.decode() + 'world'

    func_schema = core_schema.no_info_before_validator_function(
        function=fn, schema=core_schema.str_schema()
    )
    schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

    v = SchemaValidator(schema)
    assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}
    ```

    Args:
        function: The validator function to call
        schema: The schema to validate the output of the validator function
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='function-before',
        function={'type': 'no-info', 'function': function},
        schema=schema,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

no_info_plain_validator_function

no_info_plain_validator_function(
    function: NoInfoValidatorFunction,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> PlainValidatorFunctionSchema

Returns a schema that uses the provided function for validation, no info argument is passed, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(v: str) -> str:
    assert 'hello' in v
    return v + 'world'

schema = core_schema.no_info_plain_validator_function(function=fn)
v = SchemaValidator(schema)
assert v.validate_python('hello ') == 'hello world'

Parameters:

Name Type Description Default
function NoInfoValidatorFunction

The validator function to call

required
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def no_info_plain_validator_function(
    function: NoInfoValidatorFunction,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> PlainValidatorFunctionSchema:
    """
    Returns a schema that uses the provided function for validation, no `info` argument is passed, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(v: str) -> str:
        assert 'hello' in v
        return v + 'world'

    schema = core_schema.no_info_plain_validator_function(function=fn)
    v = SchemaValidator(schema)
    assert v.validate_python('hello ') == 'hello world'
    ```

    Args:
        function: The validator function to call
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='function-plain',
        function={'type': 'no-info', 'function': function},
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

no_info_wrap_validator_function

no_info_wrap_validator_function(
    function: NoInfoWrapValidatorFunction,
    schema: CoreSchema,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> WrapValidatorFunctionSchema

Returns a schema which calls a function with a validator callable argument which can optionally be used to call inner validation with the function logic, this is much like the "onion" implementation of middleware in many popular web frameworks, no info argument is passed, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(
    v: str,
    validator: core_schema.ValidatorFunctionWrapHandler,
) -> str:
    return validator(input_value=v) + 'world'

schema = core_schema.no_info_wrap_validator_function(
    function=fn, schema=core_schema.str_schema()
)
v = SchemaValidator(schema)
assert v.validate_python('hello ') == 'hello world'

Parameters:

Name Type Description Default
function NoInfoWrapValidatorFunction

The validator function to call

required
schema CoreSchema

The schema to validate the output of the validator function

required
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def no_info_wrap_validator_function(
    function: NoInfoWrapValidatorFunction,
    schema: CoreSchema,
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> WrapValidatorFunctionSchema:
    """
    Returns a schema which calls a function with a `validator` callable argument which can
    optionally be used to call inner validation with the function logic, this is much like the
    "onion" implementation of middleware in many popular web frameworks, no `info` argument is passed, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(
        v: str,
        validator: core_schema.ValidatorFunctionWrapHandler,
    ) -> str:
        return validator(input_value=v) + 'world'

    schema = core_schema.no_info_wrap_validator_function(
        function=fn, schema=core_schema.str_schema()
    )
    v = SchemaValidator(schema)
    assert v.validate_python('hello ') == 'hello world'
    ```

    Args:
        function: The validator function to call
        schema: The schema to validate the output of the validator function
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='function-wrap',
        function={'type': 'no-info', 'function': function},
        schema=schema,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

none_schema

none_schema(
    *,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> NoneSchema

Returns a schema that matches a None value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.none_schema()
v = SchemaValidator(schema)
assert v.validate_python(None) is None

Parameters:

Name Type Description Default
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def none_schema(*, ref: str | None = None, metadata: Any = None, serialization: SerSchema | None = None) -> NoneSchema:
    """
    Returns a schema that matches a None value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.none_schema()
    v = SchemaValidator(schema)
    assert v.validate_python(None) is None
    ```

    Args:
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(type='none', ref=ref, metadata=metadata, serialization=serialization)

nullable_schema

nullable_schema(
    schema: CoreSchema,
    *,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> NullableSchema

Returns a schema that matches a nullable value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.nullable_schema(core_schema.str_schema())
v = SchemaValidator(schema)
assert v.validate_python(None) is None

Parameters:

Name Type Description Default
schema CoreSchema

The schema to wrap

required
strict bool | None

Whether the underlying schema should be validated with strict mode

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def nullable_schema(
    schema: CoreSchema,
    *,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> NullableSchema:
    """
    Returns a schema that matches a nullable value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.nullable_schema(core_schema.str_schema())
    v = SchemaValidator(schema)
    assert v.validate_python(None) is None
    ```

    Args:
        schema: The schema to wrap
        strict: Whether the underlying schema should be validated with strict mode
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='nullable', schema=schema, strict=strict, ref=ref, metadata=metadata, serialization=serialization
    )

plain_serializer_function_ser_schema

plain_serializer_function_ser_schema(
    function: SerializerFunction,
    *,
    is_field_serializer: bool | None = None,
    info_arg: bool | None = None,
    return_schema: CoreSchema | None = None,
    when_used: WhenUsed = "always",
) -> PlainSerializerFunctionSerSchema

Returns a schema for serialization with a function, can be either a "general" or "field" function.

Parameters:

Name Type Description Default
function SerializerFunction

The function to use for serialization

required
is_field_serializer bool | None

Whether the serializer is for a field, e.g. takes model as the first argument, and info includes field_name

None
info_arg bool | None

Whether the function takes an __info argument

None
return_schema CoreSchema | None

Schema to use for serializing return value

None
when_used WhenUsed

When the function should be called

'always'
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def plain_serializer_function_ser_schema(
    function: SerializerFunction,
    *,
    is_field_serializer: bool | None = None,
    info_arg: bool | None = None,
    return_schema: CoreSchema | None = None,
    when_used: WhenUsed = 'always',
) -> PlainSerializerFunctionSerSchema:
    """
    Returns a schema for serialization with a function, can be either a "general" or "field" function.

    Args:
        function: The function to use for serialization
        is_field_serializer: Whether the serializer is for a field, e.g. takes `model` as the first argument,
            and `info` includes `field_name`
        info_arg: Whether the function takes an `__info` argument
        return_schema: Schema to use for serializing return value
        when_used: When the function should be called
    """
    if when_used == 'always':
        # just to avoid extra elements in schema, and to use the actual default defined in rust
        when_used = None  # type: ignore
    return _dict_not_none(
        type='function-plain',
        function=function,
        is_field_serializer=is_field_serializer,
        info_arg=info_arg,
        return_schema=return_schema,
        when_used=when_used,
    )

set_schema

set_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> SetSchema

Returns a schema that matches a set of a given schema, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.set_schema(
    items_schema=core_schema.int_schema(), min_length=0, max_length=10
)
v = SchemaValidator(schema)
assert v.validate_python({1, '2', 3}) == {1, 2, 3}

Parameters:

Name Type Description Default
items_schema CoreSchema | None

The value must be a set with items that match this schema

None
min_length int | None

The value must be a set with at least this many items

None
max_length int | None

The value must be a set with at most this many items

None
strict bool | None

The value must be a set with exactly this many items

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def set_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> SetSchema:
    """
    Returns a schema that matches a set of a given schema, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.set_schema(
        items_schema=core_schema.int_schema(), min_length=0, max_length=10
    )
    v = SchemaValidator(schema)
    assert v.validate_python({1, '2', 3}) == {1, 2, 3}
    ```

    Args:
        items_schema: The value must be a set with items that match this schema
        min_length: The value must be a set with at least this many items
        max_length: The value must be a set with at most this many items
        strict: The value must be a set with exactly this many items
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='set',
        items_schema=items_schema,
        min_length=min_length,
        max_length=max_length,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

simple_ser_schema

simple_ser_schema(
    type: ExpectedSerializationTypes,
) -> SimpleSerSchema

Returns a schema for serialization with a custom type.

Parameters:

Name Type Description Default
type ExpectedSerializationTypes

The type to use for serialization

required
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def simple_ser_schema(type: ExpectedSerializationTypes) -> SimpleSerSchema:
    """
    Returns a schema for serialization with a custom type.

    Args:
        type: The type to use for serialization
    """
    return SimpleSerSchema(type=type)

str_schema

str_schema(
    *,
    pattern: str | None = None,
    max_length: int | None = None,
    min_length: int | None = None,
    strip_whitespace: bool | None = None,
    to_lower: bool | None = None,
    to_upper: bool | None = None,
    regex_engine: Literal["rust-regex", "python-re"]
    | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> StringSchema

Returns a schema that matches a string value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.str_schema(max_length=10, min_length=2)
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello'

Parameters:

Name Type Description Default
pattern str | None

A regex pattern that the value must match

None
max_length int | None

The value must be at most this length

None
min_length int | None

The value must be at least this length

None
strip_whitespace bool | None

Whether to strip whitespace from the value

None
to_lower bool | None

Whether to convert the value to lowercase

None
to_upper bool | None

Whether to convert the value to uppercase

None
regex_engine Literal['rust-regex', 'python-re'] | None

The regex engine to use for pattern validation. Default is 'rust-regex'. - rust-regex uses the regex Rust crate, which is non-backtracking and therefore more DDoS resistant, but does not support all regex features. - python-re use the re module, which supports all regex features, but may be slower.

None
strict bool | None

Whether the value should be a string or a value that can be converted to a string

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def str_schema(
    *,
    pattern: str | None = None,
    max_length: int | None = None,
    min_length: int | None = None,
    strip_whitespace: bool | None = None,
    to_lower: bool | None = None,
    to_upper: bool | None = None,
    regex_engine: Literal['rust-regex', 'python-re'] | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> StringSchema:
    """
    Returns a schema that matches a string value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.str_schema(max_length=10, min_length=2)
    v = SchemaValidator(schema)
    assert v.validate_python('hello') == 'hello'
    ```

    Args:
        pattern: A regex pattern that the value must match
        max_length: The value must be at most this length
        min_length: The value must be at least this length
        strip_whitespace: Whether to strip whitespace from the value
        to_lower: Whether to convert the value to lowercase
        to_upper: Whether to convert the value to uppercase
        regex_engine: The regex engine to use for pattern validation. Default is 'rust-regex'.
            - `rust-regex` uses the [`regex`](https://docs.rs/regex) Rust
              crate, which is non-backtracking and therefore more DDoS
              resistant, but does not support all regex features.
            - `python-re` use the [`re`](https://docs.python.org/3/library/re.html) module,
              which supports all regex features, but may be slower.
        strict: Whether the value should be a string or a value that can be converted to a string
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='str',
        pattern=pattern,
        max_length=max_length,
        min_length=min_length,
        strip_whitespace=strip_whitespace,
        to_lower=to_lower,
        to_upper=to_upper,
        regex_engine=regex_engine,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

tagged_union_schema

tagged_union_schema(
    choices: Dict[Hashable, CoreSchema],
    discriminator: str
    | list[str | int]
    | list[list[str | int]]
    | Callable[[Any], Hashable],
    *,
    custom_error_type: str | None = None,
    custom_error_message: str | None = None,
    custom_error_context: dict[str, int | str | float]
    | None = None,
    strict: bool | None = None,
    from_attributes: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> TaggedUnionSchema

Returns a schema that matches a tagged union value, e.g.:

from pydantic_core import SchemaValidator, core_schema

apple_schema = core_schema.typed_dict_schema(
    {
        'foo': core_schema.typed_dict_field(core_schema.str_schema()),
        'bar': core_schema.typed_dict_field(core_schema.int_schema()),
    }
)
banana_schema = core_schema.typed_dict_schema(
    {
        'foo': core_schema.typed_dict_field(core_schema.str_schema()),
        'spam': core_schema.typed_dict_field(
            core_schema.list_schema(items_schema=core_schema.int_schema())
        ),
    }
)
schema = core_schema.tagged_union_schema(
    choices={
        'apple': apple_schema,
        'banana': banana_schema,
    },
    discriminator='foo',
)
v = SchemaValidator(schema)
assert v.validate_python({'foo': 'apple', 'bar': '123'}) == {'foo': 'apple', 'bar': 123}
assert v.validate_python({'foo': 'banana', 'spam': [1, 2, 3]}) == {
    'foo': 'banana',
    'spam': [1, 2, 3],
}

Parameters:

Name Type Description Default
choices Dict[Hashable, CoreSchema]

The schemas to match When retrieving a schema from choices using the discriminator value, if the value is a str, it should be fed back into the choices map until a schema is obtained (This approach is to prevent multiple ownership of a single schema in Rust)

required
discriminator str | list[str | int] | list[list[str | int]] | Callable[[Any], Hashable]

The discriminator to use to determine the schema to use * If discriminator is a str, it is the name of the attribute to use as the discriminator * If discriminator is a list of int/str, it should be used as a "path" to access the discriminator * If discriminator is a list of lists, each inner list is a path, and the first path that exists is used * If discriminator is a callable, it should return the discriminator when called on the value to validate; the callable can return None to indicate that there is no matching discriminator present on the input

required
custom_error_type str | None

The custom error type to use if the validation fails

None
custom_error_message str | None

The custom error message to use if the validation fails

None
custom_error_context dict[str, int | str | float] | None

The custom error context to use if the validation fails

None
strict bool | None

Whether the underlying schemas should be validated with strict mode

None
from_attributes bool | None

Whether to use the attributes of the object to retrieve the discriminator value

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def tagged_union_schema(
    choices: Dict[Hashable, CoreSchema],
    discriminator: str | list[str | int] | list[list[str | int]] | Callable[[Any], Hashable],
    *,
    custom_error_type: str | None = None,
    custom_error_message: str | None = None,
    custom_error_context: dict[str, int | str | float] | None = None,
    strict: bool | None = None,
    from_attributes: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> TaggedUnionSchema:
    """
    Returns a schema that matches a tagged union value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    apple_schema = core_schema.typed_dict_schema(
        {
            'foo': core_schema.typed_dict_field(core_schema.str_schema()),
            'bar': core_schema.typed_dict_field(core_schema.int_schema()),
        }
    )
    banana_schema = core_schema.typed_dict_schema(
        {
            'foo': core_schema.typed_dict_field(core_schema.str_schema()),
            'spam': core_schema.typed_dict_field(
                core_schema.list_schema(items_schema=core_schema.int_schema())
            ),
        }
    )
    schema = core_schema.tagged_union_schema(
        choices={
            'apple': apple_schema,
            'banana': banana_schema,
        },
        discriminator='foo',
    )
    v = SchemaValidator(schema)
    assert v.validate_python({'foo': 'apple', 'bar': '123'}) == {'foo': 'apple', 'bar': 123}
    assert v.validate_python({'foo': 'banana', 'spam': [1, 2, 3]}) == {
        'foo': 'banana',
        'spam': [1, 2, 3],
    }
    ```

    Args:
        choices: The schemas to match
            When retrieving a schema from `choices` using the discriminator value, if the value is a str,
            it should be fed back into the `choices` map until a schema is obtained
            (This approach is to prevent multiple ownership of a single schema in Rust)
        discriminator: The discriminator to use to determine the schema to use
            * If `discriminator` is a str, it is the name of the attribute to use as the discriminator
            * If `discriminator` is a list of int/str, it should be used as a "path" to access the discriminator
            * If `discriminator` is a list of lists, each inner list is a path, and the first path that exists is used
            * If `discriminator` is a callable, it should return the discriminator when called on the value to validate;
              the callable can return `None` to indicate that there is no matching discriminator present on the input
        custom_error_type: The custom error type to use if the validation fails
        custom_error_message: The custom error message to use if the validation fails
        custom_error_context: The custom error context to use if the validation fails
        strict: Whether the underlying schemas should be validated with strict mode
        from_attributes: Whether to use the attributes of the object to retrieve the discriminator value
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='tagged-union',
        choices=choices,
        discriminator=discriminator,
        custom_error_type=custom_error_type,
        custom_error_message=custom_error_message,
        custom_error_context=custom_error_context,
        strict=strict,
        from_attributes=from_attributes,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

time_schema

time_schema(
    *,
    strict: bool | None = None,
    le: time | None = None,
    ge: time | None = None,
    lt: time | None = None,
    gt: time | None = None,
    tz_constraint: Literal["aware", "naive"]
    | int
    | None = None,
    microseconds_precision: Literal[
        "truncate", "error"
    ] = "truncate",
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> TimeSchema

Returns a schema that matches a time value, e.g.:

from datetime import time
from pydantic_core import SchemaValidator, core_schema

schema = core_schema.time_schema(le=time(12, 0, 0), ge=time(6, 0, 0))
v = SchemaValidator(schema)
assert v.validate_python(time(9, 0, 0)) == time(9, 0, 0)

Parameters:

Name Type Description Default
strict bool | None

Whether the value should be a time or a value that can be converted to a time

None
le time | None

The value must be less than or equal to this time

None
ge time | None

The value must be greater than or equal to this time

None
lt time | None

The value must be strictly less than this time

None
gt time | None

The value must be strictly greater than this time

None
tz_constraint Literal['aware', 'naive'] | int | None

The value must be timezone aware or naive, or an int to indicate required tz offset

None
microseconds_precision Literal['truncate', 'error']

The behavior when seconds have more than 6 digits or microseconds is too large

'truncate'
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def time_schema(
    *,
    strict: bool | None = None,
    le: time | None = None,
    ge: time | None = None,
    lt: time | None = None,
    gt: time | None = None,
    tz_constraint: Literal['aware', 'naive'] | int | None = None,
    microseconds_precision: Literal['truncate', 'error'] = 'truncate',
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> TimeSchema:
    """
    Returns a schema that matches a time value, e.g.:

    ```py
    from datetime import time
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.time_schema(le=time(12, 0, 0), ge=time(6, 0, 0))
    v = SchemaValidator(schema)
    assert v.validate_python(time(9, 0, 0)) == time(9, 0, 0)
    ```

    Args:
        strict: Whether the value should be a time or a value that can be converted to a time
        le: The value must be less than or equal to this time
        ge: The value must be greater than or equal to this time
        lt: The value must be strictly less than this time
        gt: The value must be strictly greater than this time
        tz_constraint: The value must be timezone aware or naive, or an int to indicate required tz offset
        microseconds_precision: The behavior when seconds have more than 6 digits or microseconds is too large
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='time',
        strict=strict,
        le=le,
        ge=ge,
        lt=lt,
        gt=gt,
        tz_constraint=tz_constraint,
        microseconds_precision=microseconds_precision,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

timedelta_schema

timedelta_schema(
    *,
    strict: bool | None = None,
    le: timedelta | None = None,
    ge: timedelta | None = None,
    lt: timedelta | None = None,
    gt: timedelta | None = None,
    microseconds_precision: Literal[
        "truncate", "error"
    ] = "truncate",
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> TimedeltaSchema

Returns a schema that matches a timedelta value, e.g.:

from datetime import timedelta
from pydantic_core import SchemaValidator, core_schema

schema = core_schema.timedelta_schema(le=timedelta(days=1), ge=timedelta(days=0))
v = SchemaValidator(schema)
assert v.validate_python(timedelta(hours=12)) == timedelta(hours=12)

Parameters:

Name Type Description Default
strict bool | None

Whether the value should be a timedelta or a value that can be converted to a timedelta

None
le timedelta | None

The value must be less than or equal to this timedelta

None
ge timedelta | None

The value must be greater than or equal to this timedelta

None
lt timedelta | None

The value must be strictly less than this timedelta

None
gt timedelta | None

The value must be strictly greater than this timedelta

None
microseconds_precision Literal['truncate', 'error']

The behavior when seconds have more than 6 digits or microseconds is too large

'truncate'
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def timedelta_schema(
    *,
    strict: bool | None = None,
    le: timedelta | None = None,
    ge: timedelta | None = None,
    lt: timedelta | None = None,
    gt: timedelta | None = None,
    microseconds_precision: Literal['truncate', 'error'] = 'truncate',
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> TimedeltaSchema:
    """
    Returns a schema that matches a timedelta value, e.g.:

    ```py
    from datetime import timedelta
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.timedelta_schema(le=timedelta(days=1), ge=timedelta(days=0))
    v = SchemaValidator(schema)
    assert v.validate_python(timedelta(hours=12)) == timedelta(hours=12)
    ```

    Args:
        strict: Whether the value should be a timedelta or a value that can be converted to a timedelta
        le: The value must be less than or equal to this timedelta
        ge: The value must be greater than or equal to this timedelta
        lt: The value must be strictly less than this timedelta
        gt: The value must be strictly greater than this timedelta
        microseconds_precision: The behavior when seconds have more than 6 digits or microseconds is too large
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='timedelta',
        strict=strict,
        le=le,
        ge=ge,
        lt=lt,
        gt=gt,
        microseconds_precision=microseconds_precision,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

to_string_ser_schema

to_string_ser_schema(
    *, when_used: WhenUsed = "json-unless-none"
) -> ToStringSerSchema

Returns a schema for serialization using python's str() / __str__ method.

Parameters:

Name Type Description Default
when_used WhenUsed

Same meaning as for [general_function_plain_ser_schema], but with a different default

'json-unless-none'
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def to_string_ser_schema(*, when_used: WhenUsed = 'json-unless-none') -> ToStringSerSchema:
    """
    Returns a schema for serialization using python's `str()` / `__str__` method.

    Args:
        when_used: Same meaning as for [general_function_plain_ser_schema], but with a different default
    """
    s = dict(type='to-string')
    if when_used != 'json-unless-none':
        # just to avoid extra elements in schema, and to use the actual default defined in rust
        s['when_used'] = when_used
    return s  # type: ignore

tuple_positional_schema

tuple_positional_schema(
    items_schema: list[CoreSchema],
    *,
    extras_schema: CoreSchema | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> TupleSchema

Returns a schema that matches a tuple of schemas, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.tuple_positional_schema(
    [core_schema.int_schema(), core_schema.str_schema()]
)
v = SchemaValidator(schema)
assert v.validate_python((1, 'hello')) == (1, 'hello')

Parameters:

Name Type Description Default
items_schema list[CoreSchema]

The value must be a tuple with items that match these schemas

required
extras_schema CoreSchema | None

The value must be a tuple with items that match this schema This was inspired by JSON schema's prefixItems and items fields. In python's typing.Tuple, you can't specify a type for "extra" items -- they must all be the same type if the length is variable. So this field won't be set from a typing.Tuple annotation on a pydantic model.

None
strict bool | None

The value must be a tuple with exactly this many items

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization IncExSeqOrElseSerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def tuple_positional_schema(
    items_schema: list[CoreSchema],
    *,
    extras_schema: CoreSchema | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> TupleSchema:
    """
    Returns a schema that matches a tuple of schemas, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.tuple_positional_schema(
        [core_schema.int_schema(), core_schema.str_schema()]
    )
    v = SchemaValidator(schema)
    assert v.validate_python((1, 'hello')) == (1, 'hello')
    ```

    Args:
        items_schema: The value must be a tuple with items that match these schemas
        extras_schema: The value must be a tuple with items that match this schema
            This was inspired by JSON schema's `prefixItems` and `items` fields.
            In python's `typing.Tuple`, you can't specify a type for "extra" items -- they must all be the same type
            if the length is variable. So this field won't be set from a `typing.Tuple` annotation on a pydantic model.
        strict: The value must be a tuple with exactly this many items
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    if extras_schema is not None:
        variadic_item_index = len(items_schema)
        items_schema = items_schema + [extras_schema]
    else:
        variadic_item_index = None
    return tuple_schema(
        items_schema=items_schema,
        variadic_item_index=variadic_item_index,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

tuple_schema

tuple_schema(
    items_schema: list[CoreSchema],
    *,
    variadic_item_index: int | None = None,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> TupleSchema

Returns a schema that matches a tuple of schemas, with an optional variadic item, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.tuple_schema(
    [core_schema.int_schema(), core_schema.str_schema(), core_schema.float_schema()],
    variadic_item_index=1,
)
v = SchemaValidator(schema)
assert v.validate_python((1, 'hello', 'world', 1.5)) == (1, 'hello', 'world', 1.5)

Parameters:

Name Type Description Default
items_schema list[CoreSchema]

The value must be a tuple with items that match these schemas

required
variadic_item_index int | None

The index of the schema in items_schema to be treated as variadic (following PEP 646)

None
min_length int | None

The value must be a tuple with at least this many items

None
max_length int | None

The value must be a tuple with at most this many items

None
strict bool | None

The value must be a tuple with exactly this many items

None
ref str | None

Optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization IncExSeqOrElseSerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def tuple_schema(
    items_schema: list[CoreSchema],
    *,
    variadic_item_index: int | None = None,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> TupleSchema:
    """
    Returns a schema that matches a tuple of schemas, with an optional variadic item, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.tuple_schema(
        [core_schema.int_schema(), core_schema.str_schema(), core_schema.float_schema()],
        variadic_item_index=1,
    )
    v = SchemaValidator(schema)
    assert v.validate_python((1, 'hello', 'world', 1.5)) == (1, 'hello', 'world', 1.5)
    ```

    Args:
        items_schema: The value must be a tuple with items that match these schemas
        variadic_item_index: The index of the schema in `items_schema` to be treated as variadic (following PEP 646)
        min_length: The value must be a tuple with at least this many items
        max_length: The value must be a tuple with at most this many items
        strict: The value must be a tuple with exactly this many items
        ref: Optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='tuple',
        items_schema=items_schema,
        variadic_item_index=variadic_item_index,
        min_length=min_length,
        max_length=max_length,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

tuple_variable_schema

tuple_variable_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> TupleSchema

Returns a schema that matches a tuple of a given schema, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.tuple_variable_schema(
    items_schema=core_schema.int_schema(), min_length=0, max_length=10
)
v = SchemaValidator(schema)
assert v.validate_python(('1', 2, 3)) == (1, 2, 3)

Parameters:

Name Type Description Default
items_schema CoreSchema | None

The value must be a tuple with items that match this schema

None
min_length int | None

The value must be a tuple with at least this many items

None
max_length int | None

The value must be a tuple with at most this many items

None
strict bool | None

The value must be a tuple with exactly this many items

None
ref str | None

Optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization IncExSeqOrElseSerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def tuple_variable_schema(
    items_schema: CoreSchema | None = None,
    *,
    min_length: int | None = None,
    max_length: int | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: IncExSeqOrElseSerSchema | None = None,
) -> TupleSchema:
    """
    Returns a schema that matches a tuple of a given schema, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.tuple_variable_schema(
        items_schema=core_schema.int_schema(), min_length=0, max_length=10
    )
    v = SchemaValidator(schema)
    assert v.validate_python(('1', 2, 3)) == (1, 2, 3)
    ```

    Args:
        items_schema: The value must be a tuple with items that match this schema
        min_length: The value must be a tuple with at least this many items
        max_length: The value must be a tuple with at most this many items
        strict: The value must be a tuple with exactly this many items
        ref: Optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return tuple_schema(
        items_schema=[items_schema or any_schema()],
        variadic_item_index=0,
        min_length=min_length,
        max_length=max_length,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

typed_dict_field

typed_dict_field(
    schema: CoreSchema,
    *,
    required: bool | None = None,
    validation_alias: str
    | list[str | int]
    | list[list[str | int]]
    | None = None,
    serialization_alias: str | None = None,
    serialization_exclude: bool | None = None,
    metadata: Any = None,
) -> TypedDictField

Returns a schema that matches a typed dict field, e.g.:

from pydantic_core import core_schema

field = core_schema.typed_dict_field(schema=core_schema.int_schema(), required=True)

Parameters:

Name Type Description Default
schema CoreSchema

The schema to use for the field

required
required bool | None

Whether the field is required

None
validation_alias str | list[str | int] | list[list[str | int]] | None

The alias(es) to use to find the field in the validation data

None
serialization_alias str | None

The alias to use as a key when serializing

None
serialization_exclude bool | None

Whether to exclude the field when serializing

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def typed_dict_field(
    schema: CoreSchema,
    *,
    required: bool | None = None,
    validation_alias: str | list[str | int] | list[list[str | int]] | None = None,
    serialization_alias: str | None = None,
    serialization_exclude: bool | None = None,
    metadata: Any = None,
) -> TypedDictField:
    """
    Returns a schema that matches a typed dict field, e.g.:

    ```py
    from pydantic_core import core_schema

    field = core_schema.typed_dict_field(schema=core_schema.int_schema(), required=True)
    ```

    Args:
        schema: The schema to use for the field
        required: Whether the field is required
        validation_alias: The alias(es) to use to find the field in the validation data
        serialization_alias: The alias to use as a key when serializing
        serialization_exclude: Whether to exclude the field when serializing
        metadata: Any other information you want to include with the schema, not used by pydantic-core
    """
    return _dict_not_none(
        type='typed-dict-field',
        schema=schema,
        required=required,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        serialization_exclude=serialization_exclude,
        metadata=metadata,
    )

typed_dict_schema

typed_dict_schema(
    fields: Dict[str, TypedDictField],
    *,
    computed_fields: list[ComputedField] | None = None,
    strict: bool | None = None,
    extras_schema: CoreSchema | None = None,
    extra_behavior: ExtraBehavior | None = None,
    total: bool | None = None,
    populate_by_name: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
    config: CoreConfig | None = None,
) -> TypedDictSchema

Returns a schema that matches a typed dict, e.g.:

from pydantic_core import SchemaValidator, core_schema

wrapper_schema = core_schema.typed_dict_schema(
    {'a': core_schema.typed_dict_field(core_schema.str_schema())}
)
v = SchemaValidator(wrapper_schema)
assert v.validate_python({'a': 'hello'}) == {'a': 'hello'}

Parameters:

Name Type Description Default
fields Dict[str, TypedDictField]

The fields to use for the typed dict

required
computed_fields list[ComputedField] | None

Computed fields to use when serializing the model, only applies when directly inside a model

None
strict bool | None

Whether the typed dict is strict

None
extras_schema CoreSchema | None

The extra validator to use for the typed dict

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
extra_behavior ExtraBehavior | None

The extra behavior to use for the typed dict

None
total bool | None

Whether the typed dict is total

None
populate_by_name bool | None

Whether the typed dict should populate by name

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def typed_dict_schema(
    fields: Dict[str, TypedDictField],
    *,
    computed_fields: list[ComputedField] | None = None,
    strict: bool | None = None,
    extras_schema: CoreSchema | None = None,
    extra_behavior: ExtraBehavior | None = None,
    total: bool | None = None,
    populate_by_name: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
    config: CoreConfig | None = None,
) -> TypedDictSchema:
    """
    Returns a schema that matches a typed dict, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    wrapper_schema = core_schema.typed_dict_schema(
        {'a': core_schema.typed_dict_field(core_schema.str_schema())}
    )
    v = SchemaValidator(wrapper_schema)
    assert v.validate_python({'a': 'hello'}) == {'a': 'hello'}
    ```

    Args:
        fields: The fields to use for the typed dict
        computed_fields: Computed fields to use when serializing the model, only applies when directly inside a model
        strict: Whether the typed dict is strict
        extras_schema: The extra validator to use for the typed dict
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        extra_behavior: The extra behavior to use for the typed dict
        total: Whether the typed dict is total
        populate_by_name: Whether the typed dict should populate by name
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='typed-dict',
        fields=fields,
        computed_fields=computed_fields,
        strict=strict,
        extras_schema=extras_schema,
        extra_behavior=extra_behavior,
        total=total,
        populate_by_name=populate_by_name,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
        config=config,
    )

union_schema

union_schema(
    choices: list[CoreSchema | tuple[CoreSchema, str]],
    *,
    auto_collapse: bool | None = None,
    custom_error_type: str | None = None,
    custom_error_message: str | None = None,
    custom_error_context: dict[str, str | int]
    | None = None,
    mode: Literal["smart", "left_to_right"] | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> UnionSchema

Returns a schema that matches a union value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.union_schema([core_schema.str_schema(), core_schema.int_schema()])
v = SchemaValidator(schema)
assert v.validate_python('hello') == 'hello'
assert v.validate_python(1) == 1

Parameters:

Name Type Description Default
choices list[CoreSchema | tuple[CoreSchema, str]]

The schemas to match. If a tuple, the second item is used as the label for the case.

required
auto_collapse bool | None

whether to automatically collapse unions with one element to the inner validator, default true

None
custom_error_type str | None

The custom error type to use if the validation fails

None
custom_error_message str | None

The custom error message to use if the validation fails

None
custom_error_context dict[str, str | int] | None

The custom error context to use if the validation fails

None
mode Literal['smart', 'left_to_right'] | None

How to select which choice to return * smart (default) will try to return the choice which is the closest match to the input value * left_to_right will return the first choice in choices which succeeds validation

None
strict bool | None

Whether the underlying schemas should be validated with strict mode

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def union_schema(
    choices: list[CoreSchema | tuple[CoreSchema, str]],
    *,
    auto_collapse: bool | None = None,
    custom_error_type: str | None = None,
    custom_error_message: str | None = None,
    custom_error_context: dict[str, str | int] | None = None,
    mode: Literal['smart', 'left_to_right'] | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> UnionSchema:
    """
    Returns a schema that matches a union value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.union_schema([core_schema.str_schema(), core_schema.int_schema()])
    v = SchemaValidator(schema)
    assert v.validate_python('hello') == 'hello'
    assert v.validate_python(1) == 1
    ```

    Args:
        choices: The schemas to match. If a tuple, the second item is used as the label for the case.
        auto_collapse: whether to automatically collapse unions with one element to the inner validator, default true
        custom_error_type: The custom error type to use if the validation fails
        custom_error_message: The custom error message to use if the validation fails
        custom_error_context: The custom error context to use if the validation fails
        mode: How to select which choice to return
            * `smart` (default) will try to return the choice which is the closest match to the input value
            * `left_to_right` will return the first choice in `choices` which succeeds validation
        strict: Whether the underlying schemas should be validated with strict mode
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='union',
        choices=choices,
        auto_collapse=auto_collapse,
        custom_error_type=custom_error_type,
        custom_error_message=custom_error_message,
        custom_error_context=custom_error_context,
        mode=mode,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

url_schema

url_schema(
    *,
    max_length: int | None = None,
    allowed_schemes: list[str] | None = None,
    host_required: bool | None = None,
    default_host: str | None = None,
    default_port: int | None = None,
    default_path: str | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> UrlSchema

Returns a schema that matches a URL value, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.url_schema()
v = SchemaValidator(schema)
print(v.validate_python('https://example.com'))
#> https://example.com/

Parameters:

Name Type Description Default
max_length int | None

The maximum length of the URL

None
allowed_schemes list[str] | None

The allowed URL schemes

None
host_required bool | None

Whether the URL must have a host

None
default_host str | None

The default host to use if the URL does not have a host

None
default_port int | None

The default port to use if the URL does not have a port

None
default_path str | None

The default path to use if the URL does not have a path

None
strict bool | None

Whether to use strict URL parsing

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def url_schema(
    *,
    max_length: int | None = None,
    allowed_schemes: list[str] | None = None,
    host_required: bool | None = None,
    default_host: str | None = None,
    default_port: int | None = None,
    default_path: str | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> UrlSchema:
    """
    Returns a schema that matches a URL value, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.url_schema()
    v = SchemaValidator(schema)
    print(v.validate_python('https://example.com'))
    #> https://example.com/
    ```

    Args:
        max_length: The maximum length of the URL
        allowed_schemes: The allowed URL schemes
        host_required: Whether the URL must have a host
        default_host: The default host to use if the URL does not have a host
        default_port: The default port to use if the URL does not have a port
        default_path: The default path to use if the URL does not have a path
        strict: Whether to use strict URL parsing
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='url',
        max_length=max_length,
        allowed_schemes=allowed_schemes,
        host_required=host_required,
        default_host=default_host,
        default_port=default_port,
        default_path=default_path,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

with_default_schema

with_default_schema(
    schema: CoreSchema,
    *,
    default: Any = PydanticUndefined,
    default_factory: Callable[[], Any] | None = None,
    on_error: Literal["raise", "omit", "default"]
    | None = None,
    validate_default: bool | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> WithDefaultSchema

Returns a schema that adds a default value to the given schema, e.g.:

from pydantic_core import SchemaValidator, core_schema

schema = core_schema.with_default_schema(core_schema.str_schema(), default='hello')
wrapper_schema = core_schema.typed_dict_schema(
    {'a': core_schema.typed_dict_field(schema)}
)
v = SchemaValidator(wrapper_schema)
assert v.validate_python({}) == v.validate_python({'a': 'hello'})

Parameters:

Name Type Description Default
schema CoreSchema

The schema to add a default value to

required
default Any

The default value to use

PydanticUndefined
default_factory Callable[[], Any] | None

A function that returns the default value to use

None
on_error Literal['raise', 'omit', 'default'] | None

What to do if the schema validation fails. One of 'raise', 'omit', 'default'

None
validate_default bool | None

Whether the default value should be validated

None
strict bool | None

Whether the underlying schema should be validated with strict mode

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def with_default_schema(
    schema: CoreSchema,
    *,
    default: Any = PydanticUndefined,
    default_factory: Callable[[], Any] | None = None,
    on_error: Literal['raise', 'omit', 'default'] | None = None,
    validate_default: bool | None = None,
    strict: bool | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> WithDefaultSchema:
    """
    Returns a schema that adds a default value to the given schema, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    schema = core_schema.with_default_schema(core_schema.str_schema(), default='hello')
    wrapper_schema = core_schema.typed_dict_schema(
        {'a': core_schema.typed_dict_field(schema)}
    )
    v = SchemaValidator(wrapper_schema)
    assert v.validate_python({}) == v.validate_python({'a': 'hello'})
    ```

    Args:
        schema: The schema to add a default value to
        default: The default value to use
        default_factory: A function that returns the default value to use
        on_error: What to do if the schema validation fails. One of 'raise', 'omit', 'default'
        validate_default: Whether the default value should be validated
        strict: Whether the underlying schema should be validated with strict mode
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    s = _dict_not_none(
        type='default',
        schema=schema,
        default_factory=default_factory,
        on_error=on_error,
        validate_default=validate_default,
        strict=strict,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )
    if default is not PydanticUndefined:
        s['default'] = default
    return s

with_info_after_validator_function

with_info_after_validator_function(
    function: WithInfoValidatorFunction,
    schema: CoreSchema,
    *,
    field_name: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> AfterValidatorFunctionSchema

Returns a schema that calls a validator function after validation, the function is called with an info argument, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(v: str, info: core_schema.ValidationInfo) -> str:
    assert info.data is not None
    assert info.field_name is not None
    return v + 'world'

func_schema = core_schema.with_info_after_validator_function(
    function=fn, schema=core_schema.str_schema(), field_name='a'
)
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

v = SchemaValidator(schema)
assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}

Parameters:

Name Type Description Default
function WithInfoValidatorFunction

The validator function to call after the schema is validated

required
schema CoreSchema

The schema to validate before the validator function

required
field_name str | None

The name of the field this validators is applied to, if any

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def with_info_after_validator_function(
    function: WithInfoValidatorFunction,
    schema: CoreSchema,
    *,
    field_name: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> AfterValidatorFunctionSchema:
    """
    Returns a schema that calls a validator function after validation, the function is called with
    an `info` argument, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(v: str, info: core_schema.ValidationInfo) -> str:
        assert info.data is not None
        assert info.field_name is not None
        return v + 'world'

    func_schema = core_schema.with_info_after_validator_function(
        function=fn, schema=core_schema.str_schema(), field_name='a'
    )
    schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

    v = SchemaValidator(schema)
    assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}
    ```

    Args:
        function: The validator function to call after the schema is validated
        schema: The schema to validate before the validator function
        field_name: The name of the field this validators is applied to, if any
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='function-after',
        function=_dict_not_none(type='with-info', function=function, field_name=field_name),
        schema=schema,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

with_info_before_validator_function

with_info_before_validator_function(
    function: WithInfoValidatorFunction,
    schema: CoreSchema,
    *,
    field_name: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> BeforeValidatorFunctionSchema

Returns a schema that calls a validator function before validation, the function is called with an info argument, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(v: bytes, info: core_schema.ValidationInfo) -> str:
    assert info.data is not None
    assert info.field_name is not None
    return v.decode() + 'world'

func_schema = core_schema.with_info_before_validator_function(
    function=fn, schema=core_schema.str_schema(), field_name='a'
)
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

v = SchemaValidator(schema)
assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}

Parameters:

Name Type Description Default
function WithInfoValidatorFunction

The validator function to call

required
field_name str | None

The name of the field

None
schema CoreSchema

The schema to validate the output of the validator function

required
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def with_info_before_validator_function(
    function: WithInfoValidatorFunction,
    schema: CoreSchema,
    *,
    field_name: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> BeforeValidatorFunctionSchema:
    """
    Returns a schema that calls a validator function before validation, the function is called with
    an `info` argument, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(v: bytes, info: core_schema.ValidationInfo) -> str:
        assert info.data is not None
        assert info.field_name is not None
        return v.decode() + 'world'

    func_schema = core_schema.with_info_before_validator_function(
        function=fn, schema=core_schema.str_schema(), field_name='a'
    )
    schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})

    v = SchemaValidator(schema)
    assert v.validate_python({'a': b'hello '}) == {'a': 'hello world'}
    ```

    Args:
        function: The validator function to call
        field_name: The name of the field
        schema: The schema to validate the output of the validator function
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='function-before',
        function=_dict_not_none(type='with-info', function=function, field_name=field_name),
        schema=schema,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

with_info_plain_validator_function

with_info_plain_validator_function(
    function: WithInfoValidatorFunction,
    *,
    field_name: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> PlainValidatorFunctionSchema

Returns a schema that uses the provided function for validation, an info argument is passed, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(v: str, info: core_schema.ValidationInfo) -> str:
    assert 'hello' in v
    return v + 'world'

schema = core_schema.with_info_plain_validator_function(function=fn)
v = SchemaValidator(schema)
assert v.validate_python('hello ') == 'hello world'

Parameters:

Name Type Description Default
function WithInfoValidatorFunction

The validator function to call

required
field_name str | None

The name of the field this validators is applied to, if any

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def with_info_plain_validator_function(
    function: WithInfoValidatorFunction,
    *,
    field_name: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> PlainValidatorFunctionSchema:
    """
    Returns a schema that uses the provided function for validation, an `info` argument is passed, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(v: str, info: core_schema.ValidationInfo) -> str:
        assert 'hello' in v
        return v + 'world'

    schema = core_schema.with_info_plain_validator_function(function=fn)
    v = SchemaValidator(schema)
    assert v.validate_python('hello ') == 'hello world'
    ```

    Args:
        function: The validator function to call
        field_name: The name of the field this validators is applied to, if any
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='function-plain',
        function=_dict_not_none(type='with-info', function=function, field_name=field_name),
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

with_info_wrap_validator_function

with_info_wrap_validator_function(
    function: WithInfoWrapValidatorFunction,
    schema: CoreSchema,
    *,
    field_name: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> WrapValidatorFunctionSchema

Returns a schema which calls a function with a validator callable argument which can optionally be used to call inner validation with the function logic, this is much like the "onion" implementation of middleware in many popular web frameworks, an info argument is also passed, e.g.:

from pydantic_core import SchemaValidator, core_schema

def fn(
    v: str,
    validator: core_schema.ValidatorFunctionWrapHandler,
    info: core_schema.ValidationInfo,
) -> str:
    return validator(input_value=v) + 'world'

schema = core_schema.with_info_wrap_validator_function(
    function=fn, schema=core_schema.str_schema()
)
v = SchemaValidator(schema)
assert v.validate_python('hello ') == 'hello world'

Parameters:

Name Type Description Default
function WithInfoWrapValidatorFunction

The validator function to call

required
schema CoreSchema

The schema to validate the output of the validator function

required
field_name str | None

The name of the field this validators is applied to, if any

None
ref str | None

optional unique identifier of the schema, used to reference the schema in other places

None
metadata Any

Any other information you want to include with the schema, not used by pydantic-core

None
serialization SerSchema | None

Custom serialization schema

None
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def with_info_wrap_validator_function(
    function: WithInfoWrapValidatorFunction,
    schema: CoreSchema,
    *,
    field_name: str | None = None,
    ref: str | None = None,
    metadata: Any = None,
    serialization: SerSchema | None = None,
) -> WrapValidatorFunctionSchema:
    """
    Returns a schema which calls a function with a `validator` callable argument which can
    optionally be used to call inner validation with the function logic, this is much like the
    "onion" implementation of middleware in many popular web frameworks, an `info` argument is also passed, e.g.:

    ```py
    from pydantic_core import SchemaValidator, core_schema

    def fn(
        v: str,
        validator: core_schema.ValidatorFunctionWrapHandler,
        info: core_schema.ValidationInfo,
    ) -> str:
        return validator(input_value=v) + 'world'

    schema = core_schema.with_info_wrap_validator_function(
        function=fn, schema=core_schema.str_schema()
    )
    v = SchemaValidator(schema)
    assert v.validate_python('hello ') == 'hello world'
    ```

    Args:
        function: The validator function to call
        schema: The schema to validate the output of the validator function
        field_name: The name of the field this validators is applied to, if any
        ref: optional unique identifier of the schema, used to reference the schema in other places
        metadata: Any other information you want to include with the schema, not used by pydantic-core
        serialization: Custom serialization schema
    """
    return _dict_not_none(
        type='function-wrap',
        function=_dict_not_none(type='with-info', function=function, field_name=field_name),
        schema=schema,
        ref=ref,
        metadata=metadata,
        serialization=serialization,
    )

wrap_serializer_function_ser_schema

wrap_serializer_function_ser_schema(
    function: WrapSerializerFunction,
    *,
    is_field_serializer: bool | None = None,
    info_arg: bool | None = None,
    schema: CoreSchema | None = None,
    return_schema: CoreSchema | None = None,
    when_used: WhenUsed = "always",
) -> WrapSerializerFunctionSerSchema

Returns a schema for serialization with a wrap function, can be either a "general" or "field" function.

Parameters:

Name Type Description Default
function WrapSerializerFunction

The function to use for serialization

required
is_field_serializer bool | None

Whether the serializer is for a field, e.g. takes model as the first argument, and info includes field_name

None
info_arg bool | None

Whether the function takes an __info argument

None
schema CoreSchema | None

The schema to use for the inner serialization

None
return_schema CoreSchema | None

Schema to use for serializing return value

None
when_used WhenUsed

When the function should be called

'always'
Source code in .venv/lib/python3.12/site-packages/pydantic_core/core_schema.py
def wrap_serializer_function_ser_schema(
    function: WrapSerializerFunction,
    *,
    is_field_serializer: bool | None = None,
    info_arg: bool | None = None,
    schema: CoreSchema | None = None,
    return_schema: CoreSchema | None = None,
    when_used: WhenUsed = 'always',
) -> WrapSerializerFunctionSerSchema:
    """
    Returns a schema for serialization with a wrap function, can be either a "general" or "field" function.

    Args:
        function: The function to use for serialization
        is_field_serializer: Whether the serializer is for a field, e.g. takes `model` as the first argument,
            and `info` includes `field_name`
        info_arg: Whether the function takes an `__info` argument
        schema: The schema to use for the inner serialization
        return_schema: Schema to use for serializing return value
        when_used: When the function should be called
    """
    if when_used == 'always':
        # just to avoid extra elements in schema, and to use the actual default defined in rust
        when_used = None  # type: ignore
    return _dict_not_none(
        type='function-wrap',
        function=function,
        is_field_serializer=is_field_serializer,
        info_arg=info_arg,
        schema=schema,
        return_schema=return_schema,
        when_used=when_used,
    )

has_recursion_error

has_recursion_error(error: ValidationError) -> bool

Check if the given validation error is a recursion loop error.

Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/core.py
def has_recursion_error(error: ValidationError) -> bool:
    """Check if the given validation error is a recursion loop error."""
    errors = error.errors()
    return errors[0]['type'] == 'recursion_loop'

recursion_manager

recursion_manager(
    value: str | None = None,
    *,
    mode: Literal["lax", "omit", "raise"] | None = None,
    new: Literal[False] = False,
    on_missing: Literal["skip"],
) -> Iterator[RecursionState | None]
recursion_manager(
    value: str | None = None,
    *,
    mode: Literal["lax", "omit", "raise"] | None = None,
    new: bool = False,
    on_missing: Literal["create", "raise"] = "create",
) -> Iterator[RecursionState]
recursion_manager(
    value: str | None = None,
    *,
    mode: Literal["lax", "omit", "raise"] | None = None,
    new: bool = False,
    on_missing: Literal[
        "create", "raise", "skip"
    ] = "create",
) -> Iterator[RecursionState | None]

A recursion context manager for validation.

Parameters:

Name Type Description Default
value str | None

The value to add to the recursion context. If set to None, the recursion context is not modified when available. Defaults to None.

None
mode Literal['lax', 'omit', 'raise'] | None

The mode for handling recursion errors. It can be either: - 'lax': Validate leniently the source value even when a recursion loop is detected, and omit only for recursion validation errors, i.e. when the same instance is visited twice. - 'omit': Skip the validation and omit the source value. - 'raise': Raise a validation error when a recursion loop is detected, or when the same instance is visited twice. When set to None, the current mode found in recursion context is used, otherwise it falls back to 'omit'. Defaults to None.

None
new bool

Whether to initialize a new recursion context or not. If set to True, a new recursion context is created. If set to False, the current recursion context is used if available, otherwise it follows the on_missing behavior. Defaults to False.

False
on_missing Literal['create', 'raise', 'skip']

The behavior to follow when no current recursion context is available, either to create a new recursion context, raise an error, or skip the operation. Defaults to 'create'.

'create'

Raises:

Type Description
PydanticOmit

Either if the recursion mode is set to 'omit' and a recursion loop is detected or a recursion validation error occurs, or if the recursion mode is set to 'lax' and a recursion validation error occurs.

RecursionError

If the recursion mode is set to 'raise' and a recursion loop is detected or a validation error occurs, i.e. when the same instance is visited twice.

RuntimeError

If the recursion context is not set when required.

Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/core.py
@contextmanager
def recursion_manager(
    value: str | None = None,
    *,
    mode: Literal['lax', 'omit', 'raise'] | None = None,
    new: bool = False,
    on_missing: Literal['create', 'raise', 'skip'] = 'create',
) -> Iterator[RecursionState | None]:
    """A recursion context manager for validation.

    Args:
        value: The value to add to the recursion context. If set to ``None``,
            the recursion context is not modified when available.
            Defaults to ``None``.
        mode: The mode for handling recursion errors. It can be either:
            - ``'lax'``: Validate leniently the source value even when a
                recursion loop is detected, and omit only for recursion
                validation errors, i.e. when the same instance is visited
                twice.
            - ``'omit'``: Skip the validation and omit the source value.
            - ``'raise'``: Raise a validation error when a recursion loop is
                detected, or when the same instance is visited twice.
            When set to ``None``, the current mode found in recursion context
            is used, otherwise it falls back to ``'omit'``.
            Defaults to ``None``.
        new: Whether to initialize a new recursion context or not. If set to
            ``True``, a new recursion context is created. If set to ``False``,
            the current recursion context is used if available, otherwise it
            follows the `on_missing` behavior. Defaults to ``False``.
        on_missing: The behavior to follow when no current recursion context is
            available, either to create a new recursion context, raise an
            error, or skip the operation. Defaults to ``'create'``.

    Raises:
        PydanticOmit: Either if the recursion mode is set to ``'omit'`` and a
            recursion loop is detected or a recursion validation error occurs,
            or if the recursion mode is set to ``'lax'`` and a recursion
            validation error occurs.
        RecursionError: If the recursion mode is set to ``'raise'`` and a
            recursion loop is detected or a validation error occurs, i.e. when
            the same instance is visited twice.
        RuntimeError: If the recursion context is not set when required.
    """
    context = RECURSION_CONTEXT.get()

    # Resolve recursion context
    if new is False:
        context = RECURSION_CONTEXT.get()
        if context is not None:
            recursive_state = context.state
            recursive_mode = mode or context.mode
        elif on_missing == 'raise':
            raise RuntimeError(
                "No recursion available in the current context where creating "
                "a new recursion context is not allowed `on_missing='raise'`."
            )
        elif on_missing == 'skip':
            yield None
            return

    if new is True or context is None:
        recursive_state = frozenset()
        recursive_mode = mode or 'omit'

    # Helper function to handle recursion error
    def handle_error(source: ValidationError | None = None) -> None:
        if recursive_mode == 'omit':
            raise PydanticOmit
        elif recursive_mode == 'lax':
            if source is None:
                return
            raise PydanticOmit
        raise RecursionError(
            f"Recursion loop detected for field {value!r} within the "
            f"recursion context: {recursive_state}."
        ) from source

    # Update recursion context
    if value is not None:
        if value in recursive_state:
            handle_error()
        else:
            recursive_state = recursive_state | {value}

    recursion = RecursionState(recursive_state, recursive_mode)

    # Yield and reset recursion context
    token = RECURSION_CONTEXT.set(recursion)
    try:
        yield recursion
    except ValidationError as error:
        if has_recursion_error(error):
            handle_error(error)
        raise error
    finally:
        RECURSION_CONTEXT.reset(token)

validation_manager

validation_manager(
    *,
    mode: Literal["disabled", "default", "strict"]
    | None = None,
) -> Iterator[ValidationMode]

A context manager for validation and partial initialization.

If the validation mode is not specified, either the current validation mode is set to 'default' if no validation context is available, or the current validation mode is used. The validation mode is reset to the previous state after the context manager exits.

Parameters:

Name Type Description Default
mode Literal['disabled', 'default', 'strict'] | None

The validation mode to set the context to. It can be either one of the following options: - None: The validation mode is set to the current context mode if available, otherwise it falls back to 'default'. - 'disabled': The validation is disabled. - 'default': The validation is enabled. - 'strict': The validation is strictly enforced. Defaults to None.

None

Raises:

Type Description
ValueError

If an invalid validation mode is provided.

Source code in .venv/lib/python3.12/site-packages/plateforme/core/schema/core.py
@contextmanager
def validation_manager(
    *, mode: Literal['disabled', 'default', 'strict'] | None = None
) -> Iterator[ValidationMode]:
    """A context manager for validation and partial initialization.

    If the validation mode is not specified, either the current validation mode
    is set to ``'default'`` if no validation context is available, or the
    current validation mode is used. The validation mode is reset to the
    previous state after the context manager exits.

    Args:
        mode: The validation mode to set the context to. It can be either one
            of the following options:
            - ``None``: The validation mode is set to the current context mode
                if available, otherwise it falls back to ``'default'``.
            - ``'disabled'``: The validation is disabled.
            - ``'default'``: The validation is enabled.
            - ``'strict'``: The validation is strictly enforced.
            Defaults to ``None``.

    Raises:
        ValueError: If an invalid validation mode is provided.
    """
    context = VALIDATION_CONTEXT.get()

    # Set validation mode
    if mode is None:
        if context is not None:
            validation = context
        validation = ValidationMode.DEFAULT
    elif mode == 'disabled':
        validation = ValidationMode.DISABLED
    elif mode == 'default':
        validation = ValidationMode.DEFAULT
    elif mode == 'strict':
        validation = ValidationMode.STRICT
    else:
        raise ValueError(
            f"Invalid validation mode {mode!r} provided. Must be one of: "
            f"'disabled', 'default', or 'strict'."
        )

    # Yield and reset validation context
    token = VALIDATION_CONTEXT.set(validation)
    try:
        yield validation
    finally:
        VALIDATION_CONTEXT.reset(token)