API & Backend

LLM Structured Outputs: Type-Safe JSON Generation with Pydantic v2 & Instructor

Guarantee 100% schema compliance from LLMs using Pydantic v2, JSON Schemas, and the Instructor library without retry overhead.

3 min

The Problem with Raw JSON Strings

Prompting LLMs to 'return JSON only' frequently fails in production due to markdown wrappers, trailing commas, or missing fields. Structured Outputs enforce the schema directly at the token decoding layer.

extractor.py
import instructor
from openai import OpenAI
from pydantic import BaseModel

class InvoiceExtraction(BaseModel):
    vendor: str
    total_amount: float
    currency: str
    items: list[str]

client = instructor.from_openai(OpenAI())

extracted = client.chat.completions.create(
    model="gpt-4o-mini",
    response_model=InvoiceExtraction,
    messages=[{"role": "user", "content": "Invoice from Acme Corp for $450.00: 2x Cloud Licenses"}]
)

print(f"Vendor: {extracted.vendor}, Total: {extracted.total_amount} {extracted.currency}")