Structured output instructs an OpenAI model to respond exclusively with valid JSON that conforms to a schema you define. Instead of parsing freeform prose, your application receives a predictable, machine-readable payload every time — making it ideal for data extraction, classification, slot-filling, form parsing, and any workflow where downstream code must consume the model’s output programmatically. The PHP AI SDK translatesDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/openai/llms.txt
Use this file to discover all available pages before exploring further.
Schema objects into the OpenAI json_schema response format and delivers the raw JSON string in $result->text, ready for json_decode.
Basic Example
Chain->output() onto Generate::text() and pass a Schema::object() describing the fields you want:
How It Works
When anOutput with kind = KIND_OBJECT and a Schema instance is present, ChatRequestBuilder::responseFormat() builds the following request body fragment:
strict: true flag is always set by the SDK. This activates OpenAI’s constrained decoding mode — the model is guaranteed to produce output that validates against the schema, and any field not declared in properties is rejected before the response is returned.
OpenAI’s
strict mode requires that all object properties have explicit types and that no additional properties are allowed. The SDK sets strict: true automatically whenever a Schema instance is used.Schema Building
Theaisdk/core package provides a fluent schema builder. The most common patterns for structured output are:
String field (required):
Additional schema types — integers, booleans, enums, arrays, and union types — are available from the
aisdk/core package. Refer to the core package documentation for the full Schema API.JSON Object Mode
If you need the model to return arbitrary JSON without enforcing a specific schema, passOutput::KIND_OBJECT without a Schema instance. The SDK sets response_format: {type: "json_object"} in this case:
Accessing the Response
The structured JSON is available as a plain string in$result->text. Decode it with json_decode to work with the data as a PHP array:
TextModelResponse fields — usage, finishReason, and providerMetadata['openai'] — are populated exactly as they are for plain text generation requests.