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
- JSON Mode and Tool-Call Outputs
- Pydantic Schema Validation
- Repair Loops for Malformed Output
- Instructor / Outlines for Guaranteed Structure