Advertisement
Developer Tools & Cloud Infrastructure Sponsor Zone

Production Prompt Engineering & Structured Outputs: Instructor & Pydantic

By Elena Rostova Intermediate 17 min read Updated 2026-09-12

What You Will Master in This Tutorial

  • Use Pydantic v2 models to define strict JSON schemas for LLM tool calling.
  • Implement the Instructor library to enforce automatic retry validation loops.
  • Handle partial JSON streaming in frontend user interfaces.
  • Prevent prompt injection and type drift in production intelligence pipelines.

1. The Hallucination Problem in Unstructured Outputs

Building software on top of raw LLM text outputs leads to frequent parsing failures, broken JSON, and runtime crashes. Structured outputs guarantee that the model's response strictly adheres to a predefined JSON Schema, verified at the grammar and logit level.

PYTHON
import instructor
from pydantic import BaseModel, Field
from openai import OpenAI

# 1. Patch the OpenAI client with Instructor
client = instructor.from_openai(OpenAI())

# 2. Define Pydantic Schema with field-level validations
class UserProfileExtraction(BaseModel):
    name: str = Field(description="Full legal name of the user")
    email: str = Field(description="Primary verified email address")
    tech_stack: list[str] = Field(description="Programming languages and tools mentioned")
    years_experience: int = Field(ge=0, le=50, description="Calculated years of experience")

# 3. Call model with strict type enforcement
profile: UserProfileExtraction = client.chat.completions.create(
    model="gpt-4o-mini",
    response_model=UserProfileExtraction,
    max_retries=3,
    messages=[
        {"role": "user", "content": "Alex Johnson has been coding in Go and Rust for 8 years. Reach him at [email protected]."}
    ]
)

print(profile.name)             # 'Alex Johnson'
print(profile.tech_stack)       # ['Go', 'Rust']
print(profile.years_experience) # 8
Note: Key Mechanism: When a model output fails Pydantic validation, Instructor automatically passes the validation error back to the LLM to self-correct.
Advertisement
Cloud Infrastructure & High-Performance Dev Environments

Knowledge Check: Test Your Understanding

1. How does Instructor resolve validation errors when an LLM produces an invalid field?