Instructor
Python library that patches any LLM client (OpenAI, Anthropic, Gemini) to return validated Pydantic models instead of raw JSON strings, with automatic retry on failure.
Install
$pip install instructor About
Instructor wraps the client.chat.completions.create() call with a response_model parameter. Pass any Pydantic class and Instructor handles telling the model the schema, parsing the response, validating it, and retrying with the error message if validation fails.
It works with tool calling (for OpenAI/Anthropic) or JSON mode depending on the provider, always returning a proper Python object you can use immediately.
Getting Started
pip install instructor anthropic
import instructor
import anthropic
from pydantic import BaseModel
client = instructor.from_anthropic(anthropic.Anthropic())
class User(BaseModel):
name: str
age: int
user = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Extract: John Doe is 29 years old"}],
response_model=User,
)
print(user) # User(name='John Doe', age=29)Features
References & Links
Project Info
Topics
Ready to try Instructor?
Free and open source. Install in one command, works with Claude Code, Cursor, Windsurf, and more.
$pip install instructor