Skip to main content
This guide walks you through installing Instructor and running your first extraction. For detailed configuration options, see Setup.

Installation

Install the package via Composer:
Instructor requires PHP 8.3 or later.

Your First Extraction

Step 1: Set Your API Key

Instructor needs credentials for the LLM provider you plan to use. The simplest approach is to export an environment variable before running your script:
In a real project, store API keys in a .env file or your framework’s secret manager. Never hard-code keys in source files.

Step 2: Define a Response Model

Create a PHP class with typed public properties. Instructor will generate a JSON Schema from this class and instruct the LLM to return data matching that shape:

Step 3: Run the Extraction

Use StructuredOutput to send a request and receive a typed result:
The get() method returns a fully hydrated City instance. Public typed properties define the schema that Instructor sends to the model.

Alternative API Styles

Instructor offers several ways to build the same request. Choose whichever reads best in your codebase.

Fluent Builder

Chain individual with* methods for maximum readability:

Compact with() Call

Pass everything as named arguments to a single with() call:

Explicit Provider Configuration

When you need full control over the provider and model, use LLMConfig directly:

Streaming Partial Updates

For long extractions or real-time UIs, stream partial updates as the LLM generates its response:

Adding Validation

Add Symfony Validator constraints to your response model. If the LLM returns data that fails validation, Instructor will automatically retry with the error details:
To enable retries, configure maxRetries on the runtime. See Validation for details.

Choosing the Right Entry Point

Next Steps

  • Setup — installation details and provider configuration
  • Usage — the full request-building API
  • Data Model — defining response model classes
  • Validation — validation and retry behavior
  • Modes — output modes (tool calls, JSON, JSON Schema)