Overview
Helps get structured output from LLMs.
Example
from pydantic import BaseModel, EmailStr
from typing import Literal, List
class CustomerQuery(BaseModel):
name: str
email: EmailStr
query: str
category: Literal['refund_request', 'information_request', 'other']
is_complaint: bool
tags: List[str]
Validation
valid_data = CustomerQuery.model_validate_json(json_output)
Common Errors
- JSON ValidationError: Extra text or malformed JSON
- Data ValidationError: Valid JSON but data doesn’t match model schema