AI Engineering › 13 of 14
Typed Data with Pydantic
Validate LLM outputs and API payloads.
python
from pydantic import BaseModel, Field
class Sentiment(BaseModel):
label: str = Field(pattern="^(pos|neg|neutral)$")
confidence: float = Field(ge=0, le=1)
result = Sentiment.model_validate_json(llm_output)
print(result.label, result.confidence)⚡ Quick check
- ✓ BaseModel for schemas
- ✓ model_validate_json()
- ✓ Field(ge=, le=) constraints