Sparkdantic is a Python library that bridges Pydantic models with PySpark schemas. It allows developers to define data structures using Pydantic, then automatically generate equivalent `pyspark.sql.types.StructType` schemas for use in Spark DataFrames. This simplifies data validation and schema management across Python applications and Spark environments. The current version is 2.8.0, and it maintains an active release cadence, frequently updating for Pydantic and PySpark compatibility.
pip install sparkdantic pysparkVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a Pydantic model and use `create_spark_schema` to generate a corresponding PySpark `StructType`. It then shows how to initialize a SparkSession and create an empty DataFrame using the generated schema, printing both the raw schema and the DataFrame's schema for verification.
Upgrade sparkdantic to 2.0.0 or newer (e.g., `pip install sparkdantic>=2.0.0`). The current version 2.8.0 fully supports Pydantic V1 and V2.
Be aware of this automatic mapping. For stricter validation in Spark, consider UDFs or explicit casting after DataFrame creation based on the string values.
Store UUIDs as strings in Spark DataFrames. If direct UUID object handling is needed in Spark, it typically involves string parsing within UDFs.