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
| Method | Purpose |
|---|---|
withMessages(...) | Set the chat messages |
withInput(...) | Set input from a string, array, or object (converted to messages) |
withResponseModel(...) | Set the response model (class string, instance, or schema array) |
withResponseClass(...) | Set the response model from a class name |
withResponseObject(...) | Set the response model from an object instance |
withResponseJsonSchema(...) | Set the response model from a JSON Schema array |
withSystem(...) | Set the system prompt (string|\Stringable) |
withPrompt(...) | Set additional prompt text (string|\Stringable) |
withExamples(...) | Provide few-shot examples |
withModel(...) | Override the model name |
withOptions(...) | Pass provider-specific options |
withOption(...) | Set a single provider option |
withStreaming(...) | Enable or disable streaming |
withCachedContext(...) | Set cached context for providers that support prompt caching |
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:
| Method | Return type |
|---|---|
get() | The parsed value |
response() | StructuredOutputResponse |
inferenceResponse() | InferenceResponse |
stream() | StructuredOutputStream |
toJson() | JSON string of the extracted data |
toArray() | Associative array of the extracted data |
toJsonObject() | Json object |
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:| Method | Effect |
|---|---|
intoArray() | Skip deserialization, return a raw associative array |
intoInstanceOf($class) | Use the schema from the response model but hydrate into a different class |
intoObject($obj) | Pass a self-deserializing object that implements CanDeserializeSelf |
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:
| Method | Yields |
|---|---|
partials() | Partially filled objects as they arrive |
sequence() | Completed items when using Sequence as the response model |
responses() | Full StructuredOutputResponse snapshots |
finalValue() | Drains the stream and returns the final parsed value |
finalResponse() | Drains the stream and returns the final StructuredOutputResponse |