pydantic
Pydantic v2 data validation library. Covers BaseModel, field validators, custom types, serialization, and schema generation. Use when validating data structures, parsing configs, or defining typed data models in Python. USE WHEN: user mentions "pydantic", "BaseModel", "Field()", "model_validator", "field_validator", "Annotated", "model_dump", "model_validate", "pydantic settings", "pydantic v2" DO NOT USE FOR: TypeScript types/interfaces, SQLAlchemy ORM models, dataclasses
What this skill does
# Pydantic v2 Core Knowledge
## Installation
```bash
pip install pydantic>=2.0
pip install pydantic-settings # for settings management
```
## BaseModel Basics
```python
from pydantic import BaseModel, Field
from typing import Annotated
class Tag(BaseModel):
name: str
area: int
description: str = ""
active: bool = True
# Instantiate
tag = Tag(name="11301.FIC.001", area=11301, description="Flow controller")
tag = Tag.model_validate({"name": "11301.FIC.001", "area": 11301})
# Serialize
tag.model_dump() # -> dict
tag.model_dump(exclude_none=True) # skip None fields
tag.model_dump_json() # -> JSON string
tag.model_json_schema() # -> JSON Schema dict
```
## Field Configuration
```python
from pydantic import BaseModel, Field
from typing import Annotated
class MotorTag(BaseModel):
# Required field
tag: str = Field(..., min_length=1, max_length=50, pattern=r'^\d{5}\.\w+\.\w+$')
# With alias (for dict keys that differ from Python attr names)
node_name: str = Field(..., alias="NodeName")
# With default
power_kw: float = Field(default=0.0, ge=0.0, le=10000.0)
# Computed default
label: str = Field(default_factory=lambda: "")
# Metadata
area: int = Field(..., description="ISA-5.1 area code", examples=[11301, 11090])
class Config:
populate_by_name = True # allow both alias and field name
```
## Validators (v2 API)
```python
from pydantic import BaseModel, field_validator, model_validator
from typing import Self
class TagModel(BaseModel):
tag: str
area: int
description: str
# Field-level validator
@field_validator('tag')
@classmethod
def normalize_tag(cls, v: str) -> str:
v = v.strip().upper()
if not v:
raise ValueError('tag cannot be empty')
return v
# Validate multiple fields
@field_validator('area')
@classmethod
def valid_area(cls, v: int) -> int:
if v < 10000 or v > 99999:
raise ValueError(f'area must be 5-digit code, got {v}')
return v
# Cross-field validation (model-level)
@model_validator(mode='after')
def check_area_in_tag(self) -> Self:
if not self.tag.startswith(str(self.area)):
raise ValueError(f'tag {self.tag!r} does not start with area {self.area}')
return self
```
## Annotated Types (reusable constraints)
```python
from typing import Annotated
from pydantic import Field, BaseModel
# Define reusable types
AreaCode = Annotated[int, Field(ge=10000, le=99999, description="5-digit area code")]
TagName = Annotated[str, Field(min_length=1, max_length=50, pattern=r'^[\w.]+$')]
PositiveFloat = Annotated[float, Field(gt=0.0)]
class Equipment(BaseModel):
area: AreaCode
tag: TagName
power_kw: PositiveFloat
```
## Handling Optional and Union Types
```python
from pydantic import BaseModel
from typing import Optional
class Reading(BaseModel):
tag: str
value: float | None = None # None by default
unit: str | None = None
alarm_high: Optional[float] = None # equivalent to float | None
# v2: None is only allowed if explicitly typed as Optional/| None
```
## Parsing and Error Handling
```python
from pydantic import ValidationError
try:
tag = MotorTag(tag="", area=999)
except ValidationError as e:
print(e.error_count()) # number of errors
for error in e.errors():
print(error['loc']) # field path
print(error['msg']) # human-readable message
print(error['type']) # error type identifier
# Partial validation (collect all errors, not fail-fast)
# ValidationError already collects all field errors by default
```
## DataFrame Row Validation
```python
import pandas as pd
from pydantic import BaseModel, ValidationError
class Row(BaseModel):
tag: str
area: int
power_kw: float
def validate_df(df: pd.DataFrame) -> tuple[list[Row], list[dict]]:
valid, errors = [], []
for i, row in df.iterrows():
try:
valid.append(Row.model_validate(row.to_dict()))
except ValidationError as e:
errors.append({"row": i, "errors": e.errors()})
return valid, errors
```
## Nested Models
```python
class Signal(BaseModel):
name: str
data_type: str
direction: str # "IN" | "OUT"
class FunctionBlock(BaseModel):
name: str
library: str
signals: list[Signal] = []
def get_inputs(self) -> list[Signal]:
return [s for s in self.signals if s.direction == "IN"]
# Instantiate nested
fb = FunctionBlock(
name="IDF_1",
library="BST_LIB_EXT",
signals=[
Signal(name="AC", data_type="BOOL", direction="IN"),
Signal(name="RUN", data_type="BOOL", direction="OUT"),
]
)
```
## Pydantic Settings
```python
from pydantic_settings import BaseSettings, SettingsConfigDict
class AppSettings(BaseSettings):
model_config = SettingsConfigDict(
env_file=".env",
env_prefix="APP_",
case_sensitive=False,
)
anthropic_api_key: str
db_path: str = "data/project.db"
debug: bool = False
# Reads from env vars APP_ANTHROPIC_API_KEY, APP_DB_PATH, etc.
settings = AppSettings()
```
## Serialization Control
```python
from pydantic import BaseModel, field_serializer
from datetime import datetime
class Record(BaseModel):
tag: str
created_at: datetime
@field_serializer('created_at')
def serialize_dt(self, dt: datetime) -> str:
return dt.strftime('%Y-%m-%d %H:%M:%S')
model_config = {
'json_encoders': {datetime: lambda v: v.isoformat()}
}
r = Record(tag="FIC-001", created_at=datetime.now())
r.model_dump() # datetime object
r.model_dump(mode='json') # serialized via field_serializer
```
## Common Patterns
| Use Case | Pattern |
|----------|---------|
| Optional field | `field: str \| None = None` |
| List with min length | `tags: list[str] = Field(min_length=1)` |
| Enum field | `status: Literal["active", "inactive"]` |
| Coerce string to int | `model_config = ConfigDict(coerce_numbers_to_str=True)` |
| Allow extra fields | `model_config = ConfigDict(extra='allow')` |
| Forbid extra fields | `model_config = ConfigDict(extra='forbid')` |
| From ORM | `model_config = ConfigDict(from_attributes=True)` |
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.