Skip to main content

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 BaseModel classes to polars.Schema or pl.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 nullable status via FieldInfo

📦 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

poldantic-0.1.0.tar.gz (3.4 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.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

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

Hashes for poldantic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8805a7412b763d77c94e3047ab342558b1846d9b1de8ee078f01fac67be43fea
MD5 30df84a9f7a3663a183f8243a3dba3ba
BLAKE2b-256 136dfc08c2a739c346705462fcf8d333e638f7b8489274c9bb1b5464ffd1fc95

See more details on using hashes here.

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

Hashes for poldantic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ea13d3e9e681b767fa7afc84cb1c8ccc25760c9bbac7e074afef65ccc421e2b
MD5 78c65ac4819b71893e4e80b1fccc3d00
BLAKE2b-256 86d1dedf82893d11628dae179a5d24dc6f4186bead3aa52707b7b86554b52834

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