Basic Usage
UseResponseFormat::jsonObject() to request JSON output. The asJsonData() convenience method decodes the response into a PHP array:
asJsonData() method only decodes the returned content. Validation and structure depend on the provider and your prompt — there are no schema guarantees with this mode.
Guiding the JSON Structure
For best results, include clear instructions about the expected JSON structure directly in your prompt. The model will follow your guidance, but without a schema there is no enforcement:Using the Fluent API
You can also set the response format with the dedicatedwithResponseFormat() method:
Getting JSON as a String
If you need the raw JSON string instead of a decoded array, useasJson():
Provider Support
Most major providers support native JSON object mode, including OpenAI, Groq, Fireworks, and others. Some providers (such as Anthropic) do not supportresponseFormat natively — for those, consider using tool calls to extract structured data, or use the Instructor layer above Polyglot for prompt-based fallback strategies.
You can query a driver’s capabilities programmatically through DriverCapabilities::supportsResponseFormatJsonObject().
When to Use JSON Object Mode
JSON object mode is ideal for:- Extracting structured data (lists, records, key-value pairs)
- API responses that need to be machine-readable
- Generating datasets or feeding data into downstream processing
- Cases where you want structured output but do not need strict schema validation