RTX 3050 - Order Now
Home / Blog / Tutorials / Structured Output with Pydantic and LLMs
Tutorials

Structured Output with Pydantic and LLMs

Pydantic models as LLM output schemas — type-safe, validated, IDE-friendly. The Python pattern for production structured generation.

Table of Contents

  1. Pattern
  2. Setup
  3. Validation
  4. Verdict

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.

TL;DR

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=BaseModel directly
  • 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.

Need a Dedicated GPU Server?

Deploy from RTX 3050 to RTX 5090. Full root access, NVMe storage, 1Gbps — UK datacenter.

Browse GPU Servers

gigagpu

We benchmark, deploy, and optimise GPU infrastructure for AI workloads. All data in our guides comes from real-world testing on our UK-based dedicated GPU servers.

Ready to deploy your AI workload?

Dedicated GPU servers from our UK datacenter. NVMe storage, 1Gbps networking, full root access.

Browse GPU Servers Contact Sales

Have a question? Need help?