Convert Pydantic models to Polars schemas
Project description
🧩 Poldantic
Poldantic converts Pydantic models into Polars schemas — making it easy to validate, align, and document your data pipeline types between the Python and Polars worlds.
✨ Features
- 🔁 Convert
BaseModelclasses topolars.Schemaorpl.Struct - ✅ Handles optional fields (
Optional[T]) and union types - 🧱 Supports nested and deeply nested Pydantic models
- 📦 Outputs multiple formats:
dict,list,pl.Schema,pl.Struct - 🧠 Tracks
nullablestatus viaFieldInfo
📦 Installation
pip install poldantic
🚀 Usage
from pydantic import BaseModel
from typing import Optional, List
import polars as pl
import poldantic as pd
class Address(BaseModel):
street: str
zip: Optional[int]
class User(BaseModel):
id: int
name: str
address: Address
tags: List[str]
pd.get_polars_schema_dict(User)
# {'id': pl.Int64, 'name': pl.Utf8, 'address': pl.Struct([...]), 'tags': pl.List(pl.Utf8)}
pd.get_polars_schema(User)
# pl.Schema({...})
pd.get_polars_struct(User)
# pl.Struct([...])
pd.get_polars_fieldinfo_dict(User)
# {'name': FieldInfo(dtype=Utf8, nullable=False), ...}
🧪 Output Formats
| Function | Output |
|---|---|
get_polars_schema_dict() |
dict[str, pl.DataType] |
get_polars_schema_list() |
list[(str, pl.DataType)] |
get_polars_fieldinfo_dict() |
dict[str, FieldInfo] |
get_polars_fieldinfo_list() |
list[(str, FieldInfo)] |
get_polars_schema() |
pl.Schema |
get_polars_struct() |
pl.Struct |
🔍 FieldInfo
The FieldInfo object tracks both the Polars data type and its nullability:
@dataclass
class FieldInfo:
dtype: pl.DataType
nullable: bool = False
📚 Examples
See examples/poldantic_demo.ipynb for a working notebook showing nested models and schema inspection.
📜 License
MIT License © 2025 Odos Matthews
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file poldantic-0.1.0.tar.gz.
File metadata
- Download URL: poldantic-0.1.0.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8805a7412b763d77c94e3047ab342558b1846d9b1de8ee078f01fac67be43fea
|
|
| MD5 |
30df84a9f7a3663a183f8243a3dba3ba
|
|
| BLAKE2b-256 |
136dfc08c2a739c346705462fcf8d333e638f7b8489274c9bb1b5464ffd1fc95
|
File details
Details for the file poldantic-0.1.0-py3-none-any.whl.
File metadata
- Download URL: poldantic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ea13d3e9e681b767fa7afc84cb1c8ccc25760c9bbac7e074afef65ccc421e2b
|
|
| MD5 |
78c65ac4819b71893e4e80b1fccc3d00
|
|
| BLAKE2b-256 |
86d1dedf82893d11628dae179a5d24dc6f4186bead3aa52707b7b86554b52834
|