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.

GoogleTextModel is the concrete class that handles all text generation and streaming interactions for a specific Gemini model. It implements TextModelInterface from the PHP AI SDK core and is the object you receive when you call Google::model(). You rarely construct it directly — the Google facade or GoogleProvider does that for you — but understanding its contract is useful when you need to call generate() or stream() directly, inspect capabilities, or integrate with the model registry.

Namespace

AiSdk\Google\Models\GoogleTextModel

Constructor

public function __construct(
    string $modelId,
    GoogleOptions $options,
    ?ModelRegistry $registry = null,
)
modelId
string
required
The Gemini model identifier string, for example 'gemini-2.5-pro' or 'gemini-2.0-flash'. This value is passed verbatim to the API request and used for catalog lookups.
options
GoogleOptions
required
A configured GoogleOptions instance carrying the API key, base URL, and any SDK-level settings. The authHeaders() method on this object is called for every request.
registry
ModelRegistry|null
An optional ModelRegistry that can override built-in capability definitions. When provided, capabilities() and capability() consult the registry before falling back to the built-in ModelCatalog.

Methods

provider(): string

Returns the provider name string used throughout the SDK.
public function provider(): string
Returns 'google' — the constant defined on GoogleOptions::PROVIDER_NAME.

modelId(): string

Returns the model identifier that was passed to the constructor.
public function modelId(): string
Returns the $modelId string, for example 'gemini-2.5-pro'.

capabilities(): array

Resolves the full list of capabilities for this model. The resolution order is:
  1. If a ModelRegistry was injected and has a definition for this provider + model ID, capabilities are sourced from that definition.
  2. Otherwise, the built-in ModelCatalog is loaded from resources/models.json and queried by model ID.
/** @return array<int, Capability> */
public function capabilities(): array
Returns an array<int, Capability> of Capability enum values such as Capability::TextGeneration, Capability::Vision, Capability::Reasoning, etc.

capability(Capability $capability): CapabilitySupport

Returns the support status for a single capability. The resolution order mirrors capabilities() but adds a special fallback: if the model is completely unknown to both the registry and catalog, and the queried capability is Capability::TextGeneration, the method returns CapabilitySupport::supported($capability, 'unknown-model-fallback') rather than a “not supported” result. This prevents unknown model IDs (for example, newly released models not yet in the catalog) from being incorrectly rejected.
public function capability(Capability $capability): CapabilitySupport
capability
Capability
required
The Capability enum case to query, for example Capability::TextGeneration or Capability::Vision.
Returns a CapabilitySupport value object describing whether the capability is supported and, if so, under what constraints.

generate(TextModelRequest $request): TextModelResponse

Performs a synchronous, non-streaming text generation request against the Gemini API.
public function generate(TextModelRequest $request): TextModelResponse
request
TextModelRequest
required
A fully constructed TextModelRequest carrying messages, system prompt, generation parameters, tools, and any provider-specific options under the 'google' key.
Returns a TextModelResponse containing response parts, finish reason, token usage, and raw provider metadata. Request / response flow:
  1. GoogleRequestBuilder::build($modelId, 'google', $request, stream: false) converts the SDK request into a Gemini-compatible JSON body.
  2. The body is POST-ed to {baseUrl}/interactions with the API key auth headers.
  3. The raw JSON response array is passed to GoogleResponseParser::parse().
  4. A typed TextModelResponse is returned to the caller.
Example:
use AiSdk\Google\Google;
use AiSdk\Requests\TextModelRequest;

$model = Google::model('gemini-2.5-pro');

$response = $model->generate(
    TextModelRequest::make()
        ->withSystem('You are a helpful assistant.')
        ->withUserMessage('Explain quantum entanglement in two sentences.')
);

echo $response->text();

stream(TextModelRequest $request): Generator

Performs a streaming text generation request using Server-Sent Events (SSE). The method returns a Generator that yields StreamPart objects as each SSE event arrives, allowing you to process partial output incrementally.
/** @return Generator<int, StreamPart> */
public function stream(TextModelRequest $request): Generator
request
TextModelRequest
required
A TextModelRequest — identical to generate(). The stream: true flag is added internally; you do not need to set it yourself.
Returns a Generator<int, StreamPart> that yields:
  • TextDeltaPart — incremental text chunks
  • ReasoningDeltaPart — incremental reasoning/thought chunks (when thinking is enabled)
  • ToolCallStartPart / ToolCallDeltaPart — streaming tool call assembly
  • ProviderMetadataPart — metadata emitted after all content
  • FinishPart — final part carrying the finish reason and accumulated token usage
Request / response flow:
  1. GoogleRequestBuilder::build($modelId, 'google', $request, stream: true) builds the body with stream: true.
  2. The body is POST-ed to {baseUrl}/interactions?alt=sse.
  3. The SSE event stream is handed to GoogleStreamParser::parse().
  4. The generator yields StreamPart objects as events are processed.
Example:
use AiSdk\Google\Google;
use AiSdk\Requests\TextModelRequest;
use AiSdk\Streaming\TextDeltaPart;

$model = Google::model('gemini-2.0-flash');

foreach ($model->stream(TextModelRequest::make()->withUserMessage('Write a short poem.')) as $part) {
    if ($part instanceof TextDeltaPart) {
        echo $part->text;
    }
}

Request / Response Flow Summary

The following describes the full lifecycle of a generate() call:
  1. BuildGoogleRequestBuilder::build() maps TextModelRequest fields (messages, system prompt, max tokens, temperature, top-p, reasoning effort, output format) into a Gemini API JSON body.
  2. Dispatch — The body is POST-ed to {baseUrl}/interactions using the SDK HTTP runner with Google auth headers (x-api-key or Authorization: Bearer).
  3. Parse — The JSON response is decoded and handed to GoogleResponseParser::parse(), which extracts text parts, reasoning parts, and tool calls, resolves the finish reason, and tallies token usage.
  4. Return — A TextModelResponse carrying typed parts, a FinishReason enum value, a Usage object, the raw response array, and providerMetadata['google'] is returned.
For stream(), step 2 targets /interactions?alt=sse and step 3 is handled incrementally by GoogleStreamParser::parse(), which yields StreamPart objects rather than constructing a single response.

Build docs developers (and LLMs) love