cdm_data_loaders.coercion.pydantic ¶
Coerce Pydantic v2 BaseModel instances and record dictionaries to match a target PyArrow schema.
coerce_record_for_pyarrow ¶
coerce_record_for_pyarrow(
record: BaseModel | Mapping[str, Any],
schema: Schema,
*,
model: type[BaseModel] | None = None,
by_alias: bool = False
) -> dict[str, Any]
Coerce a Pydantic BaseModel instance or record mapping to match a PyArrow schema.
When record is a BaseModel instance (or model is supplied to validate a mapping),
non-null default values and @computed_field properties are populated on the output dictionary.
Only fields declared in schema are retained and coerced recursively so
pyarrow.Table.from_pylist can construct a table matching schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
BaseModel | Mapping[str, Any]
|
Pydantic |
required |
schema
|
Schema
|
Target PyArrow schema for the record's table. |
required |
model
|
type[BaseModel] | None
|
Optional |
None
|
by_alias
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Coerced record dictionary ready for |