talira

Pydantic

Data validation using Python type hints

open sourceMITfree

Pydantic (Pydantic Validation) is the most widely used data validation library for Python: you describe how data should look with ordinary type annotations on a BaseModel, a dataclass or a TypedDict, and Pydantic parses, validates and serializes it, coercing values where it makes sense in lax mode or refusing them in strict mode. Its core validation logic is written in Rust (pydantic-core), which makes it one of the fastest validation libraries for Python, and models can emit JSON Schema for other tools. Custom validators and serializers change how data is processed, and thousands of libraries build on it, including FastAPI, SQLModel, LangChain and Pydantic AI.

Problems it solves

  • validate JSON from an API request against a schema in Python
  • turn untrusted dictionaries into typed Python objects with clear error messages
  • define my data schema once with type hints instead of writing validation code by hand
  • generate JSON Schema from my Python data models
  • serialize Python objects to JSON and back with type conversion
  • load and validate configuration and environment variables into typed settings
  • a faster alternative to marshmallow for validating data in Python
  • coerce strings like "1" into integers when parsing input, or refuse them in strict mode
  • validate the structured output an LLM returns against a Python model

Features

  • models defined with Python type hints (BaseModel)
  • validation core written in Rust
  • strict and lax modes
  • JSON Schema generation
  • validates dataclasses, TypedDicts and standard library types
  • custom validators and serializers
  • TypeAdapter for validating any type
  • settings management with pydantic-settings

Limitations

  • Pydantic needs Python 3.10 or newer.12

Alternatives

The same job

These do what Pydantic does.

  1. Zod TypeScript-first schema validation with static type inference

Compare Pydantic with the top 3