0Pricing
AI Agents · Lesson

Instructor / Outlines for Guaranteed Structure

Instructor (Python) and Outlines constrain decoding so the model literally cannot produce invalid JSON.

Two Major Libraries

Two leading approaches for guaranteed structured outputs:

  • Instructor (Jason Liu) — Pydantic wrapper around OpenAI/Anthropic/many providers
  • Outlines (.txt) — constrained decoding for OSS models

Instructor Basics

# pip install instructor openai
import instructor
from openai import OpenAI
from pydantic import BaseModel

client = instructor.from_openai(OpenAI())

class User(BaseModel):
    name: str
    age: int

user = client.chat.completions.create(
    model='gpt-4o-mini',
    response_model=User,
    messages=[{'role': 'user', 'content': 'Alice, 30'}]
)
print(user.name, user.age)

All lessons in this course

  1. JSON Mode and Tool-Call Outputs
  2. Pydantic Schema Validation
  3. Repair Loops for Malformed Output
  4. Instructor / Outlines for Guaranteed Structure
← Back to AI Agents