[Pydantic](https://docs.pydantic.dev/latest/) is a library for data validation in [[Python]] and commonly used for structured outputs when working with [[large language model|LLM]]s.
Pydantic relies on [[type hints]] to declare the expected data structure.
It's written in [[Rust]] so it's super fast.
The build-in Python library [[dataclasses]] is similar with slightly less overhead but does not provide runtime validation, enforce type hints, and some other features. If you just want a lightweight data holder however, you can use `dataclasses` instead.
## installation
```bash
uv add pydantic
```
## use
Pydantic models import from `BaseModel`.
```python
from pydantic import BaseModel
from typing import ...
class Animal(BaseModel):
name: str
weight: float | None = None
```
[Read the docs](https://docs.pydantic.dev/latest/concepts/models/) for lots of other uses and features.