Pydantic is a Python library that simplifies data validation using type hints. It ensures data integrity and offers an easy way to create data models with automatic type checking and validation.

In software applications, reliable data validation is crucial to prevent errors, security issues, and unpredictable behavior.

This guide provides best practices for using Pydantic in Python projects, covering model definition, data validation, error handling, and performance optimization.


Installing Pydantic

To install Pydantic, use pip, the Python package installer, with the command:

pip install pydantic

This command installs Pydantic and its dependencies.

Basic Usage

Create Pydantic models by making classes that inherit from BaseModel. Use Python type annotations to specify each field's type:

from pydantic import BaseModel

class User(BaseModel):
    id: int
    name: str
    email: str

Pydantic supports various field types, including int, str, float, bool, list, and dict. You can also define nested models and custom types:

from typing import List, Optional
from pydantic import BaseModel

class Address(BaseModel):
    street: str
    city: str
    zip_code: Optional[str] = None

class User(BaseModel):
    id: int
    name: str
    email: str
    age: Optional[int] = None
    addresses: List[Address]

Once you've defined a Pydantic model, create instances by providing the required data. Pydantic will validate the data and raise errors if any field doesn't meet the specified requirements:

user = User(
    id=1,
    name="John Doe",
    email="john.doe@example.com",
    addresses=[{"street": "123 Main St", "city": "Anytown", "zip_code": "12345"}]
)

print(user)

# Output:
# id=1 name='John Doe' email='john.doe@example.com' age=None addresses=[Address(street='123 Main St', city='Anytown', zip_code='12345')]

Are you tired of writing the same old Python code? Want to take your programming skills to the next level? Look no further! This book is the ultimate resource for beginners and experienced Python developers alike.

Get "Python's Magic Methods - Beyond __init__ and __str__"

Magic methods are not just syntactic sugar, they're powerful tools that can significantly improve the functionality and performance of your code. With this book, you'll learn how to use these tools correctly and unlock the full potential of Python.

Defining Pydantic Models

Pydantic models use Python type annotations to define data field types.

They support various built-in types, including:

  • Primitive types: str, int, float, bool
  • Collection types: list, tuple, set, dict
  • Optional types: Optional from the typing module for fields that can be None
  • Union types: Union from the typing module to specify a field can be one of several types

Example:

from typing import List, Dict, Optional, Union
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    price: float
    tags: List[str]
    metadata: Dict[str, Union[str, int, float]]

class Order(BaseModel):
    order_id: int
    items: List[Item]
    discount: Optional[float] = None

Custom Types

In addition to built-in types, you can define custom types using Pydantic's conint, constr, and other constraint functions.

These allow you to add additional validation rules, such as length constraints on strings or value ranges for integers.

Example:

from pydantic import BaseModel, conint, constr

class Product(BaseModel):
    name: constr(min_length=2, max_length=50)
    quantity: conint(gt=0, le=1000)
    price: float

product = Product(name="Laptop", quantity=5, price=999.99)

Required vs. Optional Fields

By default, fields in a Pydantic model are required unless explicitly marked as optional.

If a required field is missing during model instantiation, Pydantic will raise a ValidationError.

Example:

from pydantic import BaseModel

class User(BaseModel):
    id: int
    name: str
    email: str

user = User(id=1, name="John Doe")


# Output
#  Field required [type=missing, input_value={'id': 1, 'name': 'John Doe'}, input_type=dict]

Optional Fields with Default Values

Fields can be made optional by using Optional from the typing module and providing a default value.

Example:

from pydantic import BaseModel
from typing import Optional

class User(BaseModel):
    id: int
    name: str
    email: Optional[str] = None

user = User(id=1, name="John Doe")

In this example, email is optional and defaults to None if not provided.

Nested Models

Pydantic allows models to be nested within each other, enabling complex data structures.

Nested models are defined as fields of other models, ensuring data integrity and validation at multiple levels.

Example:

from pydantic import BaseModel
from typing import Optional, List


class Address(BaseModel):
    street: str
    city: str
    zip_code: Optional[str] = None

class User(BaseModel):
    id: int
    name: str
    email: str
    addresses: List[Address]

user = User(
    id=1,
    name="John Doe",
    email="john.doe@example.com",
    addresses=[{"street": "123 Main St", "city": "Anytown"}]
)

Best Practices for Managing Nested Data

When working with nested models, it's important to:

  • Validate data at each level: Ensure each nested model has its own validation rules and constraints.
  • Use clear and consistent naming conventions: This makes the structure of your data more readable and maintainable.
  • Keep models simple: Avoid overly complex nested structures. If a model becomes too complex, consider breaking it down into smaller, more manageable components.

Data Validation

Pydantic includes a set of built-in validators that handle common data validation tasks automatically.

These validators include:

  • Type validation: Ensures fields match the specified type annotations (e.g., int, str, list).
  • Range validation: Enforces value ranges and lengths using constraints like conint, constr, confloat.
  • Format validation: Checks specific formats, such as EmailStr for validating email addresses.
  • Collection validation: Ensures elements within collections (e.g., list, dict) conform to specified types and constraints.

These validators simplify the process of ensuring data integrity and conformity within your models.

Here are some examples demonstrating built-in validators:

Tagged in: