Asking an LLM to "reply in JSON" works most of the time. The rest of the time you get a friendly sentence before the JSON, a missing field, or a value your code doesn't expect, and your app crashes on json.loads .
Pydantic turns that "most of the time" into something you can rely on. Here's the pattern I use.
1. Define the output as a model
Write down exactly what a valid answer looks like, including the allowed values:
from typing import Literal
from pydantic import BaseModel , Field , ValidationError
class TicketTriage ( BaseModel ) : category : Literal [ "billing" , "bug" , "feature_request" , "other" ] priority : Literal [ "low" , "medium" , "high" ] summary : str = Field ( max_length = 200 )
Enter fullscreen mode
Exit fullscreen mode
This one class is now your documentation, your validator and your type hints.
2. Show the model the schema
Pydantic can generate a JSON Schema from the class, so the prompt and the validation can never drift apart:
import json
schema = json .…