pydantic-numpy
Pydantic Model integration of the NumPy array
What it is and what it does
pydantic-numpy bridges NumPy arrays and Pydantic's validation and serialization framework. It provides typed annotations (like NpNDArrayFp64 for 3D float64 arrays) that work with both BaseModel and dataclass, plus a NumpyModel class for seamless dump/load of arrays to disk. You can instantiate models from raw arrays, NumPy .npy files, or .npz files with keys, and serialize to JSON with full schema compliance.
The package handles the impedance mismatch between NumPy's array semantics and Pydantic's type system by wrapping arrays in validated fields. It supports custom serialization, dimension constraints, dtype specification, and model-agnostic loading when multiple models might match. Runtime dependencies are minimal and well-established (pydantic, numpy, ruamel-yaml, compress-pickle, semver).
Use it for:
- Validate and serialize machine-learning model inputs/outputs that include NumPy arrays as Pydantic models.
- Define strict dtype and dimension requirements for arrays in data pipelines (e.g., float32 1D arrays only).
- Persist Pydantic models containing arrays to disk and reload them with automatic type checking.
- Generate JSON schemas for APIs that accept or return NumPy array data.
- Combine NumPy arrays with other Pydantic fields in a single validated data structure.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Integrates NumPy arrays into Pydantic models with type validation, allowing you to define, validate, serialize, and deserialize NumPy arrays as model fields.
Yes. The package is actively maintained, has no known vulnerabilities, low install friction, and solves a real gap in Pydantic's NumPy support. It's suitable for production use if you need to validate and serialize NumPy arrays within Pydantic models. The permissive BSD-3-Clause license poses no restriction.
Install
pydantic-numpy on PyPI
pip
pip install pydantic-numpyuv
uv add pydantic-numpypoetry
poetry add pydantic-numpyInstalling pydantic-numpy
Before you install
Low friction install with five runtime dependencies (pydantic, numpy, ruamel-yaml, compress-pickle, semver). Active maintenance with a release 13 days ago; repo is not archived and has recent commits.
License in practice
BSD-3-Clause is permissive; you can use, modify, and distribute this package with minimal restrictions in commercial or open-source projects.
Quickstart
pip install pydantic-numpy
import numpy as np
from pydantic import BaseModel
import pydantic_numpy.typing as pnd
class MyModel(BaseModel):
array: pnd.Np1DArrayFp64
model = MyModel(array=np.array([1.5, 2.5, 3.5]))
json_str = model.model_dump_json()
restored = MyModel.model_validate_json(json_str)
Requires Python 3.11 or later (supports current versions, <3.15).
Verify before relying
- Whether custom serialization functions can handle all array types and edge cases you need.
- Performance characteristics when working with very large arrays or deeply nested structures.
- Compatibility with all Pydantic v2 features beyond the examples shown.
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | supports the current Python release (<3.15,>=3.11) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 5 — compress-pickle, ruamel-yaml, numpy, pydantic, semver |
| Maintenance | actively maintained — 13 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 178,398/month — #10,194 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pydantic_numpy-9.0.2-py3-none-any.whl
Keywords: pydantic, numpy, typing, validation
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
numpydanticNumpydantic adds type annotations and…
permissive · top 15,000 on PyPI
pydantic-collectionsProvides a `BaseCollectionModel` class that…
permissive · top 15,000 on PyPI
pydantic_yamlPydantic-YAML adds YAML serialization and…
permissive · top 5,000 on PyPI
pydantic-zarrProvides Pydantic models for reading,…
permissive · top 15,000 on PyPI
pydantic-to-typescript2Converts Pydantic models to TypeScript…
permissive · top 15,000 on PyPI
drf-pydanticBridges pydantic models and Django REST…
permissive · top 15,000 on PyPI
pydantic-scimProvides Pydantic data models for SCIM (System…
unclear · top 15,000 on PyPI
pydantic-to-typescriptConverts pydantic model definitions into…
permissive · top 5,000 on PyPI
data-science-typesProvides PEP-561-compliant type stubs for…
permissive · top 15,000 on PyPI
Flask-PydanticAdds Pydantic model validation to Flask route…
permissive · top 15,000 on PyPI