Why This Happens
Pydantic validates data against model schemas at runtime. When data does not conform, it raises ValidationError with details about every invalid field.
The Problem
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
User(name='Alice', age='not_a_number')The Fix
from pydantic import BaseModel, ValidationError
class User(BaseModel):
name: str
age: int
try:
user = User(**raw_data)
except ValidationError as e:
print(e.json())Step-by-Step Fix
- 1
Read error details
ValidationError lists every invalid field with the reason.
- 2
Validate input data
Clean data before passing to Pydantic.
- 3
Use Optional for nullable fields
Use Optional[type] for fields that may be null.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free