dlt.common.libs.pydantic
DltConfig Objects
class DltConfig(TypedDict)
dlt configuration that can be attached to a Pydantic model.
Example:
class ItemModel(BaseModel):
field: int
dlt_config: ClassVar[DltConfig] = {"return_validated_models": True}
Options:
skip_nested_types:
If True, columns of complex types (dict, list, BaseModel) are excluded
from the schema generated from the model.
-
skip_complex_types- # deprecatedreturn_validated_models: If True, the Pydantic validator returns validated Pydantic model instances instead of converting them to dictionaries during extraction/transform steps. Defaults to False to preserve current behavior.
is_authoritative_model: If True, the Pydantic model is treated as the authoritative source of table schema. Columns derived from the model are pre-merged into the schema before contract checks, so they bypass column/data_type contract enforcement. When None (default), dlt automatically determines whether to treat the model as authoritative.
skip_complex_types
deprecated
resolve_variant_model
def resolve_variant_model(
model: Type[BaseModel],
item: Any,
discriminator_map: Optional[Tuple[str, Dict[str, Type[BaseModel]]]] = None
) -> Optional[Type[BaseModel]]
Resolve a discriminated union variant from an item's discriminator value.
Arguments:
model- A pydantic model, typically a RootModel with a discriminated union.item- A dict or model instance to read the discriminator value from.discriminator_map- Pre-computed result of_build_discriminator_map. Computed frommodelwhen not provided.
pydantic_to_table_schema_columns
def pydantic_to_table_schema_columns(
model: Union[BaseModel, Type[BaseModel]]) -> TTableSchemaColumns
Convert a pydantic model to a table schema columns dict
See also DltConfig for more control over how the schema is created
Arguments:
model- The pydantic model to convert. Can be a class or an instance.
Returns:
TTableSchemaColumns- table schema columns dict
get_extra_from_model
def get_extra_from_model(model: Type[BaseModel]) -> Optional[str]
Returns the extra setting from the model or None if not explicitly set.
apply_schema_contract_to_model
def apply_schema_contract_to_model(
model: Type[_TPydanticModel],
column_mode: TSchemaEvolutionMode,
data_mode: TSchemaEvolutionMode = "freeze",
_child_models: Optional[Dict[int, Type[BaseModel]]] = None
) -> Type[_TPydanticModel]
Configures or re-creates model so it behaves according to column_mode and data_mode settings.
column_mode sets the model behavior when unknown field is found.
data_mode sets model behavior when known field does not validate. currently evolve and freeze are supported here.
discard_row is implemented in validate_item.
create_list_model
def create_list_model(
model: Type[_TPydanticModel],
column_mode: TSchemaEvolutionMode = "freeze",
data_mode: TSchemaEvolutionMode = "freeze"
) -> Type[ListModel[_TPydanticModel]]
Creates a model from model for validating list of items in batch.
When column_mode or data_mode is discard_row, creates a lenient list model
that uses WrapValidator to turn invalid items into None (filtered by the caller).
Otherwise creates a strict list model for batch validation.
validate_and_filter_items
def validate_and_filter_items(
table_name: str, list_model: Type[ListModel[_TPydanticModel]],
items: List[TDataItem], column_mode: TSchemaEvolutionMode,
data_mode: TSchemaEvolutionMode) -> List[_TPydanticModel]
Validates list of item with list_model and returns parsed Pydantic models.
validate_and_filter_item
def validate_and_filter_item(
table_name: str, model: Type[_TPydanticModel], item: TDataItems,
column_mode: TSchemaEvolutionMode,
data_mode: TSchemaEvolutionMode) -> Optional[_TPydanticModel]
Validates item against model model and returns an instance of it.
Returns None for discard_row when validation fails, raises DataValidationError for freeze mode.