Overview
TheresponseModel parameter tells Instructor the shape of the data you want back
from the LLM. Instructor translates it into a JSON Schema, uses that schema to
guide the model, and then deserializes the model’s output back into the requested
shape.
Supported Input Types
Class String
The most common approach. Pass a fully qualified class name and Instructor analyzes its properties, type hints, and doc comments to generate the schema:User::class gives your IDE full visibility for refactoring, autocompletion,
and static analysis.
Object Instance
Pass an object instance when you need to pre-populate default values on the response model:JSON Schema Array
Pass a raw JSON Schema array when you need full control over the schema definition:Important: Thex-php-classfield is required so Instructor knows which class to deserialize the response into. Without it, a dynamicStructureobject is used instead.
Helper Wrappers
The package ships with convenience wrappers for common patterns:Scalar— extract a single scalar value (string, int, float, bool, or enum)Sequence— extract a list of objects, with per-item streaming supportMaybe— extract a value that may or may not be present
Output Formats
By default, Instructor returns a typed PHP object. You can change the output format using fluent methods onStructuredOutput:
Note: Instructor always returns objects (or arrays when intoArray() is used).
It never returns raw arrays unless explicitly requested.
Custom Response Handling
You can customize how Instructor processes the response model at each stage by implementing one or more of the following contracts on your response model class:| Contract | Phase | Purpose |
|---|---|---|
CanProvideJsonSchema | Schema generation | Provide a raw JSON Schema array, bypassing class analysis |
CanProvideSchema | Schema generation | Provide a Schema object, bypassing class analysis |
CanDeserializeSelf | Deserialization | Custom deserialization from the extracted JSON data |
CanValidateSelf | Validation | Replace the default validation process entirely |
CanTransformSelf | Transformation | Transform the validated object before returning it to the caller |
Example Implementations
The built-inScalar and Sequence helpers are practical examples of this
customization pattern. They implement custom schema providers, deserialization,
validation, and transformation to support scalar values and ordered lists
through wrapper classes:
packages/instructor/src/Extras/Scalar/packages/instructor/src/Extras/Sequence/