Convert Pydantic models to Polars schemas
Project description
🧩 Poldantic
Convert Pydantic models into Polars schemas — and back again.
Poldantic makes it easy to bridge the world of data validation (Pydantic) with blazing-fast dataframes (Polars). Useful for type-safe ETL pipelines, FastAPI integrations, and schema round-tripping.
✨ Features
- 🔁 Two-way conversion between Pydantic models and Polars schema dicts
- 🧠 Smart handling of nested structs, lists, and mixed types
- 🛠 Configurable
Optional[...]wrapping to reflect Polars' nullable-by-default nature - 🧪 100% test coverage including complex edge cases
- 🧱 Minimal dependencies, ready for production
📦 Installation
pip install poldantic
Make sure you also have:
pip install pydantic polars
🧪 Basic Usage
✅ Convert a Pydantic model to a Polars schema
from poldantic import to_polars_schema
from pydantic import BaseModel
from typing import List, Optional
class Person(BaseModel):
name: str
tags: Optional[List[str]]
schema = to_polars_schema(Person)
print(schema)
Output:
{'name': Utf8, 'tags': List[Utf8]}
✅ Convert a Polars schema to a Pydantic model
from poldantic import to_pydantic_model
import polars as pl
schema = {
"name": pl.Utf8,
"tags": pl.List(pl.Utf8),
}
Model = to_pydantic_model(schema)
print(Model(name="Alice", tags=["x", "y"]))
Output:
name='Alice' tags=['x', 'y']
🧬 Nested Example
class Address(BaseModel):
street: str
zip: int
class Customer(BaseModel):
id: int
address: Address
to_polars_schema(Customer)
Output:
{
'id': Int64,
'address': Struct({'street': Utf8, 'zip': Int64})
}
⚙️ API
to_polars_schema(model: Type[BaseModel]) -> dict[str, pl.DataType]
to_pydantic_model(
schema: dict[str, pl.DataType],
model_name: str = "PolarsModel",
force_optional: bool = True
) -> Type[BaseModel]
🧬 Supported Type Mappings
| Pydantic Type | Polars Type |
|---|---|
int |
pl.Int64() |
float |
pl.Float64() |
str |
pl.Utf8() |
bool |
pl.Boolean() |
bytes |
pl.Binary() |
datetime.date |
pl.Date() |
datetime.datetime |
pl.Datetime() |
datetime.time |
pl.Time() |
List[T] |
pl.List(T) |
Model (nested) |
pl.Struct(...) |
| Unknown/Mixed/Union | pl.Object() |
✅ Tests
Run the full suite with:
pytest
📚 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.2.0.tar.gz.
File metadata
- Download URL: poldantic-0.2.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c28bef68937f096ddd51fbda860ef269b718217a8aa7aabc2a317710bbb1a79
|
|
| MD5 |
68640312c4a06f7f580aa4e25980bb42
|
|
| BLAKE2b-256 |
dbd6ecc0165e49254999f9fffa07c85a371378eb2366a8ebd3487fecce5bb18c
|
File details
Details for the file poldantic-0.2.0-py3-none-any.whl.
File metadata
- Download URL: poldantic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.1 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 |
31c15fab2b0fe341e590b7ecef2bc95dfee5cb1b9d1ecbc810af6780866aa875
|
|
| MD5 |
cfd1e4e1e853f8bee13a2b4e7e17c572
|
|
| BLAKE2b-256 |
e06b654a3bd3813ede32c88b73d41a3f9c3b75bfce34689e7319cde7c2663546
|