Discovering Pydantic: Making Data Validation Effortless in Python

Sudhanshu WaniSudhanshu Wani
3 min read

What Is Pydantic?

Pydantic is a Python library that helps you define data models using Python’s built-in type hints. Its secret sauce? It validates and parses your data automatically, making sure every piece fits perfectly where it should.

Think of Pydantic as a super-strict librarian who won’t let you shelve a book unless its spine label matches the catalogue. If something doesn’t fit, Pydantic will tell you exactly what’s wrong before it ends up causing chaos in your code.


Why Not Just Use Dictionaries?

It’s a great question! Dictionaries are flexible, but they don’t help you catch mistakes. If you mistype a key or get the type wrong, your program might crash much later, in a mysterious way. Pydantic models are self-documenting, safe, and predictable.

Analogy:
Think about building something with LEGO blocks. Would you rather have a set with clear instructions and the right pieces, or just a random bucket? Pydantic gives you the instruction manual.


A First Encounter: Creating a Data Model

Let’s paint a picture. Suppose I’m writing a program that needs to handle information about books. Some data comes in as dictionaries, sometimes with typos, or the wrong types (“ten” instead of 10). Here’s how I might start with Pydantic:

from pydantic import BaseModel
from typing import Optional

class Book(BaseModel):
    title: str
    author: Optional[str]
    year_published: Optional[int]

This tiny bit of code creates a “data blueprint” for every book my program should handle. The title must be a string, author can be a string or None, and year_published can be an integer or None.


Letting Pydantic Handle the Hard Work

Let’s say someone hands me this data:

data = {
    "title": "Pydantic for Beginners",
    "author": "Jane Doe",
    "year_published": "2022"
}

Notice that year_published is a string, not an integer. If I create a Book model with Pydantic:

book = Book(**data)
print(book)

Surprise! Pydantic automatically converts "2022" to 2022 as an integer.

If the data is really off for example, if the title is missing:

bad_data = {
    "author": "Jane Doe",
    "year_published": 2022
}

book = Book(**bad_data)

Pydantic will raise a clear validation error:

pydantic.error_wrappers.ValidationError: 1 validation error for Book
title
  field required (type=value_error.missing)

Why Does This Matter?

Before Pydantic, validating data meant a tangle of conditional code. With Pydantic, you describe your data model once, and let Python’s type hints do all the heavy lifting.

A Typical Use Case

If you’re writing an API, processing user input, or even just reading configuration files, Pydantic ensures your data is always the right shape, with the right types and tells you exactly where things go wrong.


A Bit More Magic: Custom Validation

You can even add extra checks. Suppose my library catalog only wants books published after 1990:

from pydantic import validator

class Book(BaseModel):
    title: str
    year_published: int

    @validator('year_published')
    def year_must_be_recent(cls, v):
        if v < 1990:
            raise ValueError('Only books newer than 1990 allowed')
        return v

Now, any book older than 1990 will cause an error. No manual validation code scattered everywhere. Just one neat place!


In Summary

Pydantic is like having an expert librarian in your code, making sure everything is in the right place before you even use it. If you want to write cleaner, more reliable Python code without a mess of manual validation, give Pydantic a try!

It turned my messy, error-prone process into one line: “Pydantic, is this data okay?” And if not, it tells you exactly what’s wrong.


Happy validating!

0
Subscribe to my newsletter

Read articles from Sudhanshu Wani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sudhanshu Wani
Sudhanshu Wani