Home Knowledge Base Instructor

Instructor is a Python library that forces LLMs to return valid, validated Pydantic models by patching official provider SDKs — combining JSON mode, function calling, and automatic retry-with-error-feedback into a single decorator-driven interface — making structured LLM output as simple as defining a Python class and as reliable as a typed API endpoint.

What Is Instructor?

Why Instructor Matters

Core Usage Pattern

import instructor
from anthropic import Anthropic
from pydantic import BaseModel, Field

client = instructor.from_anthropic(Anthropic())

class Person(BaseModel):
    name: str = Field(description="Full name of the person")
    age: int = Field(ge=0, le=150, description="Age in years")
    occupation: str

person = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=512,
    messages=[{"role": "user", "content": "Extract: John Smith, 34, works as a software engineer"}],
    response_model=Person,
)
# person.name == "John Smith", person.age == 34, always a valid Person

Advanced Instructor Features

Nested Models:

class Address(BaseModel):
    street: str
    city: str
    country: str

class Company(BaseModel):
    name: str
    headquarters: Address   # Nested Pydantic model works automatically
    employees: list[Person] # List of models also works

Partial Streaming:

for partial_person in client.messages.create(..., stream=True, response_model=Iterable[Person]):
    print(partial_person)  # Progressive output as fields generate

Validation with Feedback: When the LLM outputs "age": "thirty-four", Pydantic raises ValidationError: age must be int. Instructor automatically sends: "The previous response had a validation error: age must be int. Please correct and retry." — the LLM self-corrects without developer intervention.

Instructor vs Alternatives

FeatureInstructorOutlinesGuidanceRaw JSON mode
Pydantic integrationNativeGoodLimitedManual
API model supportExcellentLimitedGoodFull
Retry on failureAutomaticN/AN/AManual
Learning curveVery lowLowMediumLow
StreamingYesNoLimitedManual
Validation feedbackYes (auto)NoNoNo

Common Use Cases

Instructor is the simplest path from Pydantic model to reliable structured LLM output — by leveraging the validation infrastructure Python developers already use daily, Instructor makes LLM-powered data extraction and classification as trustworthy and maintainable as any other typed function in a production codebase.

instructorstructuredpydantic

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.