Define the data you need as a Pydantic model. Instructor asks your LLM for those fields and validates the response. Install Instructor and set OPENAI_API_KEY to run this example.
import instructor
from pydantic import BaseModel
# Define what you want
class User(BaseModel):
name: str
age: int
# Extract it from natural language
client = instructor.from_provider("openai/gpt-6-luna")
user = client.chat.completions.create(
response_model=User,
reasoning_effort="none",
messages=[{"role": "user", "content": "John is 25 years old"}],
)
print(user) # User(name='John', age=25)
Use Instructor for fast extraction, reach for PydanticAI when you need agents. Instructor keeps schema-first flows simple and cheap. If your app needs richer agent runs, built-in observability, or shareable traces, try PydanticAI. PydanticAI is the official agent runtime from the Pydantic team, adding typed tools, replayable datasets, evals, and production dashboards while using the same Pydantic models. Dive into the to see how it extends Instructor-style workflows.
client = instructor.from_provider("openai/gpt-6-luna")
user = client.chat.completions.create(
response_model=User,
messages=[{"role": "user", "content": "..."}],
)
# That's it! user is validated and typed
Install in seconds
pip install instructor
Or with your package manager:
uv add instructor
poetry add instructor
Works with every major provider
Use the same code with any LLM provider:
import os
# OpenAI
client = instructor.from_provider("openai/gpt-6-luna")
# Anthropic
client = instructor.from_provider("anthropic/claude-haiku-5-5")
# Google
client = instructor.from_provider("google/gemini-3.8-flash")
# Ollama (local)
client = instructor.from_provider("ollama/llama3.2")
# With API keys directly (no environment variables needed)
client = instructor.from_provider(
"openai/gpt-6-luna", api_key=os.environ["OPENAI_API_KEY"]
)
client = instructor.from_provider(
"anthropic/claude-haiku-5-5", api_key=os.environ["ANTHROPIC_API_KEY"]
)
client = instructor.from_provider("groq/llama-3.1-8b-instant", api_key="gsk_...")
# All use the same API!
user = client.chat.completions.create(
response_model=User,
messages=[{"role": "user", "content": "..."}],
)
Production-ready features
Automatic retries
Failed validations are automatically retried with the error message:
from pydantic import BaseModel, field_validator
class User(BaseModel):
name: str
age: int
@field_validator('age')
def validate_age(cls, v):
if v < 0:
raise ValueError('Age must be positive')
return v
# Instructor automatically retries when validation fails
user = client.chat.completions.create(
response_model=User,
messages=[{"role": "user", "content": "..."}],
max_retries=3,
)
Streaming support
Stream partial objects as they're generated:
from instructor import Partial
for partial_user in client.chat.completions.create(
response_model=Partial[User],
messages=[{"role": "user", "content": "..."}],
stream=True,
):
print(partial_user)
# User(name=None, age=None)
# User(name="John", age=None)
# User(name="John", age=25)
Nested objects
Extract complex, nested data structures:
from typing import List
class Address(BaseModel):
street: str
city: str
country: str
class User(BaseModel):
name: str
age: int
addresses: List[Address]
# Instructor handles nested objects automatically
user = client.chat.completions.create(
response_model=User,
messages=[{"role": "user", "content": "..."}],
)
Used in production by
Trusted by over 100,000 developers and companies building AI applications:
3M+ monthly downloads
10K+ GitHub stars
1000+ community contributors
Companies using Instructor include teams at OpenAI, Google, Microsoft, AWS, and many YC startups.