Stop Writing Manual Validation: Master Pydantic
Replace manual validation in Python with Pydantic: models, type annotations, data coercion, custom validators, serialization and using Pydantic with FastAPI.

Introduction
Validation is a crucial part of any software that collects data from users or other external sources. This ensures that the data used in your program or application remains correct and well-structured. Serialization, which is the process of converting data from one format to another for transmission or storage, is also essential to ensure that we use the best data format for the right scenario (for example, using JSON for transmission and objects for storage in a database).
Pydantic is a library in the Python ecosystem that elegantly solves both use cases. To benefit greatly from the content of this article, it is assumed that you have familiarity with Python programming and a basic understanding of Object-Oriented Programming in Python.
Why Pydantic
Understanding Data Validation in Traditional Python
Before diving into Pydantic code, let’s explore how data validation can be handled for a practical scenario in plain, regular Python. Assume we have a software application for students in a university to register their courses for the semester.
# A class to store student data; this can be an instance of a record in the database
class Student:
def __init__(self, name: str, email: str, year: int):
self.name = name
self.email = email
self.year = year
self.courses = set()
def add_course(self, course: str):
self.courses.add(course)
return True
def remove_course(self, course: str):
self.courses.remove(course)
return True
# Function to handle student registration
def register_student(name: str, email: str, year: int):
return Student(name, email, year)
This is a simple way to represent a student record in our application, allowing for the storage and manipulation of student data through methods like adding or removing courses. However, validation for attributes like year can become intricate and verbose.
# Adding validation with an updated function version
def register_student_v2(name: str, email: str, year: int):
if year not in range(1, 7): # from 1 to 6, 7 is not included
raise ValueError("Student Year must be between 1 and 6 years")
return Student(name, email, year)
try:
james = register_student_v2(name="James Okon", email="james@mail.com", year=1)
grace = register_student_v2(name="Grace Mabel", email="grace@gmail.com", year=2)
brian = register_student_v2(name="Brian Obot", email="brianobot9@gmail.com", year=1000)
except ValueError as err:
print(f"Failed to register student because: {err}")
This illustrates how we incorporate validation for year, ensuring it meets predetermined requirements. However, adding validation checks for other properties requires additional verbose code, leading to repetitive structures.
Why To Use Pydantic?
Enter Pydantic. Here are some compelling reasons why Pydantic is an excellent option for validating and serializing data:
- Performance: Pydantic’s core validation logic is implemented in Rust; as a result, it stands out as one of the fastest data validation libraries for Python.
- Type Hints Powered Schema Validation: The schema that Pydantic uses to validate data is generally defined using Python type annotations/hints.
- Serialization: Pydantic excels at data serialization, crucial when converting data for network transfer or file storage.
- Strict Mode and Data Coercion: Pydantic attempts to coerce incorrect data into the right type by default, converting entries like numeric strings to integers as appropriate.
- Customization: Custom validation logic fits unique use cases seamlessly within Pydantic’s framework.
- Extensive Use in Big Projects: Many significant projects, especially in AI, leverage Pydantic, making it an essential skill for a Python developer.
Introduction to Pydantic
Pydantic is the most widely used data validation library for Python. It allows users to define validation and serialization rules through type annotations, enabling robust data management practices.
Basic Usage Example
Let’s revisit the student registration example but utilize Pydantic for validation.
from typing import Annotated
from pydantic import BaseModel, Field, EmailStr, ValidationError
class Student(BaseModel):
name: str
email: EmailStr
year: Annotated[int, Field(ge=1, le=6)]
try:
james = Student(name="James Okon", email="james@mail.com", year=1)
grace = Student(name="Grace Mabel", email="grace@gmail.com", year=2)
brian = Student(name="Brian Obot", email="brianobot9@gmail.com", year=1000)
except ValidationError as err:
print(f"Failed to register student because: {err}")
else:
print(james)
print(grace)
print(brian)
In just a few lines, we leverage Pydantic to define validations, resulting in cleaner and more maintainable code. The validation error messages also provide immediate feedback, making debugging simpler.
Key Features of Pydantic
Type Annotation: The Power of Conventions
Pydantic’s expressive validation derives from Python’s type annotation system, allowing developers to explicitly define variable types. Here’s how type annotation works:
name: str = "Bruce Wayne"
age: int = 43
height_in_feets: float = 6.4
This flexibility enables you to define types without runtime enforcement. However, Pydantic utilizes annotations to enforce these checks when models are instantiated.
Common Type Annotations in Pydantic
- str: Represents any string type, useful for names and addresses.
- int: Represents whole numbers.
- float: Represents floating-point numbers.
- EmailStr: A specialized type to validate email formats right out of the box.
Data Coercion with Pydantic
Pydantic performs automatic data coercion, allowing types to be converted when possible to match defined schemas. This feature lessens the need for redundant conversion checks.
class Example(BaseModel):
age: int
example = Example(age='25')
print(example.age) # Output: 25
In this instance, Pydantic auto-converts the string '25' to the integer 25, enhancing code robustness and reducing potential user error.
Technical Deep-Dive Sections
Advanced Validation Techniques
Custom Validators
Pydantic supports validators that can enforce complex rules or conditions for model attributes. This offers control over how data is validated and transformed.
from pydantic import validator
class Student(BaseModel):
name: str
email: EmailStr
year: int
@validator('year')
def validate_year(cls, value):
if value < 1 or value > 6:
raise ValueError('Year must be between 1 and 6')
return value
The custom validator in the Student model ensures that the year is validated against the specified criteria.
Built-in and User-defined Validations
Utilizing built-in validators can tackle most common cases. However, when more complex rules are necessary, user-defined validation methods can introduce needed logic.
# User-defined validators for format and criteria
class User(BaseModel):
username: str
age: int
@validator('username')
def username_must_not_contain_spaces(cls, value):
if ' ' in value:
raise ValueError('Username must not contain spaces')
return value
User-defined validators provide high customization based on specific app needs, thereby maintaining code clarity.
Serialization and Deserialization with Pydantic
Transforming Data Formats
Serialization allows Python objects to be transformed into formats such as JSON. Pydantic simplifies this process, allowing for straightforward conversion, ensuring that complex data types are adequately formatted.
student = Student(name="James Okon", email="james@mail.com", year=1)
student_json = student.json()
print(student_json)
This snippet serializes the Student instance into JSON for efficient transmission across web services or for storage.
Deserializing Data
Pydantic also parses JSON strings into Python objects, automatically inferring types and validating data upon model construction.
data = '{"name": "James Okon", "email": "james@mail.com", "year": 2}'
student_instance = Student.parse_raw(data)
print(student_instance)
This example illustrates how incoming data can be efficiently parsed and validated, streamlining data intake processes.
Working with Nested Models
Creating Hierarchical Data Structures
Pydantic’s design allows for the representation of nested data structures through multiple models. This design is particularly useful in RESTful APIs where data returned often contains multiple layers.
from typing import List
class Course(BaseModel):
title: str
credits: int
class StudentWithCourses(BaseModel):
name: str
email: EmailStr
courses: List[Course]
This model shows how Pydantic handles complex hierarchies and encapsulates related data naturally.
Handling Lists and Dicts
When collections like lists or dictionaries come into play, Pydantic validates and parses items effectively, facilitating robust data handling strategies.
class GroupedStudents(BaseModel):
group_name: str
students: List[Student]
group = GroupedStudents(group_name="Freshmen", students=[james, grace])
print(group.json())
Here, Pydantic allows seamless handling of multi-part data constructs, making it a powerful tool for managing related datasets.
Edge Cases with Pydantic
Handling Optional Fields
Often, datasets contain optional fields. Pydantic allows these fields to be declared using Optional and will validate their presence and data type correctly.
from typing import Optional
class StudentWithOptional(BaseModel):
name: str
email: EmailStr
year: int
phone: Optional[str] = None # This field is optional
student = StudentWithOptional(name="Mary Jane", email="mary.jane@mail.com", year=2)
print(student.phone) # Output: None because no phone number was provided
In this case, the phone field can either have a value or be absent, which Pydantic handles gracefully.
Complex Nested Validations
Pydantic models can also contain nested models, allowing for validations that span multiple levels of data structures.
class Teacher(BaseModel):
name: str
email: EmailStr
class School(BaseModel):
name: str
teachers: List[Teacher]
school_data = {
"name": "Springfield High",
"teachers": [
{"name": "Mr. Smith", "email": "smith@school.com"},
{"name": "Ms. Johnson", "email": "johnson@school.com"},
]
}
school = School(**school_data)
print(school.json())
This snippet demonstrates how complex data structures can be validated all at once, thanks to Pydantic’s capabilities.
Real-World Applications of Pydantic
In practical deployment, Pydantic’s utility shines. Many web applications and APIs leverage Pydantic for data validation and serialization, contributing immensely to reliability and clarity of data handling.
Applications in FastAPI
One of the most compelling showcases of Pydantic’s capabilities arises in the context of FastAPI, a modern web framework that utilizes Pydantic heavily for request validation and response serialization.
Example FastAPI integration with Pydantic is illustrated below:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
@app.post("/items/")
async def create_item(item: Item):
return item
In this FastAPI app, Pydantic is instrumental in defining and enforcing expected input formats for the endpoint /items/, facilitating straightforward integration and error management.
Applications in Machine Learning
Data validation and processing pipelines in machine learning applications are crucial for managing incoming data. Pydantic ensures that datasets conform to expected formats and types, simplifying preprocessing and reducing errors in training models.
from pydantic import BaseModel
class UserProfile(BaseModel):
name: str
age: int
email: EmailStr
preferences: List[str] = []
# Processing dimensionality reduction
def validate_user_data(user_data: dict) -> UserProfile:
return UserProfile(**user_data)
In this snippet, incoming user profiles are validated to ensure the data integrity required for further processing steps, improving model reliability.
Conclusion
In conclusion, Pydantic emerges as an extremely efficient tool for validating data in Python applications. By mastering its core features—including type annotations, built-in serialization, custom validations, and handling complex data structures—Python developers can significantly improve their data validation strategies. Pydantic’s versatility paves the way for easily creating scalable and maintainable codebases that uphold high standards of data integrity.
As you continue your journey with Pydantic, consider how its features can be applied to enhance the functionality and reliability of your applications. Mastering Pydantic not only enriches your skill set but also positions you favorably for tackling complex challenges in Python software development.
For more expansive insights and practical examples, refer to the Pydantic documentation.