r/Python • • Mar 03 '26

Showcase scientific_pydantic: Pydantic adapters for common scientific data types

34 Upvotes

Code: https://github.com/psalvaggio/scientific_pydantic

Docs: https://psalvaggio.github.io/scientific_pydantic/latest/

What My Project Does

This project integrates a number of common scientific data types into pydantic. It uses the Annotated pattern for integrating third-party types with adapter objects. For example:

import typing as ty  
import astropy.units as u  
import pydantic  
from scientific_pydantic.astropy.units import QuantityAdapter

class Points(pydantic.BaseModel):  
points: ty.Annotated[u.Quantity, QuantityAdapter(u.m, shape=(None, 3), ge=0)]  

would define a model with a field that is an N x 3 array of points with non-negative XYZ in spatial units equivalent to meters.

No need for arbitrary_types_allowed=True and with the normal pydantic features of JSON serialization and conversions.

I currently have adapters for numpy (ndarray and dtype), scipy (Rotation), shapely (geometry types) and astropy (UnitBase, Quantity, PhysicalType and Time), along with some stuff from the standard library that pydantic doesn't ship with (slice, range, Ellipsis).

Target Audience

Users of both pydantic and common scientific libraries like: numpy, scipy, shapely and astropy.

Comparison

https://pypi.org/project/pydantic-numpy/

My project offers a few additional built-in features, such as more powerful shape specifiers, bounds checking and clipping. I don't support custom serialization, but this is just the first version of my project, that's on my list of future features.

https://pypi.org/project/pydantic-shapely/

This is pretty similar in scope. My project does WKT parsing in addition to GeoJSON and also offers coordinate bounds. Not a game-changer.

I don't know of anything else that offers scipy Rotation or astropy adapters.