Core provider functionality for configuring and creating Google Generative AI provider instances with custom settings and authentication.
Creates a new Google Generative AI provider instance with optional configuration settings.
/**
* Creates a Google Generative AI provider instance
* @param options - Optional configuration settings
* @returns Configured GoogleGenerativeAIProvider instance
*/
function createGoogleGenerativeAI(
options?: GoogleGenerativeAIProviderSettings
): GoogleGenerativeAIProvider;
interface GoogleGenerativeAIProviderSettings {
/** Base URL for Google Generative AI API (optional) */
baseURL?: string;
/** API key for authentication (optional, can use GOOGLE_GENERATIVE_AI_API_KEY env var) */
apiKey?: string;
/** Additional headers to include in requests */
headers?: Record<string, string>;
/** Custom fetch implementation (optional) */
fetch?: typeof fetch;
/** Function to generate unique IDs for requests (optional) */
generateId?: () => string;
}Usage Examples:
import { createGoogleGenerativeAI } from "@ai-sdk/google";
// Create with API key
const google = createGoogleGenerativeAI({
apiKey: "your-api-key-here",
});
// Create with custom headers and base URL
const google = createGoogleGenerativeAI({
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
baseURL: "https://custom-endpoint.googleapis.com",
headers: {
"Custom-Header": "value",
},
});
// Create with custom fetch implementation
const google = createGoogleGenerativeAI({
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
fetch: customFetchImplementation,
});Pre-configured provider instance ready to use without setup.
/**
* Default Google Generative AI provider instance
* Uses GOOGLE_GENERATIVE_AI_API_KEY environment variable for authentication
*/
const google: GoogleGenerativeAIProvider;Usage Examples:
import { google } from "@ai-sdk/google";
import { generateText } from "ai";
// Use directly with default configuration
const result = await generateText({
model: google("gemini-1.5-flash"),
prompt: "Hello, world!",
});
// Access different model types
const languageModel = google("gemini-1.5-pro");
const imageModel = google.image("imagen-3.0-generate-002");
const embeddingModel = google.textEmbedding("text-embedding-004");Main provider interface extending the AI SDK's ProviderV2 interface.
interface GoogleGenerativeAIProvider extends ProviderV2 {
/** Callable interface - shorthand for languageModel() */
(modelId: GoogleGenerativeAIModelId): LanguageModelV2<
GoogleGenerativeAIProviderOptions,
GoogleGenerativeAIProviderMetadata
>;
/** Get a language model instance */
languageModel(modelId: GoogleGenerativeAIModelId): LanguageModelV2<
GoogleGenerativeAIProviderOptions,
GoogleGenerativeAIProviderMetadata
>;
/** Get a chat model instance (alias for languageModel) */
chat(modelId: GoogleGenerativeAIModelId): LanguageModelV2<
GoogleGenerativeAIProviderOptions,
GoogleGenerativeAIProviderMetadata
>;
/** Get an image generation model instance */
image(modelId: GoogleGenerativeAIImageModelId, settings?: GoogleGenerativeAIImageSettings): ImageModelV2<
GoogleGenerativeAIImageProviderOptions
>;
/** Get a text embedding model instance */
textEmbedding(modelId: GoogleGenerativeAIEmbeddingModelId): EmbeddingModelV2<
GoogleGenerativeAIEmbeddingProviderOptions
>;
/** Get a text embedding model instance (alias for textEmbedding) */
textEmbeddingModel(modelId: GoogleGenerativeAIEmbeddingModelId): EmbeddingModelV2<
GoogleGenerativeAIEmbeddingProviderOptions
>;
/** @deprecated Use textEmbedding() instead */
embedding(modelId: GoogleGenerativeAIEmbeddingModelId): EmbeddingModelV2<
GoogleGenerativeAIEmbeddingProviderOptions
>;
/** @deprecated Use chat() instead */
generativeAI(modelId: GoogleGenerativeAIModelId): LanguageModelV2<
GoogleGenerativeAIProviderOptions,
GoogleGenerativeAIProviderMetadata
>;
/** Built-in tools */
tools: {
googleSearch: (options?: GoogleSearchOptions) => Tool;
urlContext: () => Tool;
codeExecution: () => Tool;
};
}The provider supports multiple authentication methods:
GOOGLE_GENERATIVE_AI_API_KEY environment variableapiKey in GoogleGenerativeAIProviderSettings// Method 1: Environment variable (recommended)
process.env.GOOGLE_GENERATIVE_AI_API_KEY = "your-api-key";
const google = createGoogleGenerativeAI();
// Method 2: Constructor option
const google = createGoogleGenerativeAI({
apiKey: "your-api-key",
});
// Method 3: Default provider with environment variable
import { google } from "@ai-sdk/google";
// Automatically uses GOOGLE_GENERATIVE_AI_API_KEYconst google = createGoogleGenerativeAI({
baseURL: "https://generativelanguage.googleapis.com",
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
});const google = createGoogleGenerativeAI({
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
headers: {
"User-Agent": "MyApp/1.0",
"X-Custom-Header": "custom-value",
},
});import { createGoogleGenerativeAI } from "@ai-sdk/google";
const google = createGoogleGenerativeAI({
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
fetch: async (url, options) => {
// Custom fetch logic with retry, logging, etc.
console.log(`Making request to ${url}`);
return fetch(url, options);
},
});const google = createGoogleGenerativeAI({
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
generateId: () => `custom-${Date.now()}-${Math.random()}`,
});