Pydantic ValidationError: Invalid Data

pydantic.ValidationError: 1 validation error for User

Quick Answer

The data does not match the Pydantic model schema. Check error details for which fields failed and fix the input data.

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. 1

    Read error details

    ValidationError lists every invalid field with the reason.

  2. 2

    Validate input data

    Clean data before passing to Pydantic.

  3. 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