cdm_data_loaders.converters.facades.pydantic_to_pyarrow ¶
Convert Pydantic v2 BaseModel schemas to PyArrow schemas.
PydanticToPyArrowError ¶
Bases: ConversionError
Raised when a Pydantic model construct cannot be converted to a PyArrow schema.
Subclasses raised by :func:generate_pyarrow_schema carry a stable specification code and a short
title; their docstrings state why the construct is rejected. The converter specification document is
generated from these attributes.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
InvalidPydanticModelError ¶
Bases: PydanticToPyArrowError
Converter requires a concrete Pydantic BaseModel subclass as input.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
NonStructRootError ¶
Bases: PydanticToPyArrowError
Root of a PyArrow schema must be a struct (a table has named columns, not a bare scalar, list, or map).
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
EmptyModelError ¶
Bases: PydanticToPyArrowError
Parquet cannot persist a struct with zero child fields.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
UnconstrainedFieldError ¶
Bases: PydanticToPyArrowError
PyArrow requires a concrete static column type and has no dynamic any or untyped container column.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
OpenTupleError ¶
Bases: PydanticToPyArrowError
Positional tuple structs require a fixed element count so every position maps to a deterministic field.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
NonStringMapKeyError ¶
Bases: PydanticToPyArrowError
Fixed-field structs cannot mix open extra='allow' maps, and Arrow maps require string keys for JSONL round-tripping.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
UnsupportedFieldTypeError ¶
Bases: PydanticToPyArrowError
Non-serializable Python runtime types and null-only fields have no valid Arrow column representation.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
IncompatibleUnionBranchesError ¶
Bases: PydanticToPyArrowError
PyArrow cannot merge lists, maps, and structs into a single column without an opaque union type.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
UnresolvedForwardRefError ¶
Bases: PydanticToPyArrowError
Schema generation requires all forward type annotations to be resolved to concrete types.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
RecursiveNonJsonTypeError ¶
Bases: PydanticToPyArrowError
Truncated recursive subtrees are serialized with json.dumps, which cannot encode datetime.date or bytes; cyclic type aliases without a BaseModel boundary cannot be finitely unrolled.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
InvalidDecimalConstraintsError ¶
Bases: PydanticToPyArrowError
Arrow decimal128 requires 1 <= max_digits <= 38 and 0 <= decimal_places <= max_digits.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
ValueFieldCollisionError ¶
Bases: PydanticToPyArrowError
The synthesized _value field for scalar union branches would collide with the model's declared _value field.
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
PydanticToPyArrow
pydantic-model
¶
Bases: BaseModel
Facade converting Pydantic v2 BaseModel classes to PyArrow schemas and empty tables.
Show JSON schema:
{
"description": "Facade converting Pydantic v2 ``BaseModel`` classes to PyArrow schemas and empty tables.",
"properties": {
"max_recursion_depth": {
"default": 2,
"exclusiveMinimum": 0,
"title": "Max Recursion Depth",
"type": "integer"
},
"by_alias": {
"default": false,
"title": "By Alias",
"type": "boolean"
}
},
"title": "PydanticToPyArrow",
"type": "object"
}
Config:
arbitrary_types_allowed:Truefrozen:True
Fields:
-
max_recursion_depth(PositiveInt) -
by_alias(bool)
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
convert ¶
Convert a Pydantic BaseModel subclass to a pa.Schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[BaseModel]
|
concrete Pydantic |
required |
Returns:
| Type | Description |
|---|---|
pa.Schema
|
converted PyArrow schema |
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
convert_to_table ¶
Convert a Pydantic BaseModel subclass to a zero-row pa.Table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[BaseModel]
|
concrete Pydantic |
required |
Returns:
| Type | Description |
|---|---|
pa.Table
|
zero-row PyArrow table with the converted schema |
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
convert_to_python ¶
Convert a Pydantic BaseModel subclass to a static Python schema module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[BaseModel]
|
concrete Pydantic |
required |
docstring
|
str | None
|
optional module docstring header |
None
|
Returns:
| Type | Description |
|---|---|
str
|
formatted Python source code defining |
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
convert_to_python_file ¶
convert_to_python_file(
model: type[BaseModel],
output: str | Path,
*,
docstring: str | None = None
) -> Path
Convert a Pydantic BaseModel subclass and write a static Python schema module to output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[BaseModel]
|
concrete Pydantic |
required |
output
|
str | Path
|
destination file path for the generated Python module |
required |
docstring
|
str | None
|
optional module docstring header |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
resolved output |
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
resolve_scalar_type ¶
Map a scalar Python / Pydantic type annotation to its PyArrow DataType and pydantic_type label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
annotation
|
object
|
scalar type annotation (e.g. |
required |
Returns:
| Type | Description |
|---|---|
tuple[pa.DataType, str]
|
|
Raises:
| Type | Description |
|---|---|
PydanticToPyArrowError
|
if |
Source code in src/cdm_data_loaders/converters/facades/pydantic_to_pyarrow.py
generate_pyarrow_schema ¶
generate_pyarrow_schema(
model: type[BaseModel],
*,
max_recursion_depth: int = DEFAULT_MAX_RECURSION_DEPTH,
by_alias: bool = False
) -> pa.Schema
Generate a deterministic PyArrow Schema from a Pydantic v2 BaseModel subclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[BaseModel]
|
concrete Pydantic |
required |
max_recursion_depth
|
int
|
maximum expansion depth for recursive |
DEFAULT_MAX_RECURSION_DEPTH
|
by_alias
|
bool
|
when |
False
|
Returns:
| Type | Description |
|---|---|
pa.Schema
|
PyArrow schema with top-level fields corresponding to model fields |
Raises:
| Type | Description |
|---|---|
PydanticToPyArrowError
|
if |