Skip to main content

Pydantic: Data Validation for Production

When you build real AI applications, data comes from many places — user forms, API responses, CSV files, databases. This data can be wrong, missing, or in the wrong format. If bad data enters your ML model or AI pipeline, it breaks everything.

Pydantic is a Python library that checks (validates) your data automatically before you use it.


1. The Problem: Trusting User Input

# Without validation — anything can go wrong
def create_student(name, age, score):
print(f"Created: {name}, Age: {age}, Score: {score}")

create_student("Sai", 20, 85) # Works fine
create_student("", -5, "not a number") # Bad data — but no error!

Without validation, your code silently accepts garbage data. This can crash your ML model later with confusing errors.


2. Installing Pydantic

pip install pydantic

3. Creating a Pydantic Model

A Pydantic model is like a blueprint that says: "This is what correct data looks like."

from pydantic import BaseModel

class Student(BaseModel):
name: str
age: int
score: float

# Good data — works fine
student = Student(name="Sai", age=20, score=85.5)
print(student)
# Output: name='Sai' age=20 score=85.5

# Bad data — Pydantic catches the error!
try:
bad_student = Student(name="Ravi", age="twenty", score=72.0)
except Exception as e:
print(f"Error: {e}")
# Output: Error: 1 validation error for Student
# age - Input should be a valid integer

The magic: Pydantic checks every field automatically. If the data does not match the type you specified, it throws a clear error immediately.


4. Auto-Conversion (Coercion)

Pydantic is smart — it tries to convert data to the right type when possible:

from pydantic import BaseModel

class Product(BaseModel):
name: str
price: float
quantity: int

# String "100" gets automatically converted to int 100
product = Product(name="Laptop", price="49999.99", quantity="5")
print(product.price) # Output: 49999.99 (float, not string!)
print(product.quantity) # Output: 5 (int, not string!)

5. Adding Validation Rules

You can add extra rules beyond just type checking:

from pydantic import BaseModel, Field

class Student(BaseModel):
name: str = Field(min_length=1, max_length=50)
age: int = Field(ge=1, le=120) # ge = greater or equal, le = less or equal
score: float = Field(ge=0, le=100)

# This works
student = Student(name="Sai", age=20, score=85.5)

# This fails — age is negative
try:
bad = Student(name="Ravi", age=-5, score=72.0)
except Exception as e:
print(f"Error: {e}")
# Output: Error: age - Input should be greater than or equal to 1

6. Optional Fields and Default Values

from pydantic import BaseModel
from typing import Optional

class UserProfile(BaseModel):
name: str
email: str
phone: Optional[str] = None # Optional — can be missing
country: str = "India" # Default value if not provided

user = UserProfile(name="Sai", email="sai@example.com")
print(user.country) # Output: India (default)
print(user.phone) # Output: None (not provided)

7. Validating API Responses

In real AI apps, you get data from API responses. Pydantic makes sure the response has the right structure:

from pydantic import BaseModel
from typing import List

class AIResponse(BaseModel):
model: str
answer: str
confidence: float

# Simulating an API response (dictionary)
raw_response = {
"model": "gemini-1.5-flash",
"answer": "Machine Learning is when computers learn patterns from data.",
"confidence": 0.95
}

# Validate and parse
response = AIResponse(**raw_response)
print(f"Model: {response.model}")
print(f"Answer: {response.answer}")
print(f"Confidence: {response.confidence * 100}%")

8. Converting to Dictionary and JSON

from pydantic import BaseModel

class Student(BaseModel):
name: str
age: int
score: float

student = Student(name="Sai", age=20, score=85.5)

# Convert to dictionary
student_dict = student.model_dump()
print(student_dict)
# Output: {'name': 'Sai', 'age': 20, 'score': 85.5}

# Convert to JSON string
student_json = student.model_dump_json()
print(student_json)
# Output: {"name":"Sai","age":20,"score":85.5}

Summary

FeatureWhat it DoesExample
BaseModelBlueprint for valid dataclass Student(BaseModel)
Type hintsChecks data types automaticallyage: int
Field()Adds rules like min/maxField(ge=0, le=100)
OptionalField that can be missingOptional[str] = None
Auto-conversionConverts "5" to 5 automaticallyString → Int
model_dump()Convert to dictionaryFor saving data
model_dump_json()Convert to JSONFor API responses
  • Pydantic is used in FastAPI, LangChain, and almost every production AI tool.
  • It catches bad data before it breaks your code.
  • If you want to become an AI engineer, Pydantic is a must-know library.

Coming Soon

This AI & ML module is currently under development. Stay tuned for the advanced premium curriculum!