Skip to main content

Google logoGoogle

Access 24 Google models through Mastra's model router. Authentication is handled automatically using the GOOGLE_GENERATIVE_AI_API_KEY environment variable.

Learn more in the Google documentation.

GOOGLE_GENERATIVE_AI_API_KEY=your-api-key
import { Agent } from "@mastra/core";

const agent = new Agent({
name: "my-agent",
instructions: "You are a helpful assistant",
model: "google/gemini-1.5-flash"
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
console.log(chunk);
}

Models

ModelImageAudioVideoToolsStreamingContext Window
google/gemini-1.5-flash1,000,000
google/gemini-1.5-flash-8b1,000,000
google/gemini-1.5-pro1,000,000
google/gemini-2.0-flash1,048,576
google/gemini-2.0-flash-lite1,048,576
google/gemini-2.5-flash1,048,576
google/gemini-2.5-flash-image32,768
google/gemini-2.5-flash-image-preview32,768
google/gemini-2.5-flash-lite1,048,576
google/gemini-2.5-flash-lite-preview-06-171,048,576
google/gemini-2.5-flash-lite-preview-09-20251,048,576
google/gemini-2.5-flash-preview-04-171,048,576
google/gemini-2.5-flash-preview-05-201,048,576
google/gemini-2.5-flash-preview-09-20251,048,576
google/gemini-2.5-flash-preview-tts8,000
google/gemini-2.5-pro1,048,576
google/gemini-2.5-pro-preview-05-061,048,576
google/gemini-2.5-pro-preview-06-051,048,576
google/gemini-2.5-pro-preview-tts8,000
google/gemini-embedding-0012,048
google/gemini-flash-latest1,048,576
google/gemini-flash-lite-latest1,048,576
google/gemini-live-2.5-flash128,000
google/gemini-live-2.5-flash-preview-native-audio131,072

Advanced Configuration

Custom Headers

const agent = new Agent({
name: "custom-agent",
model: {
id: "google/gemini-1.5-flash",
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
headers: {
"X-Custom-Header": "value"
}
}
});

Dynamic Model Selection

const agent = new Agent({
name: "dynamic-agent",
model: ({ runtimeContext }) => {
const useAdvanced = runtimeContext.task === "complex";
return useAdvanced
? "google/gemini-live-2.5-flash-preview-native-audio"
: "google/gemini-1.5-flash";
}
});

Provider Options

Google supports the following provider-specific options via the providerOptions parameter:

const response = await agent.generate("Hello!", {
providerOptions: {
google: {
// See available options in the table below
}
}
});

Available Options

responseModalities?:

("TEXT" | "IMAGE")[] | undefined

thinkingConfig?:

{ thinkingBudget?: number | undefined; includeThoughts?: boolean | undefined; } | undefined

cachedContent?:

string | undefined

structuredOutputs?:

boolean | undefined

safetySettings?:

{ category: "HARM_CATEGORY_UNSPECIFIED" | "HARM_CATEGORY_HATE_SPEECH" | "HARM_CATEGORY_DANGEROUS_CONTENT" | "HARM_CATEGORY_HARASSMENT" | "HARM_CATEGORY_SEXUALLY_EXPLICIT" | "HARM_CATEGORY_CIVIC_INTEGRITY"; threshold: "HARM_BLOCK_THRESHOLD_UNSPECIFIED" | ... 4 more ... | "OFF"; }[] | undefined

threshold?:

"HARM_BLOCK_THRESHOLD_UNSPECIFIED" | "BLOCK_LOW_AND_ABOVE" | "BLOCK_MEDIUM_AND_ABOVE" | "BLOCK_ONLY_HIGH" | "BLOCK_NONE" | "OFF" | undefined

audioTimestamp?:

boolean | undefined

labels?:

Record<string, string> | undefined

mediaResolution?:

"MEDIA_RESOLUTION_UNSPECIFIED" | "MEDIA_RESOLUTION_LOW" | "MEDIA_RESOLUTION_MEDIUM" | "MEDIA_RESOLUTION_HIGH" | undefined

imageConfig?:

{ aspectRatio?: "1:1" | "2:3" | "3:2" | "3:4" | "4:3" | "4:5" | "5:4" | "9:16" | "16:9" | "21:9" | undefined; } | undefined

Direct Provider Installation

This provider can also be installed directly as a standalone package, which can be used instead of the Mastra model router string. View the package documentation for more details.

npm install @ai-sdk/google

For detailed provider-specific documentation, see the AI SDK Google provider docs.