Skip to main content

How to List the Errors of a Pydantic ValidationError with errors()

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

When Pydantic rejects data it raises a ValidationError whose printed form is long and technical. error.errors() gives the same information as a list of dictionaries, one per problem, so you can turn it into a short message of your own. This guide shows the fields you get, with real output from Pydantic 2.14.4.

Syntax​

Syntax
from pydantic import ValidationError

try:
<create or validate a model>
except ValidationError as error:
for problem in error.errors():
<use problem["loc"], problem["type"], problem["msg"]>
PartWhat it means
error.errors()A list with one dictionary per problem
problem["loc"]Where the problem is, as a tuple; the first item is the field's name
problem["type"]A short code, such as missing or int_parsing
problem["msg"]A sentence describing the problem

Pydantic's documentation: errors() "Returns a list of ErrorDetails errors found in the input data." Each one also has input, the value that failed, and some have ctx and url. "The first item in the loc list will be the field where the error occurred".

Prerequisites​

  • Pydantic 2 in a uv project. This guide uses Pydantic 2.14.4, added with uv add pydantic, on Python 3.15.2.

Step 1: Write the Script​

booking.py
from pydantic import BaseModel, ValidationError


class Booking(BaseModel):
name: str
nights: int


try:
Booking(nights="two")
except ValidationError as error:
for problem in error.errors():
print(problem["loc"], problem["type"], problem["msg"])

The booking.py script in a macOS-style code window: a Booking model with a name and a number of nights is created without a name and with nights set to two, and each error's location, type and message is printed.

The booking breaks two rules: name is missing, and "two" isn't an integer.

Step 2: Run It​

Terminal
uv run python booking.py
Output
('name',) missing Field required
('nights',) int_parsing Input should be a valid integer, unable to parse string as an integer

The output in a terminal: ('name',) missing Field required, then ('nights',) int_parsing Input should be a valid integer, unable to parse string as an integer.

Pydantic checked every field before raising, so both problems arrived together. loc[0] is the field's name, which makes a compact message easy: f"{problem['loc'][0]}: {problem['msg']}" gives name: Field required.

Common Mistakes​

MistakeWhat happensDo this instead
Stopping after the first errorThe user fixes one problem, then meets the nextLoop over all of errors()
Showing problem["input"] in a messageFor a missing field, input is the whole input, here {'nights': 'two'}, and it may hold secretsShow the field and message, not the input

Troubleshooting​

SymptomLikely causeFix
problem["loc"] has more than one itemThe error is inside a nested model or listJoin the items, as in ".".join(map(str, problem["loc"]))

FAQ​

How do I count the problems?​

error.error_count() returns the number; here it's 2.

Can I change the messages?​

Yes. Map problem["type"] to your own text, and fall back to problem["msg"] for types you haven't mapped.

Conclusion​

error.errors() turned one ValidationError into two clear entries, ('name',) missing Field required and ('nights',) int_parsing ..., ready to print in your own words.

Additional Resources​

  • Error handling — errors(), error_count() and the fields of each error.