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
UseStructuredOutput to send a request and receive a typed result:
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 individualwith* 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, useLLMConfig 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:maxRetries on the runtime. See Validation for details.
Choosing the Right Entry Point
| Scenario | Entry Point |
|---|---|
| Quick extraction with a preset | StructuredOutput::using('openai') |
| Explicit provider/model control | StructuredOutput::fromConfig(LLMConfig::fromDsn(...)) |
| Retries, events, or custom pipeline | StructuredOutputRuntime |
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)