Skip to main content
By default, Instructor deserializes LLM responses into PHP objects based on your response model class. The Output Format API lets you change this behavior while keeping the same schema definition. This decouples schema specification (what structure the LLM should produce) from output format (how you receive the result).

Available Output Formats

intoArray()

Returns extracted data as a plain associative array instead of an object.
This is useful for database insertion, JSON API responses, array manipulation, or when integrating with code that expects arrays.

intoInstanceOf()

Uses one class for the schema definition and a different class for the output object.
The LLM still sees all five fields from UserProfile, ensuring thorough extraction. The output is then hydrated into the simpler UserDTO. This is valuable when you want to separate API contracts from internal models, simplify complex extraction results, or decouple domain models from presentation layers.

intoObject()

Provides a custom object that controls its own deserialization from the extracted array. The object must implement the CanDeserializeSelf interface.
This is particularly useful with the built-in Scalar adapter for extracting single values.

Streaming with Output Formats

Output formats work seamlessly with streaming. During streaming, partial updates are always returned as objects (for validation and deduplication). The final result respects the output format you specified.

Comparison

Common Patterns

Conditional Deserialization

Inspect data before choosing the target class.

Multi-Layer Architecture

Use a rich domain model for extraction and a simplified DTO for your application layer.

Pluggable Extraction

Instructor uses a pluggable extraction pipeline to convert raw LLM responses into canonical arrays. You can customize this pipeline on the StructuredOutputRuntime to support non-standard response formats. See JSON Extraction for details on the extraction pipeline and how to write custom extractors.