The Problem
You’ve integrated an LLM into your PHP application. Now what?The Solution
Instructor gives you structured, validated, type-safe outputs:How It Works
Instructor uses a three-step process:- Define - You create a PHP class with typed properties
- Extract - Instructor sends your schema to the LLM with optimized prompts
- Validate - Results are validated; failures trigger automatic retry with feedback
Key Benefits
1. Type Safety
Your IDE understands the response. Autocomplete works. Static analysis catches errors.2. Automatic Validation
Use Symfony Validator constraints. Invalid responses trigger automatic retry:3. Self-Correcting Retries
LLMs make mistakes. Instructor handles this gracefully:4. Provider Independence
Write once, run anywhere. Switch LLM providers without changing your code:5. Multiple Output Modes
Works with any model capability:| Mode | Best For | How It Works |
|---|---|---|
Tools | OpenAI, Claude | Uses function/tool calling |
JsonSchema | GPT-4, newer models | Strict JSON Schema mode |
Json | Most models | JSON response format |
MdJson | Any model | Prompting-based extraction |
6. Streaming Support
Get partial results as they arrive:7. Multimodal Inputs
Process text, images, and chat conversations with the same API:Comparison
Without Instructor
With Instructor
Why Not Just Use JSON Mode / JSON Schema?
“But OpenAI hasresponse_format: json_object and strict JSON Schema mode now. Why do I need Instructor?”
Good question. Here’s what you’re still stuck with:
1. Provider Inconsistency
Every provider does it differently:| Provider | JSON Mode | JSON Schema | Tool Calling |
|---|---|---|---|
| OpenAI | response_format: {type: "json_object"} | response_format: {type: "json_schema", ...} | Yes |
| Anthropic | ❌ No native support | ❌ No native support | Yes (different format) |
| Gemini | Different API entirely | Different API entirely | Yes (different format) |
| Mistral | Partial support | No | Yes |
| Ollama | Model-dependent | Model-dependent | Model-dependent |
2. No Object Hydration
JSON Schema gives you… JSON. Not objects.3. Schema Definition Hell
JSON Schema is verbose and lives separately from your code:4. No Validation Beyond Types
JSON Schema validates structure, not business logic:5. No Retry Mechanism
JSON Schema mode fails silently or throws. You handle recovery:6. No Streaming Support for Structured Data
JSON Schema mode gives you complete-or-nothing:7. Anthropic Doesn’t Have JSON Mode
Claude is one of the best models, but Anthropic has no native JSON mode:8. The Real-World Comparison
| Capability | Raw JSON/JSON Schema | Instructor |
|---|---|---|
| Works with all providers | ❌ Different APIs | ✅ Unified |
| Object hydration | ❌ Manual | ✅ Automatic |
| Nested objects | ❌ Manual recursion | ✅ Automatic |
| Business validation | ❌ None | ✅ Full |
| Retry on failure | ❌ Manual | ✅ Automatic |
| Error feedback to LLM | ❌ None | ✅ Built-in |
| Streaming partials | ❌ Not possible | ✅ Supported |
| Type safety in IDE | ❌ None | ✅ Full |
| Schema = Code | ❌ Separate | ✅ Same file |
| Works with Claude | ❌ No JSON mode | ✅ Yes |
The Bottom Line
JSON Schema mode is a step forward, but it’s a low-level primitive. You still need to:- Write provider-specific code
- Manually deserialize to objects
- Implement your own validation
- Build your own retry logic
- Handle streaming yourself
- Maintain schemas separate from code
->get().
When to Use Instructor
Great for:- Extracting structured data from unstructured text
- Building forms that accept natural language
- Processing documents (invoices, resumes, contracts)
- Content classification and tagging
- Data transformation pipelines
- Any task requiring reliable LLM output structure
- Open-ended creative writing
- Tasks where free-form text is the desired output
- Simple completions without structure requirements
The Instructor Family
Instructor exists in multiple languages with consistent APIs:| Language | Repository |
|---|---|
| PHP (this) | cognesy/instructor-php |
| Python (original) | jxnl/instructor |
| JavaScript | instructor-ai/instructor-js |
| Elixir | instructor-ai/instructor-ex |
| Ruby | instructor-ai/instructor-rb |
Ready to get started? Jump to the Getting Started Guide or explore the Cookbook for practical examples.