Python

How to Use Pydantic BaseModel: A Comprehensive Guide

A Pydantic provides a class that defines or specifies the structure or format of your data and the validation rules. It must pass to be considered valid which is known as BaseModel. The BaseModel class provides a comprehensive way to define and manage the data models. In this article, we explore the pydantic.BaseModel’s functions and demonstrate how to use the BaseModel using a simple example.

Features of BaseModel

BaseModel provides a number of features that make it a valuable tool for data validation and serialization/deserialization:

Field Validation: BaseModel allows you to define the type, required/optional, and other constraints for each field in your model. This helps to ensure that the data that is stored in your model is valid.

Serialization/deserialization: BaseModel can be used to serialize and deserialize the data to and from different formats such as JSON, XML, and YAML. This makes it simple to work with data in various systems and applications.

Model Inheritance: BaseModel supports model inheritance which allows you to create more complex models by extending from the existing models. This can be a helpful way to reuse the code and organize your data.

BaseModel Methods and Attributes

The BaseModel class in Pydantic includes various methods. Some of them are:

1. Model_dump(): The Model as a Dictionary

An instance of the model can be transformed into a dictionary using the model_dump() method. Using this, the model can be serialized to JSON or other formats.

This method returns a dictionary that contains the model’s fields and their values. The field names represent the dictionary’s keys, and the field values represent its values.

from pydantic import BaseModel

class Student(BaseModel):
    name: str
    enroll: int

student = Student(name="Max", enroll =101)
student_dict = student. model_dump()
print(student_dict)

Output:

2. Model_dump_json: JSON Representation

The model_dump_json() method returns a JSON string representation of the model which is useful for transmitting or storing the data over the network in files. The fields and values of the model are returned in a JSON string by the model_dump_json() method.

from pydantic import BaseModel
class Student(BaseModel):
    name: str
    enroll: int
student = Student(name="Tim", enroll =103)
student_user = student.model_dump_json()
print(student_user)

The model_dump_json() method is similar to the model_dump() method, but it has a few additional features like it can be used to serialize the nested models and also serialize the models with custom encoders and decoders.

3. Model_copy(): Shallow Copy of the Model

The model_copy() function can be used to make a shallow or deep copy of the model. The new instance of the model is a different instance, but it consists of the same values as the original. It can be helpful when you need to change or manipulate the data while maintaining the original version.

from pydantic import BaseModel
class Student(BaseModel):
    name: str
    marks: int

stu = Student(name='Sara', marks=8)

stu_copy = stu.model_copy()
print("before update: ")
print(stu_copy)
stu_copy.name = 'Rob'
stu_copy.marks = 9
print("after update: ")
print(stu_copy)

Output:

4. Model_validate(): Loading the Data from the Objects

The model_validate() method in Pydantic BaseModel is a class method that can be used to create an instance of the model from a dictionary.

from pydantic import BaseModel

class Student(BaseModel):
    name: str
    marks: int

student= Student.model_validate({'name': 'Jack', 'marks': 6})
print(student)

The model_validate()  method validates the data in the dictionary before creating the instance of the model. If the data is invalid, a ValidationError exception is raised. The model_validate() method returns a new model instance. The instance has the values of the fields that are passed to the method.

Output:

5. Model_validate_json()

The model_validate_json() method can be used to create an instance of the model from a file or dictionary. This can be helpful when parsing the data from the files that are stored in JSON or other formats. The model_validate_json() method takes a file path as its argument. The file path can be a path to a file on the local filesystem or it can be a path to a file in a remote location.

Code:

from pydantic import BaseModel
class Student(BaseModel):
    name: str
    grade: str
# Assuming 'data.json' contains: {"name": "Alex", "grade": "B"}
student = Student.model_validate_json('{"name": "Alex", "grade": "B"}')
print(student)

The method takes a dictionary or file as its argument and returns a BaseModel instance if the dictionary is valid or raises an exception if the dictionary is invalid.

6. Model_json_schema(): Model as JSON Schema

The model_json_schema() method can be used to get the JSON schema for the model. The JSON schema is a dictionary that describes the fields of the model including their types, names, and default values.

Code:

from pydantic import BaseModel
class Student(BaseModel):
    name: str
    marks: int = 5
stu = Student.model_json_schema()
print(stu)

As you can see, the JSON schema includes the name and marks fields. The name field is required and the marks field has a default value of 5.

7. Construct(): Creating Models without Validation

It is used to create an instance of the model without validating the data. This is useful when the data is known to be valid. The model_construct() method takes a dictionary as its argument. The dictionary can be in any format that the model’s validator can parse.

Code:

from pydantic import BaseModel

class Address(BaseModel):
    city: str
    code: int

address = Address.model_construct({'city': 'Paris', 'code': 44970})
print(address)

The model_construct() method will not validate the data in the dictionary. The data must be valid before it is passed to the method.

8. __Fields_Set__: Set of Initialized Fields

The __fields_set__ attribute in Pydantic BaseModel is a set that contains the names of all the fields in the model. This attribute is used by the construct() and model_construct() methods to ensure that only valid fields are set on the model.

Code:

print(person.__fields_set__)

Output:

How to Use the Pydantic BaseModel

Here is an example of using the Pydantic BaseModel to create a data model for the “Employee” class.

Code:

from pydantic import BaseModel

class Employee(BaseModel):
    name: str
    age: int
    email: str

emp = Employee(name="Alex", age=30, email="[email protected]")

print(emp)

In this code, we create a Pydantic model called “Employee” where the name attribute is a string, the age attribute is an integer, and the email attribute is a string.

When we create an instance of the “Employee” model, we pass the values for the defined attributes. The “Employee” model then validates these values to ensure that they are of the correct type and they meet the specified constraints. In this case, the name attribute must be a string, the age attribute must be an integer, and the email attribute must be a string that is a valid email address.

If the validation of the “Employee” model is successful, the “emp” variable contains a valid “Employee” object. We can then print the “emp” object to see its values.

Output:

As you can see, the “Employee” object has the values that we pass when we create it. The “Employee” object is also valid which means that the data that we pass in meets the specified constraints.

However, if we try to pass the invalid data to our model, it raises the ValidationError. Let’s input some invalid data into our “Employee” model and observe how Pydantic’s BaseModel deals with it.

Code:

invalid_employee_data = {
    "name": "Alex",
    "age": "thirty",  # Invalid age format
    "email": " [email protected]"
}

try:
    invalid_employee = Employee(**invalid_employee_data)
    print(invalid_employee)
except Exception as e:
    print(f"Error: {e}")

Output:

As you can see, an error is raised due to the invalid age format. Pydantic’s BaseModel performs a thorough validation and raises a ValidationError when it comes across the data that doesn’t match the expected types.

Conclusion

We explored about the BaseModel, a class that defines the structure of your data and the validation rules. The models in Pydantic is inheritted from the BaseModel. First, we discussed some of the BaseModel’s key features and then explored a few methods that are offered by BaseModel. We demonstrated an example on how to use the Pydantic BaseModel to create a data model and how you can perform the data validation using it.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.