Open Source Other Structured OutputPydanticPythonValidationJSON Mode

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.

jxnl/instructor 10.5k stars 870 forks MIT

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

Patches openai, anthropic, google-generativeai, and litellm clients
Returns validated Pydantic model instances — not dicts or strings
Automatic retry with validation error feedback to the model
Streaming support — stream partial Pydantic models as they generate
Async support — ainstructor for async client patching
Instructor Hub — community cookbook of prompting patterns
Compatible with complex nested Pydantic models

References & Links

Free Open Source · MIT
View on GitHub Visit Homepage

Project Info

Language Python
License MIT
Stars 10.5k ★
Forks 870
Category Other

Topics

Structured OutputPydanticPythonValidationJSON Mode

Ready to try Instructor?

Free and open source. Install in one command, works with Claude Code, Cursor, Windsurf, and more.

$pip install instructor
← Back to Marketplace