Skip to main content
Structured LLM outputs validated with Pydantic.

Skill metadata

Reference: full SKILL.md

The following is the complete skill definition that Mibyan loads when this skill is triggered. This is what the agent sees as instructions when the skill is active.

Instructor: Structured LLM Outputs

When to Use This Skill

Use Instructor when you need to:
  • Extract structured data from LLM responses reliably
  • Validate outputs against Pydantic schemas automatically
  • Retry failed extractions with automatic error handling
  • Parse complex JSON with type safety and validation
  • Stream partial results for real-time processing
  • Support multiple LLM providers with consistent API
GitHub Stars: 15,000+ | Battle-tested: 100,000+ developers

Installation

Quick Start

Basic Example: Extract User Data

With OpenAI

Core Concepts

1. Response Models (Pydantic)

Response models define the structure and validation rules for LLM outputs.

Basic Model

Benefits:
  • Type safety with Python type hints
  • Automatic validation (word_count > 0)
  • Self-documenting with Field descriptions
  • IDE autocomplete support

Nested Models

Optional Fields

Enums for Constraints

2. Validation

Pydantic validates LLM outputs automatically. If validation fails, Instructor retries.

Built-in Validators

Custom Validators

Model-Level Validation

3. Automatic Retrying

Instructor retries automatically when validation fails, providing error feedback to the LLM.
How it works:
  1. LLM generates output
  2. Pydantic validates
  3. If invalid: Error message sent back to LLM
  4. LLM tries again with error feedback
  5. Repeats up to max_retries

4. Streaming

Stream partial results for real-time processing.

Streaming Partial Objects

Streaming Iterables

Provider Configuration

Anthropic Claude

OpenAI

Local Models (Ollama)

Common Patterns

Pattern 1: Data Extraction from Text

Pattern 2: Classification

Pattern 3: Multi-Entity Extraction

Pattern 4: Structured Analysis

Pattern 5: Batch Processing

Advanced Features

Union Types

Dynamic Models

Custom Modes

Context Management

Error Handling

Handling Validation Errors

Custom Error Messages

Best Practices

1. Clear Field Descriptions

2. Use Appropriate Validation

3. Provide Examples in Prompts

4. Use Enums for Fixed Categories

5. Handle Missing Data Gracefully

Comparison to Alternatives

When to choose Instructor:
  • Need structured, validated outputs
  • Want type safety and IDE support
  • Require automatic retries
  • Building data extraction systems
When to choose alternatives:
  • DSPy: Need prompt optimization
  • LangChain: Building complex chains
  • Manual: Simple, one-off extractions

Resources

See Also

  • references/validation.md - Advanced validation patterns
  • references/providers.md - Provider-specific configuration
  • references/examples.md - Real-world use cases