Skip to main content
JSON object mode instructs the model to return its response as a valid JSON object. This is useful when you need structured data that can be easily processed by your application, without defining a full schema.

Basic Usage

Use ResponseFormat::jsonObject() to request JSON output. The asJsonData() convenience method decodes the response into a PHP array:
The 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 dedicated withResponseFormat() method:

Getting JSON as a String

If you need the raw JSON string instead of a decoded array, use asJson():

Provider Support

Most major providers support native JSON object mode, including OpenAI, Groq, Fireworks, and others. Some providers (such as Anthropic) do not support responseFormat 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
If you need guaranteed field names, types, and required properties, consider JSON Schema mode instead.