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.

The aisdk/google package is the official Google Gemini provider for the PHP AI SDK. It translates the SDK’s portable Generate::text() builder into the Gemini Responses API, handles authentication with your API key, and maps the raw JSON reply back to a consistent TextModelResponse object — so your application code stays the same regardless of which model you choose.

Basic Example

Pass a model created with Google::model() and a prompt string to produce a synchronous text response. The call blocks until the full response is received and returns a TextModelResponse.
use AiSdk\Generate;
use AiSdk\Google;

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

$result = Generate::text()
    ->model(Google::model('gemini-3.5-flash'))
    ->prompt('Explain closures in PHP in two sentences.')
    ->run();

echo $result->text;
You can also pass the prompt directly to Generate::text() as a shorthand:
$result = Generate::text('Explain closures in PHP in two sentences.')
    ->model(Google::model('gemini-3.5-flash'))
    ->run();

echo $result->text;

System Instructions

Use ->instructions() to supply a system-level instruction that shapes the model’s behaviour for the entire conversation. The string is sent as the system_instruction field in the request body and is applied before any user turn.
$result = Generate::text()
    ->model(Google::model('gemini-3.5-flash'))
    ->instructions('You are a concise technical writer. Always respond in plain text without markdown.')
    ->prompt('What is dependency injection?')
    ->run();

echo $result->text;

Generation Parameters

Three portable parameters map directly into Gemini’s generation_config object:
MethodRequest fieldDefault
->maxTokens(int)generation_config.max_output_tokens1024
->temperature(float)generation_config.temperatureprovider default
->topP(float)generation_config.top_pprovider default
All three can be chained in any order:
$result = Generate::text()
    ->model(Google::model('gemini-3.5-flash'))
    ->prompt('Write a haiku about refactoring.')
    ->maxTokens(256)
    ->temperature(0.7)
    ->topP(0.9)
    ->run();

echo $result->text;
Lower temperature values (e.g. 0.2) produce more deterministic output. Higher values (e.g. 1.0) increase creativity and variation. topP nucleus-sampling trims the probability distribution to the top-P cumulative mass before sampling.

Reading the Response

->run() returns a TextModelResponse with the following properties:

Text output

echo $result->text; // The primary generated text string

Token usage

$usage = $result->usage;

echo $usage->inputTokens;   // Tokens in the prompt / context
echo $usage->outputTokens;  // Tokens generated (includes reasoning tokens)
echo $usage->totalTokens;   // Total token count when reported by Gemini
For thinking models (e.g. gemini-3.5-flash with reasoning enabled), the SDK tracks reasoning tokens separately:
echo $usage->reasoningTokens; // Non-null for thinking models; null otherwise
When a cached context is active, the number of tokens that were served from cache is available on:
echo $usage->cachedInputTokens; // Non-null when cached content was used; null otherwise

Provider metadata

The raw Gemini response fields id, model, finish_reason, usage_metadata, and prompt_feedback are forwarded verbatim under the google key:
$meta = $result->providerMetadata['google'];

echo $meta['id'];           // Interaction ID assigned by the API
echo $meta['model'];        // Model name echoed back by Gemini
echo $meta['finish_reason']; // Raw finish reason string from Gemini

Finish reason

$result->finishReason is a FinishReason enum value normalised from whatever Gemini returns:
Gemini valueSDK enum
stop, stopped, end_turnFinishReason::Stop
max_tokens, max_output_tokens, lengthFinishReason::Length
safety, content_filter, blockedFinishReason::ContentFilter
Any tool-call responseFinishReason::ToolCalls
use AiSdk\FinishReason;

if ($result->finishReason === FinishReason::Length) {
    // Response was truncated — consider increasing maxTokens
}

Multi-Turn Conversations

For conversational applications, build a messages array and pass it with ->messages(). Each entry uses a Message object with a role of user or assistant.
use AiSdk\Generate;
use AiSdk\Google;
use AiSdk\Message;

$messages = [
    Message::user('What is the capital of France?'),
    Message::assistant('The capital of France is Paris.'),
    Message::user('What is the population of that city?'),
];

$result = Generate::text()
    ->model(Google::model('gemini-3.5-flash'))
    ->messages($messages)
    ->run();

echo $result->text;
The SDK serialises multi-turn histories into an array of typed steps (user_input, model_response, tool_result) before sending. Single-turn requests with one plain text user message are optimised to a bare string in the input field.
The SDK routes all text generation requests to /interactions, not to the native Gemini generateContent endpoint. The default base URL is https://generativelanguage.googleapis.com/v1beta, making the full path https://generativelanguage.googleapis.com/v1beta/interactions. You can override baseUrl in Google::create() if you proxy requests through your own gateway.

Build docs developers (and LLMs) love