1313from dlt .common .schema .typing import TTableReferenceParam
1414from dlt .common .libs .pydantic import DltConfig
1515from pydantic_flatten_rootmodel import flatten_root_model
16- from pydantic import TypeAdapter
16+ from pydantic import BaseModel , TypeAdapter
1717from pydantic .fields import FieldInfo
1818from .rest_client import (
1919 get_v1_rest_client ,
2828from .helpers import ListReference , generate_list_entries_path
2929
3030
31+ def pydantic_model_dump (model : BaseModel , ** kwargs ):
32+ """
33+ Dumps a Pydantic model to a dictionary, using the model's field names as keys AND observing the field aliases,
34+ which is important for DLT to correctly map the data to the destination.
35+ """
36+ return model .model_dump (by_alias = True , ** kwargs )
37+
38+
3139if is_logging ():
3240 # ignore https://github.com/dlt-hub/dlt/blob/268768f78bd7ea7b2df8ca0722faa72d4d4614c5/dlt/extract/hints.py#L390-L393
3341 # This warning is thrown because of using Pydantic models as the column schema in a table variant
@@ -84,7 +92,7 @@ def get_entity_data_class_paged(entity: ENTITY):
8492
8593
8694def use_id (entity : Company | Person | Opportunity | ListModel ):
87- return entity . model_dump ( ) | {"_dlt_id" : entity .id }
95+ return pydantic_model_dump ( entity ) | {"_dlt_id" : entity .id }
8896
8997
9098def __create_id_resource (
@@ -194,7 +202,8 @@ def mark_dropdown_item(
194202 dropdown_item : Dropdown | RankedDropdown , field : FieldModel
195203) -> DataItemWithMeta :
196204 return dlt .mark .with_hints (
197- item = dropdown_item .model_dump () | {"_dlt_id" : dropdown_item .dropdownOptionId },
205+ item = pydantic_model_dump (dropdown_item )
206+ | {"_dlt_id" : dropdown_item .dropdownOptionId },
198207 hints = dlt .mark .make_hints (
199208 table_name = get_dropdown_options_table (field ),
200209 write_disposition = "replace" ,
@@ -217,7 +226,7 @@ def process_and_yield_fields(
217226 return (ret , references )
218227 for field in entity .fields :
219228 yield dlt .mark .with_hints (
220- item = field . model_dump ( exclude = {"value" })
229+ item = pydantic_model_dump ( field , exclude = {"value" })
221230 | {"value_type" : field .value .root .type , "_dlt_id" : field .id },
222231 hints = dlt .mark .make_hints (
223232 table_name = Table .FIELDS .value ,
@@ -263,9 +272,12 @@ def process_and_yield_fields(
263272 ret [new_column ] = None
264273 continue
265274 interaction = value .data .root
266- ret [new_column ] = interaction .model_dump (include = {"id" , "type" })
275+ ret [new_column ] = pydantic_model_dump (
276+ interaction , include = {"id" , "type" }
277+ )
267278 yield dlt .mark .with_hints (
268- item = interaction .model_dump (),
279+ item = pydantic_model_dump (interaction )
280+ | {"_dlt_id" : f"{ interaction .type } _{ interaction .id } " },
269281 hints = dlt .mark .make_hints (
270282 columns = FlattenedInteraction ,
271283 table_name = Table .INTERACTIONS .value ,
@@ -346,7 +358,9 @@ def __entities(
346358 for e in entities .data :
347359 (ret , references ) = yield from process_and_yield_fields (e , name )
348360 yield dlt .mark .with_hints (
349- item = e .model_dump (exclude = {"fields" }) | ret | {"_dlt_id" : e .id },
361+ item = pydantic_model_dump (e , exclude = {"fields" })
362+ | ret
363+ | {"_dlt_id" : e .id },
350364 hints = dlt .mark .make_hints (
351365 table_name = name ,
352366 references = references ,
@@ -409,7 +423,7 @@ def __list_entries() -> Iterable[TDataItem]:
409423 (ret , references ) = gen .value
410424
411425 combined_list_entry = (
412- e . model_dump ( exclude = {"entity" })
426+ pydantic_model_dump ( e , exclude = {"entity" })
413427 | ret
414428 | {"_dlt_id" : e .id , "entity_id" : e .entity .id }
415429 )
0 commit comments