instructor
Structured outputs with Instructor. Extract typed data from LLMs using Pydantic models and validation. Use for data extraction, structured generation, and…
от majiayu000★ 31 звезда на GitHub
Установить instructor в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
Instructor
Expert guidance for structured LLM outputs with Pydantic validation.
Triggers
Use this skill when:
- Extracting structured data from LLM responses
- Building type-safe LLM applications
- Validating LLM outputs with Pydantic
- Implementing data extraction pipelines
- Working with structured generation
- Keywords: instructor, structured output, pydantic, data extraction, validation, typed responses
Installation
pip install instructor
Quick Start
import instructor
from pydantic import BaseModel
from openai import OpenAI
# Patch OpenAI client
client = instructor.from_openai(OpenAI())
class User(BaseModel):
name: str
age: int
# Extract structured data
user = client.chat.completions.create(
model="gpt-4o",
response_model=User,
messages=[
{"role": "user", "content": "John is 25 years old"}
]
)
print(user) # User(name='John', age=25)
Pydantic Models
Basic Models
from pydantic import BaseModel, Field
from typing import Optional, List
from enum import Enum
class Priority(str, Enum):
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
class Task(BaseModel):
title: str = Field(description="Task title")
description: str = Field(description="Detailed description")
priority: Priority = Field(description="Task priority level")
due_date: Optional[str] = Field(None, description="Due date in YYYY-MM-DD format")
tags: List[str] = Field(default_factory=list, description="Related tags")
task = client.chat.completions.create(
model="gpt-4o",
response_model=Task,
messages=[
{"role": "user", "content": "Create a task for reviewing the Q4 report, high priority, due next Friday"}
]
)
Nested Models
from pydantic import BaseModel
from typing import List
class Address(BaseModel):
street: str
city: str
country: str
postal_code: str
class Person(BaseModel):
name: str
email: str
address: Address
phone_numbers: List[str]
person = client.chat.completions.create(
model="gpt-4o",
response_model=Person,
messages=[
{"role": "user", "content": """
Extract: John Smith, [email protected], lives at 123 Main St,
New York, USA 10001. Phone: 555-1234, 555-5678
"""}
]
)
Validation
Field Validators
from pydantic import BaseModel, field_validator
from typing import List
class SearchQuery(BaseModel):
query: str
filters: List[str]
@field_validator('query')
@classmethod
def query_not_empty(cls, v):
if not v.strip():
raise ValueError("Query cannot be empty")
return v.strip()
@field_validator('filters')
@classmethod
def validate_filters(cls, v):
valid = ["date", "author", "category"]
for f in v:
if f not in valid:
raise ValueError(f"Invalid filter: {f}")
return v
Model Validators
from pydantic import BaseModel, model_validator
class DateRange(BaseModel):
start_date: str
end_date: str
@model_validator(mode='after')
def validate_dates(self):
if self.start_date > self.end_date:
raise ValueError("Start date must be before end date")
return self
Retries
from instructor import from_openai
from tenacity import retry, stop_after_attempt
client = from_openai(
OpenAI(),
mode=instructor.Mode.TOOLS
)
# Automatic retries on validation failure
user = client.chat.completions.create(
model="gpt-4o",
response_model=User,
max_retries=3,
messages=[{"role": "user", "content": "Extract user info"}]
)
Streaming
from instructor import from_openai, Partial
from pydantic import BaseModel
from typing import List
class Report(BaseModel):
title: str
sections: List[str]
summary: str
client = from_openai(OpenAI())
# Stream partial results
for partial in client.chat.completions.create_partial(
model="gpt-4o",
response_model=Report,
messages=[{"role": "user", "content": "Write a report on AI trends"}],
):
print(partial) # Partial[Report] with available fields
Iterable Extraction
from instructor import from_openai
from pydantic import BaseModel
from typing import Iterable
class Product(BaseModel):
name: str
price: float
category: str
client = from_openai(OpenAI())
products: Iterable[Product] = client.chat.completions.create_iterable(
model="gpt-4o",
response_model=Product,
messages=[
{"role": "user", "content": """
Extract products:
- iPhone 15 Pro: $999, Electronics
- Nike Air Max: $150, Footwear
- MacBook Pro: $2499, Electronics
"""}
]
)
for product in products:
print(product)
Multiple Providers
Anthropic
import instructor
from anthropic import Anthropic
client = instructor.from_anthropic(Anthropic())
user = cl
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
- metadata.json
FAQ
Что делает скилл instructor?
Structured outputs with Instructor. Extract typed data from LLMs using Pydantic models and validation. Use for data extraction, structured generation, and type-safe LLM responses.
Как установить скилл instructor?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл instructor запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
skill-creator
AnthropicCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
discernment-nudge
AnthropicAfter you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
data-consolidate
AnthropicJoins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
inspection-to-capa
AnthropicRuns the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…