Table of Contents
For Python applications calling LLMs for structured output, Pydantic is the right schema layer. Type-safe, validates server responses, plays well with FastAPI / agentic frameworks. Combined with vLLM's guided decoding, you get end-to-end type-safe LLM output.
Define output as Pydantic model; convert to JSON schema; pass via vLLM response_format. LLM output is guaranteed valid against schema; Pydantic parses + validates server-side. Type-safe across the boundary. Standard pattern in Instructor / outlines / langchain-pydantic.
Pattern
from pydantic import BaseModel, Field
from openai import OpenAI
class Invoice(BaseModel):
vendor: str
invoice_date: str = Field(..., description="ISO date")
total: float
currency: str = Field(..., pattern="^(GBP|USD|EUR)$")
line_items: list[dict] = Field(default_factory=list)
client = OpenAI(base_url="http://localhost:8000/v1", api_key="dummy")
resp = client.beta.chat.completions.parse(
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
messages=[
{"role": "system", "content": "Extract invoice data."},
{"role": "user", "content": invoice_text},
],
response_format=Invoice, # auto-generates JSON schema
)
invoice: Invoice = resp.choices[0].message.parsed
Setup
- Pydantic v2 (use
BaseModel; v1 is legacy) - OpenAI Python SDK 1.40+ supports
response_format=BaseModeldirectly - vLLM 0.6+ accepts JSON schemas via
response_format - For older OpenAI SDK: convert via
model_json_schema()manually
Validation
Pydantic validates the server response on parse. Validation failures (extremely rare with guided decoding) raise ValidationError — treat as a logic bug, not a fallback path. With guided decoding active, output is valid by construction.
Use Pydantic Field validators for runtime constraints beyond JSON Schema:
- Custom regex patterns
- Numeric bounds
- Enum values
- Cross-field consistency checks
Verdict
For Python production LLM apps with structured outputs, Pydantic + vLLM guided decoding + OpenAI SDK's native Pydantic support is the right stack. Type-safe end-to-end, IDE-friendly, well-tested ecosystem. The standard pattern in 2026 production Python AI apps.
Bottom line
Pydantic + guided decoding for type safety. See guided decoding.