pydantic-spark
Converting pydantic classes to spark schemas
What it is and what it does
pydantic-spark is a lightweight bridge between Pydantic's Python type system and Apache Spark's schema representation. It lets you define data models as Pydantic classes and automatically generate the corresponding Spark schema dictionaries, or reverse the process by generating Python code from an existing Spark schema. The library extends Pydantic's BaseModel through a SparkBase class and adds a spark_schema() method that outputs a schema-compatible dictionary.
The package is designed for data engineers and Python developers working with Spark who want to avoid manually writing schema definitions or keeping Pydantic models and Spark schemas in sync. It includes a coerce_type option for field-level type conversion during schema generation. With only pydantic as a runtime dependency, installation is straightforward, though the project is currently dormant—last updated in late 2023—so new features or maintenance are unlikely.
Use it for:
- Define Spark DataFrame schemas using Pydantic classes, ensuring type safety and validation in Python before writing to Spark.
- Generate boilerplate Spark schema code from existing Pydantic models to reduce manual schema definition work.
- Reverse-engineer Python Pydantic classes from Spark schemas to keep data contracts synchronized across systems.
- Apply field-level type coercion rules during schema generation when Pydantic types need to map to different Spark types.
- Validate and document data pipelines by using Pydantic's validation alongside Spark's schema enforcement.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Converts Pydantic class definitions to Apache Spark schemas and generates Python code from Spark schemas, bridging type validation and distributed data processing.
Yes, if you actively use both Pydantic and Spark and want to avoid duplicating schema definitions. The low install friction and permissive license make it a low-risk addition. However, be aware that the project is dormant—no updates since late 2023—so you should verify compatibility with your current Pydantic and Spark versions before relying on it in production, and plan to maintain a fork if critical bugs emerge.
Install
pydantic-spark on PyPI
pip
pip install pydantic-sparkuv
uv add pydantic-sparkpoetry
poetry add pydantic-sparkInstalling pydantic-spark
Before you install
Low install friction with a single runtime dependency on pydantic. Maintenance is dormant—last commit was 2024-03-04 and no releases since 2023-11-24—so expect no active bug fixes or feature development, though the codebase remains archived and available.
License in practice
MIT license is permissive, allowing commercial and private use with minimal restrictions; you may use this package freely in most contexts.
Quickstart
from pydantic_spark.base import SparkBase
class TestModel(SparkBase):
key1: str
key2: int
schema_dict = TestModel.spark_schema()
Requires pydantic as a runtime dependency; Spark itself is not listed as a dependency, so you must have it available in your environment separately.
Verify before relying
- Whether Spark is an implicit peer dependency or truly optional at runtime.
- Compatibility with recent Pydantic v2 major version changes and their breaking API shifts.
- Whether the coerce_type feature and other advanced options are production-ready or experimental.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8,<4.0) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — pydantic |
| Maintenance | dormant — 994 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 199,617/month — #9,702 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pydantic_spark-1.0.1-py3-none-any.whl
Keywords: pydantic, spark
Tags
More Database packages
psycopg2-binary is a PostgreSQL database…
copyleft · top 1,000 on PyPI
redisPython client library for connecting to and…
permissive · top 1,000 on PyPI
ydbYDB Python SDK is the official client library…
permissive · top 1,000 on PyPI
snowflake-connector-pythonConnects Python applications to Snowflake data…
permissive · top 1,000 on PyPI
sqlparsesqlparse tokenizes SQL text into a tree of…
permissive · top 1,000 on PyPI
dbt-adaptersProvides base adapter protocols and shared…
permissive · top 1,000 on PyPI
pydantic-avroConverts between pydantic class definitions and…
permissive · top 5,000 on PyPI
sparkdanticConverts Pydantic models to PySpark schemas…
unclear · top 5,000 on PyPI
jsonschema-pydanticConverts JSON Schema definitions into Pydantic…
unclear · top 15,000 on PyPI
dydanticDynamically generates Pydantic models from JSON…
permissive · top 5,000 on PyPI
jsonschema-pydantic-converterConverts JSON Schema definitions to Pydantic v2…
permissive · top 15,000 on PyPI
py-avro-schemaGenerates Apache Avro schemas from Python…
permissive · top 15,000 on PyPI
dataclasses-avroschemaGenerates Avro schemas from Python dataclasses,…
permissive · top 5,000 on PyPI
django-pydantic-fieldProvides type-safe Pydantic model schemas for…
permissive · top 5,000 on PyPI
schemaValidates Python data structures (dicts, lists,…
permissive · top 1,000 on PyPI
pydantic-to-pyarrowConverts pydantic model definitions to pyarrow…
permissive · top 15,000 on PyPI