responseModel allows you to specify shape of the response you expect from LLM .
Instructor translates the responseModel parameter into actual schema based on the type and value of the parameter.
Handling string $responseModel value
Ifstring value is provided, it is used as a name of the class of the response model.
Instructor checks if the class exists and analyzes the class & properties type information & doc comments to generate a schema needed to specify LLM response constraints.
The best way to provide the name of the response model class is to use NameOfTheClass::class, making it easy for IDE to check the type, handle refactorings, etc.
Handling object $responseModel value
Ifobject value is provided, it is considered an instance of the response model. Instructor checks the class of the instance, then analyzes it and its property type data to specify LLM response constraints.
Handling array $responseModel value
Ifarray value is provided, it is considered a raw JSON Schema, therefore allowing Instructor to use it directly in LLM requests (after wrapping in appropriate context - e.g. function call).
Instructor requires information on the class of each nested object in your JSON Schema, so it can correctly deserialize the data into appropriate type.
This information is available to Instructor when you are passing $responseModel as a class name or an instance, but it is missing from raw JSON Schema. Lack of the information on target class makes it impossible for Instructor to deserialize the data into appropriate, expected type.
Current design uses JSON Schema $comment field (or x-php-class field) on property to overcome this information gap. Instructor expects developer to use this field to provide fully qualified name of the target class to be used to deserialize property data of object or enum type.
Important: Arrays as Schema INPUT vs OUTPUT
Arrays as Schema INPUT ✅ You can provide an array asresponseModel to specify the JSON Schema:
x-php-class field is REQUIRED for InstructorPHP to know which
class to deserialize the data into.
Arrays as OUTPUT ❌
InstructorPHP does NOT return raw arrays. The output is ALWAYS an object:
intoArray() output format:
Custom response handling strategy
Instructor allows you to customize processing of$responseModel value also by looking at the interfaces the class or instance implements:
CanProvideJsonSchema- implement to be able to provide raw JSON Schema (as an array) of the response model, overriding the default approach of Instructor, which is analyzing $responseModel value class information,CanProvideSchema- implement to be able to provideSchemaobject of the response model, overriding class analysis stage; can be useful in building object wrappers (see:Sequenceclass),CanDeserializeSelf- implement to customize the way the response from LLM is deserialized from JSON into PHP object,CanValidateSelf- implement to customize the way the deserialized object is validated - it fully replaces the default validation process for given response model,CanTransformSelf- implement to transform the validated object into any target value that will be then passed back to the caller (e.g. unwrap simple type from a class to scalar value)
CanProvideJsonSchema- executed during the schema generation phase,CanDeserializeSelf- executed during the deserialization phase,CanValidateSelf- executed during the validation phase,CanTransformSelf- executed during the transformation phase.
Example implementations
For a practical example of using those contracts to customize Instructor processing flow see:- src/Extras/Scalar/
- src/Extras/Sequence/
- custom schema provider,
- deserialization,
- validation
- and transformation