pydantic
UtilitiesData validation
What is pydantic?
Pydantic is the most widely used Python data validation library. It uses Python type annotations to define schemas for data models, automatically validates incoming data, and raises descriptive errors when types or constraints are violated. Pydantic v2 is the foundation of FastAPI and is used across the Python ecosystem for config management, API schemas, and data contracts.
You can experiment with Pydantic models in PyRun without any local setup. Define your models, test edge cases, and explore validation behaviour directly in the browser. PyRun loads Pydantic via micropip, making it instantly available from your first import.
Code Example
Type-safe data validation with nested models.
Pydantic Data Models
Try in Editorfrom pydantic import BaseModel, ValidationError
from typing import List
class Address(BaseModel):
street: str
city: str
zip_code: str
class User(BaseModel):
id: int
name: str
age: int
address: Address
user = User(
id=1, name="Alice", age=28,
address=Address(street="123 Main St", city="Springfield", zip_code="62701")
)
print("Valid User:")
print(user.model_dump())
try:
bad = User(id="not-an-int", name="Bob", age=-5,
address={"street": "X", "city": "Y", "zip_code": "Z"})
except ValidationError as e:
print("\nValidation Error:")
print(e)Why run pydantic in PyRun?
- ✦ Zero setup — no pip install, no virtual environment, no Python download
- ✦ Instant results — powered by WebAssembly, runs locally in your browser
- ✦ Share your code — generate a link and anyone can run it instantly
- ✦ Works offline — after first load, PyRun runs without internet