“Mastering Data Validation in Python with Pydantic: The Backbone of Reliable AI Models”

Radhika KumbharRadhika Kumbhar
3 min read

✅ Introduction

In the ever-evolving world of Python programming and Artificial Intelligence (AI), data integrity plays a crucial role. One wrong input, and the entire model could collapse or give misleading predictions. That’s where Pydantic comes into the picture — a powerful data validation and parsing library that integrates seamlessly into Python projects, ensuring your data is always clean, well-structured, and predictable.

In this blog, I’ll walk you through the real-world usage of Pydantic, what I’ve learned from my recent deep-dive, and why it's becoming an essential tool in every modern Python developer’s toolbox.

🔍 Why Pydantic?

Whether you are developing a REST API, building an AI pipeline, or handling complex JSON configurations, data validation is non-negotiable. Pydantic:

  • Validates data types and constraints

  • Offers nested model structures

  • Automatically parses inputs (even from dict/JSON)

  • Reduces boilerplate with clear, declarative syntax

  • Plays well with frameworks like FastAPI and ML systems

💡 Real-World Use Case: Patient Data Validation

Let’s consider a scenario from the healthcare domain — storing patient data in a clean, structured way. Here’s how Pydantic simplifies the process:

✅ Step 1: Define Nested Models

class Address(BaseModel):
    city: str
    state: str
    pincode: int

Using nested models like Address improves readability, reusability, and enforces structure.


✅ Step 2: Define the Main Model with Metadata and Constraints

class Patient(BaseModel):
    name: Annotated[str, Field(max_length=50, title='Name of the Patient')]
    email: EmailStr
    age: int = Field(gt=0, lt=120)
    weight: Annotated[float, Field(gt=0, strict=True)]  # kg
    height: Annotated[float, Field(gt=0)]  # m
    married: Annotated[bool, Field(default=None, description='Marital status')]
    allergies: Optional[List[str]] = None
    contact_details: Dict[str, str]
    address: Address
  • Field(...) allows us to set constraints (gt, lt) and add metadata.

  • EmailStr is a built-in validator to check valid emails.

  • Annotated helps enhance the field with type info and constraints.


✅ Step 3: Computed Fields

@computed_field
@property
def bmi(self) -> float:
    return round(self.weight / (self.height ** 2))

Even though bmi isn’t in the original data, we can compute it on the fly, which is great for feature engineering in AI systems.


✅ Step 4: Field Validators

@field_validator('email')
@classmethod
def email_validator(cls, value):
    valid_domains = ['hdfc.com', 'icici.com']
    if value.split('@')[-1] not in valid_domains:
        raise ValueError('Invalid Email')

Field validators are ideal for field-level custom validation logic — useful when certain business rules (like domain restrictions) apply.


✅ Step 5: Model Validators (Multi-field logic)

@model_validator(mode='after')
def validate_emergency_contact(cls, model):
    if model.age > 60 and 'emergency' not in model.contact_details:
        raise ValueError('Emergency contact is required for patients above 60')
    return model

Model validators allow cross-field validation, essential for real-world business rules and health checks in critical domains like finance and healthcare.


🔄 Data Exporting (Model to JSON/Dict)

temp = patient1.model_dump_json(include=['name', 'email'])
print(temp)

You can easily export your model to:

  • Dict using model_dump()

  • JSON using model_dump_json()

  • Use options like include, exclude, exclude_unset for clean control

🧠 Key Concepts Recap:

ConceptPurpose
BaseModelBase class for all Pydantic models
Field()Add metadata, constraints, default values
EmailStrValidates proper email format
@computed_fieldDynamically compute values not in the input
@field_validatorValidate individual fields
@model_validatorValidate model with multiple fields
Nested ModelsBetter structure and reusability
ExportingConvert models to dict/JSON with full control

🔗 Further Resources:

0
Subscribe to my newsletter

Read articles from Radhika Kumbhar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Radhika Kumbhar
Radhika Kumbhar