An introduction to Pydantic (with basic example)

Updated: November 30, 2023 By: Khue Post a comment

This article will help you take your first steps in using Pydantic. While prior knowledge of Pydantic is not required, a basic understanding of Python programming is necessary.

What is Pydantic and how to install it?

Pydantic is a Python library for data validation and parsing using type hints1. It is fast, extensible, and easy to use. To install Pydantic, you can use pip or conda commands, like this:

pip install pydantic

Or like this:

conda install pydantic -c conda-forge

Why use Pydantic?

Pydantic isn’t a must-do, but a should-do. The library brings to the table a plethora of benefits:

  • It is easy to use and has a simple API, making it accessible to developers of all levels.
  • It performs fast validation and integrates seamlessly with Python’s data structures and type hints, making it suitable for use in high-performance applications.
  • It can automatically generate documentation for your data models, saving time and making it easier to understand the structure of your data.
  • It allows custom validation rules and serialization and deserialization, making it possible to handle a wide range of data validation scenarios.
  • It can be easily integrated with FastAPI, a high-performance Python web framework, to provide automatic request and response validation for APIs.
  • It ensures that the data used in a project is consistent and meets the required standards, reducing the risk of errors and making it easier to maintain the codebase.

Pydantic is more and more used by companies of all sizes. Mastering it can help you get a well-paid job or build your own dream project (and maybe a startup).

At this point, words alone won’t do. Let’s write some code to experience Pydantic. It may seem tough at first, but believe me, you’ll love it really soon.

Practical example

This is a very, very basic example of using Pydantic, in a step-by-step fashion. Here, we’ll use Pydantic to crate and validate a simple data model that represents a person with information including name, age, address, and whether they are active or not.

1. Import the BaseModel class from Pydantic. This is the base class for all Pydantic models:

from pydantic import BaseModel

2. Define your data model by creating a new class that inherits from BaseModel. You can use type hints to specify the data types of the attributes. You can also use default values or validators to customize the behavior of your model.

# Define a Pydantic model
class Person(BaseModel):
    name: str  # required
    age: int  # required
    address: str = None  # optional
    is_active: bool = True  # default value

    # This will help to validate the assignment after the object is created
    class Config:
        validate_assignment = True

3. Create an instance of your model by passing the data as keyword arguments. Pydantic will validate the data and raise an error if it does not match the model definition.

Valid input:

p1 = Person(name="Mr. Turtle", age=999, address="Sling Academy")
# name='Mr. Turtle' age=999 address='Sling Academy' is_active=True

p2 = Person(name="The Devil of Darkness", age=415, is_active=False)
# name='The Devil of Darkness' age=415 address=None is_active=False

Invalid input:

p3 = Person(name="Mrs. Fox", age="abc")
# ValidationError: 1 validation error for Person
# Input should be a valid integer, unable to parse string as an integer

4. You can access and modify the attributes of your model instance as normal Python attributes. Pydantic will check the types and values of the attributes whenever you assign them. = "Mr. Turtle the Old Man"
p1.age = 1000

# name='Mr. Turtle the Old Man' age=1000 address='Sling Academy' is_active=True

If you try to assign an invalid value, a ValidationError will be raised:

p1.address = 123456
# ValidationError: 1 validation error for Person
# address
# Input should be a valid string [type=string_type, input_value=123456, input_type=int]

5. You can also use methods and properties on your model instance, such as model_dump()model_dump_json()model_copy(), etc. These methods will respect the validation rules and serialization options of your model.

# {'name': 'Mr. Turtle', 'age': 999, 'address': 'Sling Academy', 'is_active': True}

p4 = p1.model_copy()
# {"name":"Mr. Turtle","age":999,"address":"Sling Academy","is_active":true}

If you don’t fully grasp the example above, there’s nothing to worry about. Every aspect of Pydantic will be explained in more detail in the tutorials to come.