# Pydantic — card for AI agents

Data validation and parsing library using Python type hints.

Category: Forms, validation and schemas · PyPI package `pydantic` · latest version 2.14.2 (checked 2026-11-01)
Install: `pip install pydantic`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (paste into AGENTS.md or CLAUDE.md)

## Pydantic
- Write V2 code. Use `model_dump()`, `model_dump_json()`, `model_validate()`, `model_copy()` and `model_json_schema()`, not the V1 names `dict()`, `json()`, `parse_obj()` or `copy()`.
- For raw JSON input, call `Model.model_validate_json(data)` instead of the deprecated `parse_raw`/`parse_file`. Otherwise, load the data first and pass it to `model_validate`.
- To build models from ORM objects, set `from_attributes=True` in the model config and use `model_validate`, not `from_orm`.
- Lax mode coerces input by default. Pass `strict=True` to `model_validate` when inputs must match the declared types exactly.
- Catch `ValidationError` and read `e.errors()` for per-field `type`, `loc` and `msg`.
- Validation uses aliases by default (`validate_by_alias` is True, `validate_by_name` is False). Enable `validate_by_name` if callers pass field names.
- Use `@field_serializer` or `@model_serializer` for custom output. `json_encoders` in the model config is deprecated.

## Agent Skills for it
- pydantic-ai from davila7/claude-code-templates: `npx skills add davila7/claude-code-templates --skill pydantic-ai`
- pydantic from midudev/autoskills: `npx skills add midudev/autoskills --skill pydantic`
- pydantic-models-py from microsoft/skills: `npx skills add microsoft/skills --skill pydantic-models-py`

## Docs written for agents
- [llms.txt (index of the docs)](https://pydantic.dev/docs/validation/llms.txt)

## What it is
- The README opens with "Data validation using Python type hints." You describe the data you expect as a class that inherits from `BaseModel`, with annotated fields. You create an instance by passing external data to the class as keyword arguments, then read the fields as attributes. The repository was created in 2017 and is MIT licensed.
- The docs say schema validation and serialization "are controlled by type annotations". The same model can be dumped to a dict or a JSON string, and it can emit a JSON Schema. The core validation logic lives in a separate package, `pydantic-core`, where most types are validated in Rust.
- There are two modes. In lax mode, the default, Pydantic tries to coerce data to the declared type. In strict mode it does not convert anything. Version 2 is a ground-up rewrite of version 1 and breaks parts of the v1 API. The README targets Python 3.10+.

## Caveats
- V2 renamed the model methods: `dict()`, `json()`, `parse_obj()` and `copy()` became `model_dump()`, `model_dump_json()`, `model_validate()` and `model_copy()`. The old names still work where possible, but they emit `DeprecationWarning`s.
- The migration tool is beta: The maintainers provide `bump-pydantic` to rewrite v1 code. They write that it is "still in beta".
- Coercion is on by default: Pydantic converts common wrong types instead of rejecting them. For example, a numeric string passed to an `int` field is parsed as an int. If you need exact types, turn on strict mode.
- Equality is stricter in V2: Two models must match on type, field values and private attributes to be equal, plus extra values when `model_config['extra'] == 'allow'`. A model no longer compares equal to a dict.
- Constructor arguments are often copied: The migration guide says arguments passed to the constructor are copied in many cases, so they can be validated and coerced. It flags this as notable when the arguments are mutable objects.

## Numbers (AgentGid data)
- Gid Score: 86.0 (#31 of all tools, 3 of 63 in its category)
- Used in 538 public projects set up for AI coding agents (8.6%)
- Downloads last week: 292,309,217 (as of 2026-11-24), +19.0% over 4 weeks
- GitHub stars: 29,639 (as of 2026-11-24)
- Last push: 2026-11-23

## Often used with
Uvicorn 55.0%, FastAPI 54.8%, python-dotenv 52.0%, Requests 34.8%, Pydantic Settings 34.4%, PyYAML 34.0%

## Checked alternatives (same job)
- jsonschema: https://agentgid.com/tools/jsonschema/agent.md
- marshmallow: https://agentgid.com/tools/marshmallow/agent.md
- fastjsonschema: https://agentgid.com/tools/fastjsonschema/agent.md
- cattrs: https://agentgid.com/tools/cattrs/agent.md
- msgspec: https://agentgid.com/tools/msgspec/agent.md
- schema: https://agentgid.com/tools/schema/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/pydantic/
- Homepage: https://github.com/pydantic/pydantic
- Repository: https://github.com/pydantic/pydantic
- Source: https://github.com/pydantic/pydantic#readme
- Source: https://github.com/pydantic/pydantic
- Source: https://github.com/pydantic/pydantic/releases
- Source: https://pydantic.dev/docs/validation
- Source: https://pydantic.dev/docs/validation/latest/get-started/migration/index.md
- Source: https://pydantic.dev/docs/validation/latest/get-started/why/index.md
- Source: https://pydantic.dev/docs/validation/latest/concepts/alias/index.md


## Pydantic: check incoming data in Python apps

Pydantic is a free Python library that checks data before your app uses it. You describe what a sign-up form, a file or a reply from another service should look like. Pydantic accepts the data, fixes simple things like the text "34" into the number 34, or lists exactly what is wrong. Many Python tools, including FastAPI, are built on it.

### Where to use it
- Check sign-up forms before saving: A new user types a name, an email and an age; your app saves only data that fits and shows a clear message for each wrong field.
- Notice when another service changes: Your app reads orders or prices from someone else's service; a check on each reply tells you the day its format changes, instead of saving broken data.
- Read uploaded data files safely: People upload a file with contacts or settings; your app checks every entry and reports the missing or wrong ones by name.
- Describe your data to other tools: The same description can be turned into a standard format file, so documentation pages, form builders and AI models know which fields you expect.
- Load app settings from the server: Database address, upload limit and secret keys come from the server's settings; the app refuses to start if one is missing or wrong.
- Check an AI model's answer: You ask an AI model to fill in a support ticket; your app accepts the answer only if every field is there and in range. (idea, not from the official pages)

### How to start
- Install Pydantic into your project's Python environment (Python 3.10 or newer). Add the email extra if you check email addresses.

```
pip install pydantic
# with the `email` extra:
pip install 'pydantic[email]'
```

- Describe your data as a class with typed fields and pass the incoming data to it.

```
from datetime import datetime
from typing import Optional
from pydantic import BaseModel

class User(BaseModel):
    id: int
    name: str = 'John Doe'
    signup_ts: Optional[datetime] = None
    friends: list[int] = []

external_data = {'id': '123', 'signup_ts': '2017-06-01 12:22', 'friends': [1, '2', b'3']}
user = User(**external_data)
```

- Catch ValidationError to show the user what to fix; e.errors() gives one entry per wrong field.

### License
Free to use in any project, including paid and closed-source apps, under the MIT license. Keep its copyright and license notice if you ship its files.

### Good to know
- It needs Python 3.10 or newer.
- Version 2 was rewritten from scratch and renamed many functions. Old tutorials and old AI answers often show version 1 code.
- By default it quietly converts close matches, like the text "1" into the number 1. Turn on strict mode if you want such input refused.
- Email checks and loading settings from the server need extra packages, installed separately.
- The docs also promote Logfire, a separate paid service from the same company. Pydantic itself works without it.

Checked 2026-11-10 against: https://github.com/pydantic/pydantic/blob/main/README.md, https://github.com/pydantic/pydantic/blob/main/LICENSE, https://pypi.org/pypi/pydantic/json, https://pydantic.dev/docs/validation/latest/get-started/, https://pydantic.dev/docs/validation/latest/get-started/install/, https://pydantic.dev/docs/validation/latest/get-started/why/, https://pydantic.dev/docs/validation/latest/get-started/migration/, https://pydantic.dev/docs/validation/latest/get-started/version-policy/, https://pydantic.dev/docs/validation/latest/concepts/models/, https://pydantic.dev/docs/validation/latest/concepts/json_schema/, https://pydantic.dev/docs/validation/latest/concepts/pydantic_settings/, https://pydantic.dev/docs/validation/latest/examples/files/, https://pydantic.dev/docs/validation/latest/examples/requests/

### Mistakes to avoid
- Don't: Model.parse_obj(data), model.dict(), model.json(), Model.schema() (Pydantic 1 names) → Do: Model.model_validate(data), model.model_dump(), model.model_dump_json(), Model.model_json_schema() (Version 2 renamed these methods; the old ones still run but warn (our run printed PydanticDeprecatedSince20 for parse_obj and dict).)
- Don't: from pydantic import BaseSettings → Do: pip install pydantic-settings, then from pydantic_settings import BaseSettings (BaseSettings left the main package in version 2.)
- Don't: @validator('x') and class Config: orm_mode = True → Do: @field_validator('x') and model_config = ConfigDict(from_attributes=True) (The old decorator is deprecated and orm_mode was renamed.)
- Don't: Using EmailStr after plain pip install pydantic → Do: pip install 'pydantic[email]' (In our run the class definition failed with ImportError: email-validator is not installed.)
- Don't: note: Optional[str] (expecting the field to be optional) → Do: note: Optional[str] = None, or note: str | None = None (Without a default the field is still required; Optional only allows None as a value.)

### Questions
- Can I use it in a paid, closed-source app? Yes. It is MIT-licensed; keep the copyright and license notice with its files.
- My code uses parse_obj() and .dict(). Is that still right? Those are Pydantic 1 names. In version 2 they are model_validate() and model_dump(); the old names still run but warn that they are deprecated.
- Is there a tool to move old code to version 2? Yes, bump-pydantic. The docs say it is still in beta.
- Will a minor update break my code? The project says it will not break things on purpose in 2.x minor releases, but error message texts may change; check the error type, not the message.
- Does it send my data anywhere? No. Checks run inside your app; sending failed checks to Logfire is a separate, optional service.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
