Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/phpaisdk/google/llms.txt

Use this file to discover all available pages before exploring further.

Structured output forces Gemini to return valid JSON instead of free-form prose. The aisdk/google provider achieves this by injecting response_mime_type and an optional response_schema into generation_config on every request that carries an Output object — no prompt engineering required.

How it works

When you attach an Output object to your request, GoogleRequestBuilder::responseFormat() is called internally and the result is merged into generation_config before the request is sent:
  • JSON mode (no schema): adds response_mime_type: "application/json".
  • Object schema mode: adds both response_mime_type: "application/json" and response_schema containing the JSON Schema derived from your PHP schema definition.
// Simplified view of what GoogleRequestBuilder does internally
private static function responseFormat(Output $output): array
{
    if ($output->kind === Output::KIND_OBJECT && $output->schema !== null) {
        return [
            'response_mime_type' => 'application/json',
            'response_schema'    => $output->schema->jsonSchema(),
        ];
    }

    if ($output->kind === Output::KIND_OBJECT) {
        return ['response_mime_type' => 'application/json'];
    }

    return [];
}

JSON mode (no schema)

Use Output::json() when you want Gemini to return valid JSON but you are not enforcing a specific structure. This is useful for open-ended extraction tasks where the shape varies.
use AiSdk\Generate;
use AiSdk\Google;
use AiSdk\Outputs\Output;

Google::create(['apiKey' => env('GOOGLE_GENERATIVE_AI_API_KEY')]);

$result = Generate::text('List the three primary colours as a JSON array.')
    ->model(Google::model('gemini-3.5-flash'))
    ->output(Output::json())
    ->run();

$data = json_decode($result->text, true);
// ["red", "yellow", "blue"]

Object schema mode

Use Output::object($schema) to pin the response to a specific shape. The SDK serialises your PHP schema to a JSON Schema object and passes it to Gemini via response_schema.
use AiSdk\Generate;
use AiSdk\Google;
use AiSdk\Outputs\Output;
use AiSdk\Schema\Schema;

Google::create(['apiKey' => env('GOOGLE_GENERATIVE_AI_API_KEY')]);

$schema = Schema::object(
    description: 'A product record extracted from the review.',
    properties: [
        Schema::string('name',     'Product name'),
        Schema::string('brand',    'Manufacturer brand'),
        Schema::number('rating',   'Numeric rating 1–5'),
        Schema::string('sentiment','Overall sentiment: positive, neutral, or negative'),
    ],
    required: ['name', 'brand', 'rating', 'sentiment'],
);

$result = Generate::text(
        'Review: "Acme Pro headphones sound amazing, crystal clear, 5 stars."'
    )
    ->model(Google::model('gemini-3.5-flash'))
    ->output(Output::object($schema))
    ->run();

$product = json_decode($result->text, true);
// [
//   "name"      => "Acme Pro",
//   "brand"     => "Acme",
//   "rating"    => 5,
//   "sentiment" => "positive",
// ]

Parsing the result

$result->text always contains the raw JSON string. Decode it with json_decode():
$data = json_decode($result->text, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    throw new \RuntimeException('Gemini returned invalid JSON: '.json_last_error_msg());
}

echo $data['name'];    // "Acme Pro"
echo $data['rating'];  // 5
Combine structured output with ->instructions() to further constrain the response. For example, you can instruct the model to use specific enum values for fields like sentiment, keep numeric values within a range, or omit optional fields when data is unavailable — all without modifying the schema.
$result = Generate::text($reviewText)
    ->model(Google::model('gemini-3.5-flash'))
    ->instructions('Only populate "sentiment" with exactly one of: positive, neutral, negative.')
    ->output(Output::object($schema))
    ->run();
Structured output requires a model that advertises the structured_output capability. All gemini-1.5*, gemini-2.0*, gemini-2.5*, and gemini-3* text models support it. Image-generation models do not. See the models capabilities reference for the full list.

Build docs developers (and LLMs) love