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 theOPENAI_API_KEYenvironment variable. You can also choose a provider explicitly withStructuredOutput::using('openai')or by passing a runtime configured withLLMConfig.
Building The Request
Thewith() method covers the common path. It accepts all the parameters you typically need
in a single call:
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 withScalar responses or any result where you know the expected PHP type,
you can skip get() and call a typed accessor directly:
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:[['role' => 'user', 'content' => 'Jason is 28 years old.']].
Structured-To-Structured Processing
Theinput 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:Using A Runtime
For applications that share provider configuration and behavior across many requests, create aStructuredOutputRuntime once and reuse it:
Streaming Support
Instructor supports streaming of partial results, allowing you to process data as it arrives from the model:StructuredOutputStream provides several iteration methods: