pydantic#

class litestar.plugins.pydantic.PydanticDTO#

Bases: AbstractDTO, Generic[T]

Support for domain modelling with Pydantic.

decode_builtins(value: dict[str, Any]) Any#

Decode a dictionary of Python values into an the DTO’s datatype.

decode_bytes(value: bytes) Any#

Decode a byte string into an the DTO’s datatype.

classmethod generate_field_definitions(model_type: type[pydantic_v1.BaseModel | pydantic_v2.BaseModel]) Generator[DTOFieldDefinition, None, None]#

Generate FieldDefinition instances from model_type.

Yields:

FieldDefinition instances.

classmethod detect_nested_field(field_definition: FieldDefinition) bool#

Return True if field_definition represents a nested model field.

Parameters:

field_definition – inspect type to determine if field represents a nested model.

Returns:

True if field_definition represents a nested model field.

classmethod get_config_for_model_type(config: DTOConfig, model_type: type[Any]) DTOConfig#

Create a new configuration for this specific model_type, during the creation of the factory.

The returned config object will be set as the config attribute on the newly defined factory class.

class litestar.plugins.pydantic.PydanticInitPlugin#

Bases: InitPluginProtocol

__init__(exclude: PydanticV1FieldsListType | PydanticV2FieldsListType | None = None, exclude_defaults: bool = False, exclude_none: bool = False, exclude_unset: bool = False, include: PydanticV1FieldsListType | PydanticV2FieldsListType | None = None, prefer_alias: bool = False, validate_strict: bool = False) None#

Pydantic Plugin to support serialization / validation of Pydantic types / models

Parameters:
  • exclude – Fields to exclude during serialization

  • exclude_defaults – Fields to exclude during serialization when they are set to their default value

  • exclude_none – Fields to exclude during serialization when they are set to None

  • exclude_unset – Fields to exclude during serialization when they arenot set

  • include – Fields to exclude during serialization

  • prefer_alias – Use the by_alias=True flag when dumping models

  • validate_strict – Use strict=True when calling .model_validate on Pydantic 2.x models

on_app_init(app_config: AppConfig) AppConfig#

Receive the AppConfig instance after on_app_init hooks have been called.

Examples

from litestar import Litestar, get
from litestar.di import Provide
from litestar.plugins import InitPluginProtocol


def get_name() -> str:
    return "world"


@get("/my-path")
def my_route_handler(name: str) -> dict[str, str]:
    return {"hello": name}


class MyPlugin(InitPluginProtocol):
    def on_app_init(self, app_config: AppConfig) -> AppConfig:
        app_config.dependencies["name"] = Provide(get_name)
        app_config.route_handlers.append(my_route_handler)
        return app_config


app = Litestar(plugins=[MyPlugin()])
Parameters:

app_config – The AppConfig instance.

Returns:

The app config object.

class litestar.plugins.pydantic.PydanticSchemaPlugin#

Bases: OpenAPISchemaPlugin

__init__(prefer_alias: bool = False) None#
static is_plugin_supported_type(value: Any) bool#

Given a value of indeterminate type, determine if this value is supported by the plugin.

This is called by the default implementation of is_plugin_supported_field() for backwards compatibility. User’s should prefer to override that method instead.

Parameters:

value – An arbitrary value.

Returns:

A bool indicating whether the value is supported by the plugin.

static is_undefined_sentinel(value: Any) bool#

Return True if value should be treated as an undefined field

static is_constrained_field(field_definition: FieldDefinition) bool#

Return True if the field should be treated as constrained. If returning True, constraints should be defined in the field’s extras

to_openapi_schema(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema#

Given a type annotation, transform it into an OpenAPI schema class.

Parameters:
  • field_definition – FieldDefinition instance.

  • schema_creator – An instance of the schema creator class

Returns:

An OpenAPI instance.

classmethod for_pydantic_model(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema#

Create a schema object for a given pydantic model class.

Parameters:
  • field_definition – FieldDefinition instance.

  • schema_creator – An instance of the schema creator class

Returns:

A schema instance.

class litestar.plugins.pydantic.PydanticPlugin#

Bases: InitPluginProtocol

A plugin that provides Pydantic integration.

__init__(exclude: PydanticV1FieldsListType | PydanticV2FieldsListType | None = None, exclude_defaults: bool = False, exclude_none: bool = False, exclude_unset: bool = False, include: PydanticV1FieldsListType | PydanticV2FieldsListType | None = None, prefer_alias: bool = False, validate_strict: bool = False) None#

Pydantic Plugin to support serialization / validation of Pydantic types / models

Parameters:
  • exclude – Fields to exclude during serialization

  • exclude_defaults – Fields to exclude during serialization when they are set to their default value

  • exclude_none – Fields to exclude during serialization when they are set to None

  • exclude_unset – Fields to exclude during serialization when they arenot set

  • include – Fields to exclude during serialization

  • prefer_alias – Use the by_alias=True flag when dumping models

  • validate_strict – Use strict=True when calling .model_validate on Pydantic 2.x models

on_app_init(app_config: AppConfig) AppConfig#

Configure application for use with Pydantic.

Parameters:

app_config – The AppConfig instance.

class litestar.plugins.pydantic.PydanticDIPlugin#

Bases: DIPlugin

has_typed_init(type_: Any) bool#

Return True if type_ has type information available for its __init__() method that cannot be extracted from this method’s type annotations (e.g. a Pydantic BaseModel subclass), and DIPlugin.get_typed_init() supports extraction of these annotations.

get_typed_init(type_: Any) tuple[inspect.Signature, dict[str, Any]]#

Return signature and type information about the type_s __init__() method.