ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://docs.pydantic.dev/latest/ |
| Last Crawled | 2026-04-09 08:38:00 (37 minutes ago) |
| First Indexed | 2023-04-29 15:54:42 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Welcome to Pydantic - Pydantic Validation |
| Meta Description | Data validation using Python type hints |
| Meta Canonical | null |
| Boilerpipe Text | Documentation for version:
v2.12.5
.
Pydantic is the most widely used data validation library for Python.
Fast and extensible, Pydantic plays nicely with your linters/IDE/brain. Define how data should be in pure, canonical Python 3.9+; validate it with Pydantic.
Monitor Pydantic with Pydantic Logfire
Pydantic Logfire
is an application monitoring tool that is as simple to use and powerful as Pydantic itself.
Logfire integrates with many popular Python libraries including FastAPI, OpenAI and Pydantic itself, so you can use Logfire to monitor Pydantic validations and understand why some inputs fail validation:
Monitoring Pydantic with Logfire
from
datetime
import
datetime
import
logfire
from
pydantic
import
BaseModel
logfire
.
configure
()
logfire
.
instrument_pydantic
()
# (1)!
class
Delivery
(
BaseModel
):
timestamp
:
datetime
dimensions
:
tuple
[
int
,
int
]
# this will record details of a successful validation to logfire
m
=
Delivery
(
timestamp
=
'2020-01-02T03:04:05Z'
,
dimensions
=
[
'10'
,
'20'
])
print
(
repr
(
m
.
timestamp
))
#> datetime.datetime(2020, 1, 2, 3, 4, 5, tzinfo=TzInfo(UTC))
print
(
m
.
dimensions
)
#> (10, 20)
Delivery
(
timestamp
=
'2020-01-02T03:04:05Z'
,
dimensions
=
[
'10'
])
# (2)!
Set logfire record all both successful and failed validations, use
record='failure'
to only record failed validations,
learn more
.
This will raise a
ValidationError
since there are too few
dimensions
, details of the input data and validation errors will be recorded in Logfire.
Would give you a view like this in the Logfire platform:
This is just a toy example, but hopefully makes clear the potential value of instrumenting a more complex application.
Learn more about Pydantic Logfire
Sign up for our newsletter,
The Pydantic Stack
, with updates & tutorials on Pydantic, Logfire, and Pydantic AI:
Why use Pydantic?
¶
Powered by type hints
— with Pydantic, schema validation and serialization are controlled by type annotations; less to learn, less code to write, and integration with your IDE and static analysis tools.
Learn more…
Speed
— Pydantic's core validation logic is written in Rust. As a result, Pydantic is among the fastest data validation libraries for Python.
Learn more…
JSON Schema
— Pydantic models can emit JSON Schema, allowing for easy integration with other tools.
Learn more…
Strict
and
Lax
mode — Pydantic can run in either strict mode (where data is not converted) or lax mode where Pydantic tries to coerce data to the correct type where appropriate.
Learn more…
Dataclasses
,
TypedDicts
and more — Pydantic supports validation of many standard library types including
dataclass
and
TypedDict
.
Learn more…
Customisation
— Pydantic allows custom validators and serializers to alter how data is processed in many powerful ways.
Learn more…
Ecosystem
— around 8,000 packages on PyPI use Pydantic, including massively popular libraries like
FastAPI
,
huggingface
,
Django Ninja
,
SQLModel
, &
LangChain
.
Learn more…
Battle tested
— Pydantic is downloaded over 360M times/month and is used by all FAANG companies and 20 of the 25 largest companies on NASDAQ. If you're trying to do something with Pydantic, someone else has probably already done it.
Learn more…
Installing Pydantic
is as simple as:
pip install pydantic
Pydantic examples
¶
To see Pydantic at work, let's start with a simple example, creating a custom class that inherits from
BaseModel
:
Validation Successful
from
datetime
import
datetime
from
pydantic
import
BaseModel
,
PositiveInt
class
User
(
BaseModel
):
id
:
int
# (1)!
name
:
str
=
'John Doe'
# (2)!
signup_ts
:
datetime
|
None
# (3)!
tastes
:
dict
[
str
,
PositiveInt
]
# (4)!
external_data
=
{
'id'
:
123
,
'signup_ts'
:
'2019-06-01 12:22'
,
# (5)!
'tastes'
:
{
'wine'
:
9
,
b
'cheese'
:
7
,
# (6)!
'cabbage'
:
'1'
,
# (7)!
},
}
user
=
User
(
**
external_data
)
# (8)!
print
(
user
.
id
)
# (9)!
#> 123
print
(
user
.
model_dump
())
# (10)!
"""
{
'id': 123,
'name': 'John Doe',
'signup_ts': datetime.datetime(2019, 6, 1, 12, 22),
'tastes': {'wine': 9, 'cheese': 7, 'cabbage': 1},
}
"""
id
is of type
int
; the annotation-only declaration tells Pydantic that this field is required. Strings,
bytes, or floats will be coerced to integers if possible; otherwise an exception will be raised.
name
is a string; because it has a default, it is not required.
signup_ts
is a
datetime
field that is required, but the value
None
may be provided;
Pydantic will process either a
Unix timestamp
integer (e.g.
1496498400
)
or a string representing the date and time.
tastes
is a dictionary with string keys and positive integer values. The
PositiveInt
type is
shorthand for
Annotated[int, annotated_types.Gt(0)]
.
The input here is an
ISO 8601
formatted datetime, but Pydantic will
convert it to a
datetime
object.
The key here is
bytes
, but Pydantic will take care of coercing it to a string.
Similarly, Pydantic will coerce the string
'1'
to the integer
1
.
We create instance of
User
by passing our external data to
User
as keyword arguments.
We can access fields as attributes of the model.
We can convert the model to a dictionary with
model_dump()
.
If validation fails, Pydantic will raise an error with a breakdown of what was wrong:
Validation Error
# continuing the above example...
from
datetime
import
datetime
from
pydantic
import
BaseModel
,
PositiveInt
,
ValidationError
class
User
(
BaseModel
):
id
:
int
name
:
str
=
'John Doe'
signup_ts
:
datetime
|
None
tastes
:
dict
[
str
,
PositiveInt
]
external_data
=
{
'id'
:
'not an int'
,
'tastes'
:
{}}
# (1)!
try
:
User
(
**
external_data
)
# (2)!
except
ValidationError
as
e
:
print
(
e
.
errors
())
"""
[
{
'type': 'int_parsing',
'loc': ('id',),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'input': 'not an int',
'url': 'https://errors.pydantic.dev/2/v/int_parsing',
},
{
'type': 'missing',
'loc': ('signup_ts',),
'msg': 'Field required',
'input': {'id': 'not an int', 'tastes': {}},
'url': 'https://errors.pydantic.dev/2/v/missing',
},
]
"""
The input data is wrong here —
id
is not a valid integer, and
signup_ts
is missing.
Trying to instantiate
User
will raise a
ValidationError
with a list of errors.
Who is using Pydantic?
¶
Hundreds of organisations and packages are using Pydantic. Some of the prominent companies and organizations around the world who are using Pydantic include:
For a more comprehensive list of open-source projects using Pydantic see the
list of dependents on github
, or you can find some awesome projects using Pydantic in
awesome-pydantic
. |
| Markdown | [Skip to content](https://docs.pydantic.dev/latest/#pydantic-validation)
What's new — we've launched [Pydantic Logfire](https://pydantic.dev/articles/logfire-announcement)  to help you monitor and understand your [Pydantic validations.](https://logfire.pydantic.dev/docs/integrations/pydantic/)
[](https://docs.pydantic.dev/latest/ "Pydantic Validation")
Pydantic Validation
Welcome to Pydantic
Type to start searching
[pydantic/pydantic](https://github.com/pydantic/pydantic "Go to repository")
- [Get Started](https://docs.pydantic.dev/latest/)
- [Concepts](https://docs.pydantic.dev/latest/concepts/models/)
- [API Documentation](https://docs.pydantic.dev/latest/api/base_model/)
- [Internals](https://docs.pydantic.dev/latest/internals/architecture/)
- [Examples](https://docs.pydantic.dev/latest/examples/files/)
- [Error Messages](https://docs.pydantic.dev/latest/errors/errors/)
- [Integrations](https://docs.pydantic.dev/latest/integrations/logfire/)
- [Blog](https://blog.pydantic.dev/)
- [Pydantic People](https://docs.pydantic.dev/latest/pydantic_people/)
[](https://docs.pydantic.dev/latest/ "Pydantic Validation") Pydantic Validation
[pydantic/pydantic](https://github.com/pydantic/pydantic "Go to repository")
- Get Started
Get Started
- Welcome to Pydantic
[Welcome to Pydantic](https://docs.pydantic.dev/latest/)
Page contents
- [Why use Pydantic?](https://docs.pydantic.dev/latest/#why-use-pydantic)
- [Pydantic examples](https://docs.pydantic.dev/latest/#pydantic-examples)
- [Who is using Pydantic?](https://docs.pydantic.dev/latest/#who-is-using-pydantic)
- [Why use Pydantic](https://docs.pydantic.dev/latest/why/)
- [Help with Pydantic](https://docs.pydantic.dev/latest/help_with_pydantic/)
- [Installation](https://docs.pydantic.dev/latest/install/)
- [Migration Guide](https://docs.pydantic.dev/latest/migration/)
- [Version Policy](https://docs.pydantic.dev/latest/version-policy/)
- [Contributing](https://docs.pydantic.dev/latest/contributing/)
- [Changelog](https://docs.pydantic.dev/latest/changelog/)
- Concepts
Concepts
- [Models](https://docs.pydantic.dev/latest/concepts/models/)
- [Fields](https://docs.pydantic.dev/latest/concepts/fields/)
- [JSON Schema](https://docs.pydantic.dev/latest/concepts/json_schema/)
- [JSON](https://docs.pydantic.dev/latest/concepts/json/)
- [Types](https://docs.pydantic.dev/latest/concepts/types/)
- [Unions](https://docs.pydantic.dev/latest/concepts/unions/)
- [Alias](https://docs.pydantic.dev/latest/concepts/alias/)
- [Configuration](https://docs.pydantic.dev/latest/concepts/config/)
- [Serialization](https://docs.pydantic.dev/latest/concepts/serialization/)
- [Validators](https://docs.pydantic.dev/latest/concepts/validators/)
- [Dataclasses](https://docs.pydantic.dev/latest/concepts/dataclasses/)
- [Forward Annotations](https://docs.pydantic.dev/latest/concepts/forward_annotations/)
- [Strict Mode](https://docs.pydantic.dev/latest/concepts/strict_mode/)
- [Type Adapter](https://docs.pydantic.dev/latest/concepts/type_adapter/)
- [Validation Decorator](https://docs.pydantic.dev/latest/concepts/validation_decorator/)
- [Conversion Table](https://docs.pydantic.dev/latest/concepts/conversion_table/)
- [Settings Management](https://docs.pydantic.dev/latest/concepts/pydantic_settings/)
- [Performance](https://docs.pydantic.dev/latest/concepts/performance/)
- [Experimental](https://docs.pydantic.dev/latest/concepts/experimental/)
- API Documentation
API Documentation
- Pydantic
Pydantic
- [BaseModel](https://docs.pydantic.dev/latest/api/base_model/)
- [RootModel](https://docs.pydantic.dev/latest/api/root_model/)
- [Pydantic Dataclasses](https://docs.pydantic.dev/latest/api/dataclasses/)
- [TypeAdapter](https://docs.pydantic.dev/latest/api/type_adapter/)
- [Validate Call](https://docs.pydantic.dev/latest/api/validate_call/)
- [Fields](https://docs.pydantic.dev/latest/api/fields/)
- [Aliases](https://docs.pydantic.dev/latest/api/aliases/)
- [Configuration](https://docs.pydantic.dev/latest/api/config/)
- [JSON Schema](https://docs.pydantic.dev/latest/api/json_schema/)
- [Errors](https://docs.pydantic.dev/latest/api/errors/)
- [Functional Validators](https://docs.pydantic.dev/latest/api/functional_validators/)
- [Functional Serializers](https://docs.pydantic.dev/latest/api/functional_serializers/)
- [Standard Library Types](https://docs.pydantic.dev/latest/api/standard_library_types/)
- [Pydantic Types](https://docs.pydantic.dev/latest/api/types/)
- [Network Types](https://docs.pydantic.dev/latest/api/networks/)
- [Version Information](https://docs.pydantic.dev/latest/api/version/)
- [Annotated Handlers](https://docs.pydantic.dev/latest/api/annotated_handlers/)
- [Experimental](https://docs.pydantic.dev/latest/api/experimental/)
- Pydantic Core
Pydantic Core
- [pydantic\_core](https://docs.pydantic.dev/latest/api/pydantic_core/)
- [pydantic\_core.core\_schema](https://docs.pydantic.dev/latest/api/pydantic_core_schema/)
- [Pydantic Settings](https://docs.pydantic.dev/latest/api/pydantic_settings/)
- Pydantic Extra Types
Pydantic Extra Types
- [Color](https://docs.pydantic.dev/latest/api/pydantic_extra_types_color/)
- [Country](https://docs.pydantic.dev/latest/api/pydantic_extra_types_country/)
- [Payment](https://docs.pydantic.dev/latest/api/pydantic_extra_types_payment/)
- [Phone Numbers](https://docs.pydantic.dev/latest/api/pydantic_extra_types_phone_numbers/)
- [Routing Numbers](https://docs.pydantic.dev/latest/api/pydantic_extra_types_routing_numbers/)
- [Coordinate](https://docs.pydantic.dev/latest/api/pydantic_extra_types_coordinate/)
- [Mac Address](https://docs.pydantic.dev/latest/api/pydantic_extra_types_mac_address/)
- [ISBN](https://docs.pydantic.dev/latest/api/pydantic_extra_types_isbn/)
- [Pendulum](https://docs.pydantic.dev/latest/api/pydantic_extra_types_pendulum_dt/)
- [Currency](https://docs.pydantic.dev/latest/api/pydantic_extra_types_currency_code/)
- [Language](https://docs.pydantic.dev/latest/api/pydantic_extra_types_language_code/)
- [Script Code](https://docs.pydantic.dev/latest/api/pydantic_extra_types_script_code/)
- [Semantic Version](https://docs.pydantic.dev/latest/api/pydantic_extra_types_semantic_version/)
- [Timezone Name](https://docs.pydantic.dev/latest/api/pydantic_extra_types_timezone_name/)
- [ULID](https://docs.pydantic.dev/latest/api/pydantic_extra_types_ulid/)
- Internals
Internals
- [Architecture](https://docs.pydantic.dev/latest/internals/architecture/)
- [Resolving Annotations](https://docs.pydantic.dev/latest/internals/resolving_annotations/)
- Examples
Examples
- [Validating File Data](https://docs.pydantic.dev/latest/examples/files/)
- [Web and API Requests](https://docs.pydantic.dev/latest/examples/requests/)
- [Queues](https://docs.pydantic.dev/latest/examples/queues/)
- [Databases](https://docs.pydantic.dev/latest/examples/orms/)
- [Custom Validators](https://docs.pydantic.dev/latest/examples/custom_validators/)
- [Dynamic models](https://docs.pydantic.dev/latest/examples/dynamic_models/)
- Error Messages
Error Messages
- [Error Handling](https://docs.pydantic.dev/latest/errors/errors/)
- [Validation Errors](https://docs.pydantic.dev/latest/errors/validation_errors/)
- [Usage Errors](https://docs.pydantic.dev/latest/errors/usage_errors/)
- Integrations
Integrations
- [Pydantic Logfire](https://docs.pydantic.dev/latest/integrations/logfire/)
- [LLMs](https://docs.pydantic.dev/latest/integrations/llms/)
- Dev Tools
Dev Tools
- [Mypy](https://docs.pydantic.dev/latest/integrations/mypy/)
- [Pyrefly](https://docs.pydantic.dev/latest/integrations/pyrefly/)
- [PyCharm](https://docs.pydantic.dev/latest/integrations/pycharm/)
- [Hypothesis](https://docs.pydantic.dev/latest/integrations/hypothesis/)
- [Visual Studio Code](https://docs.pydantic.dev/latest/integrations/visual_studio_code/)
- [datamodel-code-generator](https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/)
- [devtools](https://docs.pydantic.dev/latest/integrations/devtools/)
- [Rich](https://docs.pydantic.dev/latest/integrations/rich/)
- [Linting](https://docs.pydantic.dev/latest/integrations/linting/)
- [Documentation](https://docs.pydantic.dev/latest/integrations/documentation/)
- Production Tools
Production Tools
- [AWS Lambda](https://docs.pydantic.dev/latest/integrations/aws_lambda/)
- [Blog](https://blog.pydantic.dev/)
- [Pydantic People](https://docs.pydantic.dev/latest/pydantic_people/)
Page contents
- [Why use Pydantic?](https://docs.pydantic.dev/latest/#why-use-pydantic)
- [Pydantic examples](https://docs.pydantic.dev/latest/#pydantic-examples)
- [Who is using Pydantic?](https://docs.pydantic.dev/latest/#who-is-using-pydantic)
# Pydantic Validation[¶](https://docs.pydantic.dev/latest/#pydantic-validation "Permanent link")
[](https://github.com/pydantic/pydantic/actions?query=event%3Apush+branch%3Amain+workflow%3ACI) [](https://github.com/pydantic/pydantic/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)
[](https://pypi.python.org/pypi/pydantic) [](https://anaconda.org/conda-forge/pydantic) [](https://pepy.tech/project/pydantic)
[](https://github.com/pydantic/pydantic/blob/main/LICENSE) [](https://docs.pydantic.dev/latest/llms.txt)
Documentation for version: [v2.12.5](https://github.com/pydantic/pydantic/releases/tag/v2.12.5).
Pydantic is the most widely used data validation library for Python.
Fast and extensible, Pydantic plays nicely with your linters/IDE/brain. Define how data should be in pure, canonical Python 3.9+; validate it with Pydantic.
Monitor Pydantic with Pydantic Logfire 
**[Pydantic Logfire](https://pydantic.dev/logfire)** is an application monitoring tool that is as simple to use and powerful as Pydantic itself.
Logfire integrates with many popular Python libraries including FastAPI, OpenAI and Pydantic itself, so you can use Logfire to monitor Pydantic validations and understand why some inputs fail validation:
Monitoring Pydantic with Logfire
```
```
1. Set logfire record all both successful and failed validations, use `record='failure'` to only record failed validations, [learn more](https://logfire.pydantic.dev/docs/integrations/pydantic/).
2. This will raise a `ValidationError` since there are too few `dimensions`, details of the input data and validation errors will be recorded in Logfire.
Would give you a view like this in the Logfire platform:
[](https://logfire.pydantic.dev/docs/guides/web-ui/live/)
This is just a toy example, but hopefully makes clear the potential value of instrumenting a more complex application.
**[Learn more about Pydantic Logfire](https://logfire.pydantic.dev/docs/)**
**Sign up for our newsletter, *The Pydantic Stack*, with updates & tutorials on Pydantic, Logfire, and Pydantic AI:**
## Why use Pydantic?[¶](https://docs.pydantic.dev/latest/#why-use-pydantic "Permanent link")
- **Powered by type hints** — with Pydantic, schema validation and serialization are controlled by type annotations; less to learn, less code to write, and integration with your IDE and static analysis tools. [Learn more…](https://docs.pydantic.dev/latest/why/#type-hints)
- **Speed** — Pydantic's core validation logic is written in Rust. As a result, Pydantic is among the fastest data validation libraries for Python. [Learn more…](https://docs.pydantic.dev/latest/why/#performance)
- **JSON Schema** — Pydantic models can emit JSON Schema, allowing for easy integration with other tools. [Learn more…](https://docs.pydantic.dev/latest/why/#json-schema)
- **Strict** and **Lax** mode — Pydantic can run in either strict mode (where data is not converted) or lax mode where Pydantic tries to coerce data to the correct type where appropriate. [Learn more…](https://docs.pydantic.dev/latest/why/#strict-lax)
- **Dataclasses**, **TypedDicts** and more — Pydantic supports validation of many standard library types including `dataclass` and `TypedDict`. [Learn more…](https://docs.pydantic.dev/latest/why/#dataclasses-typeddict-more)
- **Customisation** — Pydantic allows custom validators and serializers to alter how data is processed in many powerful ways. [Learn more…](https://docs.pydantic.dev/latest/why/#customisation)
- **Ecosystem** — around 8,000 packages on PyPI use Pydantic, including massively popular libraries like *FastAPI*, *huggingface*, *Django Ninja*, *SQLModel*, & *LangChain*. [Learn more…](https://docs.pydantic.dev/latest/why/#ecosystem)
- **Battle tested** — Pydantic is downloaded over 360M times/month and is used by all FAANG companies and 20 of the 25 largest companies on NASDAQ. If you're trying to do something with Pydantic, someone else has probably already done it. [Learn more…](https://docs.pydantic.dev/latest/why/#using-pydantic)
[Installing Pydantic](https://docs.pydantic.dev/latest/install/) is as simple as: `pip install pydantic`
## Pydantic examples[¶](https://docs.pydantic.dev/latest/#pydantic-examples "Permanent link")
To see Pydantic at work, let's start with a simple example, creating a custom class that inherits from `BaseModel`:
Validation Successful
```
```
1. `id` is of type `int`; the annotation-only declaration tells Pydantic that this field is required. Strings, bytes, or floats will be coerced to integers if possible; otherwise an exception will be raised.
2. `name` is a string; because it has a default, it is not required.
3. `signup_ts` is a [`datetime`](https://docs.python.org/3/library/datetime.html#datetime.datetime) field that is required, but the value `None` may be provided; Pydantic will process either a [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time) integer (e.g. `1496498400`) or a string representing the date and time.
4. `tastes` is a dictionary with string keys and positive integer values. The `PositiveInt` type is shorthand for `Annotated[int, annotated_types.Gt(0)]`.
5. The input here is an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formatted datetime, but Pydantic will convert it to a [`datetime`](https://docs.python.org/3/library/datetime.html#datetime.datetime) object.
6. The key here is `bytes`, but Pydantic will take care of coercing it to a string.
7. Similarly, Pydantic will coerce the string `'1'` to the integer `1`.
8. We create instance of `User` by passing our external data to `User` as keyword arguments.
9. We can access fields as attributes of the model.
10. We can convert the model to a dictionary with [`model_dump()`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_dump).
If validation fails, Pydantic will raise an error with a breakdown of what was wrong:
Validation Error
```
```
1. The input data is wrong here — `id` is not a valid integer, and `signup_ts` is missing.
2. Trying to instantiate `User` will raise a [`ValidationError`](https://docs.pydantic.dev/latest/api/pydantic_core/#pydantic_core.ValidationError) with a list of errors.
## Who is using Pydantic?[¶](https://docs.pydantic.dev/latest/#who-is-using-pydantic "Permanent link")
Hundreds of organisations and packages are using Pydantic. Some of the prominent companies and organizations around the world who are using Pydantic include:
[](https://docs.pydantic.dev/latest/why/#org-adobe "Adobe")
[](https://docs.pydantic.dev/latest/why/#org-amazon "Amazon and AWS")
[](https://docs.pydantic.dev/latest/why/#org-anthropic "Anthropic")
[](https://docs.pydantic.dev/latest/why/#org-apple "Apple")
[](https://docs.pydantic.dev/latest/why/#org-asml "ASML")
[](https://docs.pydantic.dev/latest/why/#org-astrazeneca "AstraZeneca")
[](https://docs.pydantic.dev/latest/why/#org-cisco "Cisco Systems")
[](https://docs.pydantic.dev/latest/why/#org-comcast "Comcast")
[](https://docs.pydantic.dev/latest/why/#org-datadog "Datadog")
[](https://docs.pydantic.dev/latest/why/#org-facebook "Facebook")
[](https://docs.pydantic.dev/latest/why/#org-github "GitHub")
[](https://docs.pydantic.dev/latest/why/#org-google "Google")
[](https://docs.pydantic.dev/latest/why/#org-hsbc "HSBC")
[](https://docs.pydantic.dev/latest/why/#org-ibm "IBM")
[](https://docs.pydantic.dev/latest/why/#org-intel "Intel")
[](https://docs.pydantic.dev/latest/why/#org-intuit "Intuit")
[](https://docs.pydantic.dev/latest/why/#org-ipcc "Intergovernmental Panel on Climate Change")
[](https://docs.pydantic.dev/latest/why/#org-jpmorgan "JPMorgan")
[](https://docs.pydantic.dev/latest/why/#org-jupyter "Jupyter")
[](https://docs.pydantic.dev/latest/why/#org-microsoft "Microsoft")
[](https://docs.pydantic.dev/latest/why/#org-molssi "Molecular Science Software Institute")
[](https://docs.pydantic.dev/latest/why/#org-nasa "NASA")
[](https://docs.pydantic.dev/latest/why/#org-netflix "Netflix")
[](https://docs.pydantic.dev/latest/why/#org-nsa "NSA")
[](https://docs.pydantic.dev/latest/why/#org-nvidia "NVIDIA")
[](https://docs.pydantic.dev/latest/why/#org-openai "OpenAI")
[](https://docs.pydantic.dev/latest/why/#org-oracle "Oracle")
[](https://docs.pydantic.dev/latest/why/#org-palantir "Palantir")
[](https://docs.pydantic.dev/latest/why/#org-qualcomm "Qualcomm")
[](https://docs.pydantic.dev/latest/why/#org-redhat "Red Hat")
[](https://docs.pydantic.dev/latest/why/#org-revolut "Revolut")
[](https://docs.pydantic.dev/latest/why/#org-robusta "Robusta")
[](https://docs.pydantic.dev/latest/why/#org-salesforce "Salesforce")
[](https://docs.pydantic.dev/latest/why/#org-starbucks "Starbucks")
[](https://docs.pydantic.dev/latest/why/#org-ti "Texas Instruments")
[](https://docs.pydantic.dev/latest/why/#org-twilio "Twilio")
[](https://docs.pydantic.dev/latest/why/#org-twitter "Twitter")
[](https://docs.pydantic.dev/latest/why/#org-ukhomeoffice "UK Home Office")
For a more comprehensive list of open-source projects using Pydantic see the [list of dependents on github](https://github.com/pydantic/pydantic/network/dependents), or you can find some awesome projects using Pydantic in [awesome-pydantic](https://github.com/Kludex/awesome-pydantic).
Back to top
Made with [Material for MkDocs](https://squidfunk.github.io/mkdocs-material/) |
| Readable Markdown | [](https://github.com/pydantic/pydantic/actions?query=event%3Apush+branch%3Amain+workflow%3ACI) [](https://github.com/pydantic/pydantic/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)
[](https://pypi.python.org/pypi/pydantic) [](https://anaconda.org/conda-forge/pydantic) [](https://pepy.tech/project/pydantic)
[](https://github.com/pydantic/pydantic/blob/main/LICENSE) [](https://docs.pydantic.dev/latest/llms.txt)
Documentation for version: [v2.12.5](https://github.com/pydantic/pydantic/releases/tag/v2.12.5).
Pydantic is the most widely used data validation library for Python.
Fast and extensible, Pydantic plays nicely with your linters/IDE/brain. Define how data should be in pure, canonical Python 3.9+; validate it with Pydantic.
Monitor Pydantic with Pydantic Logfire 
**[Pydantic Logfire](https://pydantic.dev/logfire)** is an application monitoring tool that is as simple to use and powerful as Pydantic itself.
Logfire integrates with many popular Python libraries including FastAPI, OpenAI and Pydantic itself, so you can use Logfire to monitor Pydantic validations and understand why some inputs fail validation:
Monitoring Pydantic with Logfire
```
```
1. Set logfire record all both successful and failed validations, use `record='failure'` to only record failed validations, [learn more](https://logfire.pydantic.dev/docs/integrations/pydantic/).
2. This will raise a `ValidationError` since there are too few `dimensions`, details of the input data and validation errors will be recorded in Logfire.
Would give you a view like this in the Logfire platform:
[](https://logfire.pydantic.dev/docs/guides/web-ui/live/)
This is just a toy example, but hopefully makes clear the potential value of instrumenting a more complex application.
**[Learn more about Pydantic Logfire](https://logfire.pydantic.dev/docs/)**
**Sign up for our newsletter, *The Pydantic Stack*, with updates & tutorials on Pydantic, Logfire, and Pydantic AI:**
## Why use Pydantic?[¶](https://docs.pydantic.dev/latest/#why-use-pydantic "Permanent link")
- **Powered by type hints** — with Pydantic, schema validation and serialization are controlled by type annotations; less to learn, less code to write, and integration with your IDE and static analysis tools. [Learn more…](https://docs.pydantic.dev/latest/why/#type-hints)
- **Speed** — Pydantic's core validation logic is written in Rust. As a result, Pydantic is among the fastest data validation libraries for Python. [Learn more…](https://docs.pydantic.dev/latest/why/#performance)
- **JSON Schema** — Pydantic models can emit JSON Schema, allowing for easy integration with other tools. [Learn more…](https://docs.pydantic.dev/latest/why/#json-schema)
- **Strict** and **Lax** mode — Pydantic can run in either strict mode (where data is not converted) or lax mode where Pydantic tries to coerce data to the correct type where appropriate. [Learn more…](https://docs.pydantic.dev/latest/why/#strict-lax)
- **Dataclasses**, **TypedDicts** and more — Pydantic supports validation of many standard library types including `dataclass` and `TypedDict`. [Learn more…](https://docs.pydantic.dev/latest/why/#dataclasses-typeddict-more)
- **Customisation** — Pydantic allows custom validators and serializers to alter how data is processed in many powerful ways. [Learn more…](https://docs.pydantic.dev/latest/why/#customisation)
- **Ecosystem** — around 8,000 packages on PyPI use Pydantic, including massively popular libraries like *FastAPI*, *huggingface*, *Django Ninja*, *SQLModel*, & *LangChain*. [Learn more…](https://docs.pydantic.dev/latest/why/#ecosystem)
- **Battle tested** — Pydantic is downloaded over 360M times/month and is used by all FAANG companies and 20 of the 25 largest companies on NASDAQ. If you're trying to do something with Pydantic, someone else has probably already done it. [Learn more…](https://docs.pydantic.dev/latest/why/#using-pydantic)
[Installing Pydantic](https://docs.pydantic.dev/latest/install/) is as simple as: `pip install pydantic`
## Pydantic examples[¶](https://docs.pydantic.dev/latest/#pydantic-examples "Permanent link")
To see Pydantic at work, let's start with a simple example, creating a custom class that inherits from `BaseModel`:
Validation Successful
```
```
1. `id` is of type `int`; the annotation-only declaration tells Pydantic that this field is required. Strings, bytes, or floats will be coerced to integers if possible; otherwise an exception will be raised.
2. `name` is a string; because it has a default, it is not required.
3. `signup_ts` is a [`datetime`](https://docs.python.org/3/library/datetime.html#datetime.datetime) field that is required, but the value `None` may be provided; Pydantic will process either a [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time) integer (e.g. `1496498400`) or a string representing the date and time.
4. `tastes` is a dictionary with string keys and positive integer values. The `PositiveInt` type is shorthand for `Annotated[int, annotated_types.Gt(0)]`.
5. The input here is an [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formatted datetime, but Pydantic will convert it to a [`datetime`](https://docs.python.org/3/library/datetime.html#datetime.datetime) object.
6. The key here is `bytes`, but Pydantic will take care of coercing it to a string.
7. Similarly, Pydantic will coerce the string `'1'` to the integer `1`.
8. We create instance of `User` by passing our external data to `User` as keyword arguments.
9. We can access fields as attributes of the model.
10. We can convert the model to a dictionary with [`model_dump()`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_dump).
If validation fails, Pydantic will raise an error with a breakdown of what was wrong:
Validation Error
```
```
1. The input data is wrong here — `id` is not a valid integer, and `signup_ts` is missing.
2. Trying to instantiate `User` will raise a [`ValidationError`](https://docs.pydantic.dev/latest/api/pydantic_core/#pydantic_core.ValidationError) with a list of errors.
## Who is using Pydantic?[¶](https://docs.pydantic.dev/latest/#who-is-using-pydantic "Permanent link")
Hundreds of organisations and packages are using Pydantic. Some of the prominent companies and organizations around the world who are using Pydantic include:
[](https://docs.pydantic.dev/latest/why/#org-adobe "Adobe")
[](https://docs.pydantic.dev/latest/why/#org-amazon "Amazon and AWS")
[](https://docs.pydantic.dev/latest/why/#org-anthropic "Anthropic")
[](https://docs.pydantic.dev/latest/why/#org-apple "Apple")
[](https://docs.pydantic.dev/latest/why/#org-asml "ASML")
[](https://docs.pydantic.dev/latest/why/#org-astrazeneca "AstraZeneca")
[](https://docs.pydantic.dev/latest/why/#org-cisco "Cisco Systems")
[](https://docs.pydantic.dev/latest/why/#org-comcast "Comcast")
[](https://docs.pydantic.dev/latest/why/#org-datadog "Datadog")
[](https://docs.pydantic.dev/latest/why/#org-facebook "Facebook")
[](https://docs.pydantic.dev/latest/why/#org-github "GitHub")
[](https://docs.pydantic.dev/latest/why/#org-google "Google")
[](https://docs.pydantic.dev/latest/why/#org-hsbc "HSBC")
[](https://docs.pydantic.dev/latest/why/#org-ibm "IBM")
[](https://docs.pydantic.dev/latest/why/#org-intel "Intel")
[](https://docs.pydantic.dev/latest/why/#org-intuit "Intuit")
[](https://docs.pydantic.dev/latest/why/#org-ipcc "Intergovernmental Panel on Climate Change")
[](https://docs.pydantic.dev/latest/why/#org-jpmorgan "JPMorgan")
[](https://docs.pydantic.dev/latest/why/#org-jupyter "Jupyter")
[](https://docs.pydantic.dev/latest/why/#org-microsoft "Microsoft")
[](https://docs.pydantic.dev/latest/why/#org-molssi "Molecular Science Software Institute")
[](https://docs.pydantic.dev/latest/why/#org-nasa "NASA")
[](https://docs.pydantic.dev/latest/why/#org-netflix "Netflix")
[](https://docs.pydantic.dev/latest/why/#org-nsa "NSA")
[](https://docs.pydantic.dev/latest/why/#org-nvidia "NVIDIA")
[](https://docs.pydantic.dev/latest/why/#org-openai "OpenAI")
[](https://docs.pydantic.dev/latest/why/#org-oracle "Oracle")
[](https://docs.pydantic.dev/latest/why/#org-palantir "Palantir")
[](https://docs.pydantic.dev/latest/why/#org-qualcomm "Qualcomm")
[](https://docs.pydantic.dev/latest/why/#org-redhat "Red Hat")
[](https://docs.pydantic.dev/latest/why/#org-revolut "Revolut")
[](https://docs.pydantic.dev/latest/why/#org-robusta "Robusta")
[](https://docs.pydantic.dev/latest/why/#org-salesforce "Salesforce")
[](https://docs.pydantic.dev/latest/why/#org-starbucks "Starbucks")
[](https://docs.pydantic.dev/latest/why/#org-ti "Texas Instruments")
[](https://docs.pydantic.dev/latest/why/#org-twilio "Twilio")
[](https://docs.pydantic.dev/latest/why/#org-twitter "Twitter")
[](https://docs.pydantic.dev/latest/why/#org-ukhomeoffice "UK Home Office")
For a more comprehensive list of open-source projects using Pydantic see the [list of dependents on github](https://github.com/pydantic/pydantic/network/dependents), or you can find some awesome projects using Pydantic in [awesome-pydantic](https://github.com/Kludex/awesome-pydantic). |
| Shard | 140 (laksa) |
| Root Hash | 6903177193130590940 |
| Unparsed URL | dev,pydantic!docs,/latest/ s443 |