Unlock Efficient Data Validation in Python with Pydantic
This article walks through installing Pydantic, creating basic and nested models, handling validation errors, using default values, defining custom validators, loading settings from environment variables, and serializing models to dictionaries and JSON, demonstrating how Pydantic streamlines data validation in Python.
Pydantic is a powerful Python library that leverages type hints to provide data validation and settings management.
Installation
Install the library via pip:
pip install pydanticSimple Example
Define a model by inheriting BaseModel and declare fields with type annotations. Pydantic validates the data when an instance is created.
from pydantic import BaseModel
class User(BaseModel):
id: int
name: str
email: str
user = User(id=1, name="John Doe", email="[email protected]")
print(user)The resulting output is shown below:
ValidationError Example
Providing a value of the wrong type triggers a ValidationError:
from pydantic import ValidationError
try:
user = User(id="one", name="John Doe", email="[email protected]")
except ValidationError as e:
print(e)The error message indicates that id must be an integer:
Using Default Values
Optional fields can be declared with typing.Optional and a default value:
from typing import Optional
class User(BaseModel):
id: int
name: str
email: str
age: Optional[int] = None # Optional field with a default value of None
user = User(id=1, name="John Doe", email="[email protected]")
print(user.age) # Output: NoneCustom Validators
Use the @validator decorator to enforce custom validation logic:
from pydantic import validator, ValidationError
class User(BaseModel):
id: int
name: str
email: str
@validator('name')
def name_must_contain_space(cls, value):
if ' ' not in value:
raise ValueError('Name must contain a space')
return value
try:
user = User(id=1, name="JohnDoe", email="[email protected]")
except ValidationError as e:
print(e)The validation error is displayed below:
Nested Types
Pydantic supports nested models, enabling complex data structures:
class Address(BaseModel):
street: str
city: str
zipcode: str
class User(BaseModel):
id: int
name: str
address: Address
address_data = {'street': '123 Main St', 'city': 'Anytown', 'zipcode': '12345'}
user_data = {'id': 1, 'name': 'John Doe', 'address': address_data}
user = User(**user_data)
print(user)Resulting output:
Reading Environment Variables
Use BaseSettings to load configuration from a .env file:
from pydantic import BaseSettings
class Settings(BaseSettings):
app_name: str
admin_email: str
class Config:
env_file = '.env'
settings = Settings()
print(settings.app_name)
print(settings.admin_email)Example .env file:
APP_NAME=My Awesome App
[email protected]Serialization and Deserialization
Models can be converted to dictionaries or JSON and reconstructed from them:
from pydantic import BaseModel
class User(BaseModel):
id: int
name: str
email: str
user = User(id=1, name="John Doe", email="[email protected]")
user_dict = user.model_dump()
print(user_dict)
user_json = user.model_dump_json()
print(user_json)
new_user = User(**user_dict)
print(new_user)Output of the serialization steps is shown below:
Conclusion
Pydantic provides a robust validation and parsing mechanism for Python through type annotations. It simplifies data integrity checks, improves code readability, and is a valuable tool for both simple applications and complex systems.
AI Algorithm Path
A public account focused on deep learning, computer vision, and autonomous driving perception algorithms, covering visual CV, neural networks, pattern recognition, related hardware and software configurations, and open-source projects.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
