Skip to main content
Version: devel

dlt.common.libs.pydantic

DltConfig Objects

class DltConfig(TypedDict)

View source on GitHub

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 - # deprecated

    return_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]]

View source on GitHub

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 from model when not provided.

pydantic_to_table_schema_columns

def pydantic_to_table_schema_columns(
model: Union[BaseModel, Type[BaseModel]]) -> TTableSchemaColumns

View source on GitHub

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]

View source on GitHub

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]

View source on GitHub

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]]

View source on GitHub

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]

View source on GitHub

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]

View source on GitHub

Validates item against model model and returns an instance of it.

Returns None for discard_row when validation fails, raises DataValidationError for freeze mode.

This demo works on codespaces. Codespaces is a development environment available for free to anyone with a Github account. You'll be asked to fork the demo repository and from there the README guides you with further steps.
The demo uses the Continue VSCode extension.

Off to codespaces!

DHelp

Ask a question

Welcome to "Codex Central", your next-gen help center, driven by OpenAI's GPT-4 model. It's more than just a forum or a FAQ hub – it's a dynamic knowledge base where coders can find AI-assisted solutions to their pressing problems. With GPT-4's powerful comprehension and predictive abilities, Codex Central provides instantaneous issue resolution, insightful debugging, and personalized guidance. Get your code running smoothly with the unparalleled support at Codex Central - coding help reimagined with AI prowess.