Skip to main content
The response model is the contract between your code and the LLM. It tells Instructor what schema to send to the model and how to deserialize the response back into a PHP object.

Plain PHP Classes

For most cases, a class with public typed properties is all you need:
Instructor reads the property types, builds a JSON Schema from them, and hydrates the response back into the class. Public properties are filled by the LLM; private and protected properties are left untouched with their default values.

Supported Response Model Shapes

Instructor accepts several forms as the responseModel parameter:

Type Hints

Use standard PHP type hints to specify the type of each field. Instructor supports all common types: string, int, float, bool, array, objects, and enums. Use nullable types to indicate that a field is optional:
Instructor only sets public fields. Private and protected fields are ignored unless the class defines matching setter methods or constructor parameters.

DocBlock Type Hints

When you cannot or prefer not to use PHP type hints, DocBlock comments work as well. This is particularly useful for typed arrays, since PHP does not support generic array type hints natively:

Typed Collections And Arrays

PHP does not support generics, so you need DocBlock comments to specify array element types. Instructor reads these annotations and includes them in the schema:
When you need a top-level list rather than an object with an array property, use the Sequence helper instead:

Nested Objects And Enums

Nested objects and backed enums are part of the normal path. If your class graph is simple and typed, it works out of the box:

Describing Your Model To The LLM

You can guide the model by adding descriptions and instructions to your classes and properties. Instructor includes these in the schema sent to the LLM.

PHP DocBlocks

DocBlock comments on classes and properties are automatically extracted:

Attributes

The #[Description] and #[Instructions] attributes provide a structured alternative to DocBlocks:
You can combine attributes and DocBlocks on the same class. Instructor merges them into a single description block.

Optional Data With Maybe

The Maybe helper wraps any response model to handle cases where the requested data might not be present in the input:
Maybe asks the model to set a hasValue boolean and, when the data is missing, to explain why in an error string. This is more reliable than using nullable types when you need to distinguish “data not found” from “data is null.”

Best Practices

  • Use public typed properties. They give Instructor the clearest possible schema.
  • Keep names descriptive. Property names like $customerEmail produce better results than $e.
  • Put validation close to the model. Use Symfony constraints or ValidationMixin directly on the response class.
  • Prefer small, focused models. A ContactInfo class with three fields extracts more reliably than a catch-all Document class with twenty.
  • Use enums for constrained values. Backed enums produce an enum constraint in the schema, which dramatically improves accuracy for categorical fields.