skillfed

pydantic-spark

Converting pydantic classes to spark schemas

pydantic-spark v1.0.1 199.6K downloads/30d#9,702 on PyPI26
Permissive license MIT DORMANT released

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-spark

uv

uv add pydantic-spark

poetry

poetry add pydantic-spark

Installing 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

License :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

pydantic to spark schemaconvert pydantic class sparkspark schema from python typespydantic spark integrationschema conversion pydanticspark dataframe schema generation
schema-generationspark-integrationdata-validation

More Database packages

psycopg2-binary

psycopg2-binary is a PostgreSQL database…

copyleft · top 1,000 on PyPI

redis

Python client library for connecting to and…

permissive · top 1,000 on PyPI

ydb

YDB Python SDK is the official client library…

permissive · top 1,000 on PyPI

snowflake-connector-python

Connects Python applications to Snowflake data…

permissive · top 1,000 on PyPI

sqlparse

sqlparse tokenizes SQL text into a tree of…

permissive · top 1,000 on PyPI

dbt-adapters

Provides base adapter protocols and shared…

permissive · top 1,000 on PyPI

pydantic-avro

Converts between pydantic class definitions and…

permissive · top 5,000 on PyPI

sparkdantic

Converts Pydantic models to PySpark schemas…

unclear · top 5,000 on PyPI

jsonschema-pydantic

Converts JSON Schema definitions into Pydantic…

unclear · top 15,000 on PyPI

dydantic

Dynamically generates Pydantic models from JSON…

permissive · top 5,000 on PyPI

jsonschema-pydantic-converter

Converts JSON Schema definitions to Pydantic v2…

permissive · top 15,000 on PyPI

py-avro-schema

Generates Apache Avro schemas from Python…

permissive · top 15,000 on PyPI

dataclasses-avroschema

Generates Avro schemas from Python dataclasses,…

permissive · top 5,000 on PyPI

django-pydantic-field

Provides type-safe Pydantic model schemas for…

permissive · top 5,000 on PyPI

schema

Validates Python data structures (dicts, lists,…

permissive · top 1,000 on PyPI

pydantic-to-pyarrow

Converts pydantic model definitions to pyarrow…

permissive · top 15,000 on PyPI