Skip to main content

Basic Usage

Instructor extracts structured data from text using LLM inference. You define a PHP class that describes the shape of the data you want, and Instructor takes care of building the prompt, calling the model, and deserializing the response into a typed object.
By default, Instructor looks for the OPENAI_API_KEY environment variable. You can also choose a provider explicitly with StructuredOutput::using('openai') or by passing a runtime configured with LLMConfig.

Building The Request

The with() method covers the common path. It accepts all the parameters you typically need in a single call:
When you prefer a more explicit, step-by-step style, use the fluent API:
Both approaches produce identical requests. Use whichever reads better in your code.

Request Methods

Reading The Result

Instructor provides several ways to consume the response depending on your needs.

get() - The Parsed Value

The most common method. Returns the deserialized, validated object (or scalar when using the Scalar adapter):

response() - The Full Response Envelope

Returns a StructuredOutputResponse that wraps both the parsed value and the raw LLM response, giving you access to usage metadata, finish reason, and more:

inferenceResponse() - The Underlying Inference Response

Returns the low-level InferenceResponse from the Polyglot layer, useful when you need direct access to HTTP response data or provider-specific details:

stream() - Streaming Partial Results

Returns a StructuredOutputStream for real-time processing. Streaming is enabled automatically when you call stream():

create() - Lazy Execution

Returns a PendingStructuredOutput handle without triggering the LLM call. Nothing executes until you read from it:
PendingStructuredOutput exposes the same reading methods as StructuredOutput plus a few utility helpers:

Typed Convenience Methods

When working with Scalar responses or any result where you know the expected PHP type, you can skip get() and call a typed accessor directly:
Available typed methods: getString(), getInt(), getFloat(), getBoolean(), getObject(), getArray().

String As Input

You can pass a plain string anywhere messages are expected. Instructor wraps it into a user message automatically:
This is equivalent to passing [['role' => 'user', 'content' => 'Jason is 28 years old.']].

Structured-To-Structured Processing

The input parameter accepts objects, arrays, or strings. This lets you transform one structured representation into another:

Output Formats

By default, Instructor returns an instance of your response model class. You can change this with the output format methods:
Three output format methods are available:

Using A Runtime

For applications that share provider configuration and behavior across many requests, create a StructuredOutputRuntime once and reuse it:
The runtime holds settings like retries, output mode, validators, transformers, and deserializers. Individual requests stay lightweight and focused on content. You can also use the static shorthand to pick a provider without building a full runtime:

Streaming Support

Instructor supports streaming of partial results, allowing you to process data as it arrives from the model:
The StructuredOutputStream provides several iteration methods: