Skip to main content

Convert Pydantic models to Polars schemas

Project description

🧩 Poldantic

Convert Pydantic models into Polars schemas — and back again.

Poldantic bridges the world of data validation (via Pydantic) and blazing-fast computation (via Polars). It's ideal for type-safe ETL pipelines, FastAPI response models, and schema round-tripping between Python classes and dataframes.


✨ Features

  • 🔁 Bidirectional conversion: Pydantic models ⇄ Polars schemas
  • 🧠 Smart support for nested models, lists, sets, tuples, enums, and optional fields
  • 🛠 Handles complex edge cases with minimal fallback to pl.Object
  • 🧪 100% test coverage with edge-case and structural schema tests
  • ⚙️ Minimal dependencies and easy integration into production pipelines

📦 Installation

pip install poldantic

Supports Python 3.8+ and Polars ≥ 0.19.


🚀 Usage

🔄 Pydantic → Polars

from poldantic import to_polars_schema
from pydantic import BaseModel
from typing import Optional, List

class Person(BaseModel):
    name: str
    tags: Optional[List[str]]

schema = to_polars_schema(Person)
print(schema)

Output:

{'name': Utf8, 'tags': List[Utf8]}

🔄 Polars → Pydantic

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 Models

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 Reference

to_polars_schema(model: Type[BaseModel]) -> dict[str, pl.DataType]

Converts a Pydantic model into a Polars-compatible schema dictionary. Supports nested models as pl.Struct(...).


to_pydantic_model(
    schema: dict[str, pl.DataType],
    model_name: str = "PolarsModel",
    force_optional: bool = True
) -> Type[BaseModel]

Converts a Polars schema dict into a Pydantic model. All fields are wrapped in Optional[...] by default to match Polars' nullability semantics.


📚 Supported Type Mappings

Pydantic Type Polars Type
int pl.Int64()
float pl.Float64()
str pl.String() or pl.Utf8()
bool pl.Boolean()
bytes pl.Binary()
datetime.date pl.Date()
datetime.datetime pl.Datetime()
datetime.time pl.Time()
datetime.timedelta pl.Duration()
Enum subclasses pl.String()
List[T], Set[T], Tuple[T, ...] pl.List(T)
Nested Pydantic model pl.Struct(...)
Union[int, str], Any pl.Object()

🧪 Running Tests

To run the test suite:

pytest

Tests cover a wide variety of primitives, nested models, optional fields, container types, and edge cases.


📄 License

MIT License © 2025 Odos Matthews


💡 Tip

Poldantic is an ideal companion for tools like Articuno and FastAPI — enabling full-circle schema validation and type-checking between APIs and DataFrames.

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

poldantic-0.2.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

poldantic-0.2.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file poldantic-0.2.1.tar.gz.

File metadata

  • Download URL: poldantic-0.2.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for poldantic-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1eda91a1b6808a3ab6dcb5c2b7010a55df60ccd3ef5af7f0c4e588fabb5bba17
MD5 4650d8f30e7da7908886bae7bcdc5cee
BLAKE2b-256 2fa7e54545a9b8135f82e77bad2c5e53ad41a24287f8ebb2252b7dd98c0f2f2e

See more details on using hashes here.

File details

Details for the file poldantic-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: poldantic-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for poldantic-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d046acee7d7831a52628959c75cf73c655b1cd8f5a761a4ff2eb7ebc5bccb435
MD5 19e45f97d127c20c1fffa7079d6bf5bd
BLAKE2b-256 e0861f4183a3be99eafd867a734e2a70c9effd0ae1fa6751553ae48fbf922b47

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page