The PHP AI SDK lets you request structured JSON responses from Groq models usingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/groq/llms.txt
Use this file to discover all available pages before exploring further.
->output(Schema::object(...)). Because most Groq-hosted models do not yet expose the json_schema response format natively, the SDK automatically adapts the request: it switches response_format to json_object and injects a system instruction — “Respond only with a valid JSON object that matches the requested schema.” — so you get reliable, schema-shaped output without any extra wiring.
How Structured Output Works
Use
Schema::object() to describe the shape you want back. Nest Schema::string(), Schema::integer(), and other scalar helpers inside the properties array, and call ->required() on any fields that must be present.use AiSdk\Schema;
$schema = Schema::object(
name: 'address',
properties: [
Schema::string(name: 'city')->required(),
Schema::string(name: 'country')->required(),
],
);
Chain
->output($schema) onto Generate::text() before calling ->run(). The SDK resolves capability support at build time and adapts the request body accordingly.use AiSdk\Generate;
use AiSdk\Groq;
use AiSdk\Schema;
$result = Generate::text()
->model(Groq::model('llama-3.1-8b-instant'))
->prompt('Extract the city and country from: Lahore, Pakistan.')
->output(Schema::object(
name: 'address',
properties: [
Schema::string(name: 'city')->required(),
Schema::string(name: 'country')->required(),
],
))
->run();
The Adapted Fallback in Practice
For Llama and other Groq-hosted models that do not supportjson_schema, the SDK silently rewrites the request body before it hits the wire:
response_format.typeis changed fromjson_schematojson_object.- The system message
"Respond only with a valid JSON object that matches the requested schema."is prepended (or appended to an existing system turn).
->output() API works for every model. The adaptation happens inside GroqTextModel::buildBody() when the resolved CapabilitySupport->state is Adapted.
The injected instruction is appended to your existing system prompt when one is present, so your own instructions are never overwritten.
Checking Capability Support
UseGroq::model('...')->capability(Capability::StructuredOutput) to inspect how a specific model handles structured output before you run a generation.
Inspect structured output capability
CapabilitySupport value object exposes two properties:
| Property | Type | Description |
|---|---|---|
$state | CapabilitySupportState | Supported, Adapted, or NotSupported |
$strategy | string | Human-readable explanation of the adaptation, if any |
Native vs. Adapted Support
Models with Native json_schema Support
The following Groq-hosted models receive your schema as a first-class json_schema response format — no adaptation, no injected instruction:
| Model ID | Notes |
|---|---|
openai/gpt-oss-20b | 131 072-token context |
openai/gpt-oss-120b | 131 072-token context |
moonshotai/kimi* | Matches all Kimi variant IDs |
Adapted state.
Complete Example
Structured output — full example