Skip to content

pydantic-explain

Human-readable error messages for Pydantic validation errors.

Installation

With uv:

Bash
1
uv add pydantic-explain

Usage

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from pydantic import BaseModel, ValidationError
from pydantic_explain import format_errors


class Address(BaseModel):
    street: str
    zipcode: str


class User(BaseModel):
    name: str
    addresses: list[Address]


try:
    User.model_validate({
        "name": "Alice",
        "addresses": [
            {"street": "123 Main St"},
            {"street": "456 Oak Ave", "zipcode": ["invalid"]},
        ],
    })
except ValidationError as e:
    print(format_errors(e))
Text Only
1
2
3
4
5
6
7
8
9
Validation failed for User with 2 errors

  addresses[0].zipcode
    Field required
    Got: (missing)

  addresses[1].zipcode
    Input should be a valid string
    Got: ['invalid']

Documentation

Full documentation is available at pydantic-explain docs.