@ai-sdk/google 3.0.11 → 3.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/docs/15-google-generative-ai.mdx +1088 -0
- package/package.json +10 -6
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -30,7 +30,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
30
30
|
var import_provider_utils15 = require("@ai-sdk/provider-utils");
|
|
31
31
|
|
|
32
32
|
// src/version.ts
|
|
33
|
-
var VERSION = true ? "3.0.
|
|
33
|
+
var VERSION = true ? "3.0.12" : "0.0.0-test";
|
|
34
34
|
|
|
35
35
|
// src/google-generative-ai-embedding-model.ts
|
|
36
36
|
var import_provider = require("@ai-sdk/provider");
|
package/dist/index.mjs
CHANGED
|
@@ -0,0 +1,1088 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Google Generative AI
|
|
3
|
+
description: Learn how to use Google Generative AI Provider.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Google Generative AI Provider
|
|
7
|
+
|
|
8
|
+
The [Google Generative AI](https://ai.google.dev) provider contains language and embedding model support for
|
|
9
|
+
the [Google Generative AI](https://ai.google.dev/api/rest) APIs.
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
The Google provider is available in the `@ai-sdk/google` module. You can install it with
|
|
14
|
+
|
|
15
|
+
<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}>
|
|
16
|
+
<Tab>
|
|
17
|
+
<Snippet text="pnpm add @ai-sdk/google" dark />
|
|
18
|
+
</Tab>
|
|
19
|
+
<Tab>
|
|
20
|
+
<Snippet text="npm install @ai-sdk/google" dark />
|
|
21
|
+
</Tab>
|
|
22
|
+
<Tab>
|
|
23
|
+
<Snippet text="yarn add @ai-sdk/google" dark />
|
|
24
|
+
</Tab>
|
|
25
|
+
|
|
26
|
+
<Tab>
|
|
27
|
+
<Snippet text="bun add @ai-sdk/google" dark />
|
|
28
|
+
</Tab>
|
|
29
|
+
</Tabs>
|
|
30
|
+
|
|
31
|
+
## Provider Instance
|
|
32
|
+
|
|
33
|
+
You can import the default provider instance `google` from `@ai-sdk/google`:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { google } from '@ai-sdk/google';
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
If you need a customized setup, you can import `createGoogleGenerativeAI` from `@ai-sdk/google` and create a provider instance with your settings:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { createGoogleGenerativeAI } from '@ai-sdk/google';
|
|
43
|
+
|
|
44
|
+
const google = createGoogleGenerativeAI({
|
|
45
|
+
// custom settings
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
You can use the following optional settings to customize the Google Generative AI provider instance:
|
|
50
|
+
|
|
51
|
+
- **baseURL** _string_
|
|
52
|
+
|
|
53
|
+
Use a different URL prefix for API calls, e.g. to use proxy servers.
|
|
54
|
+
The default prefix is `https://generativelanguage.googleapis.com/v1beta`.
|
|
55
|
+
|
|
56
|
+
- **apiKey** _string_
|
|
57
|
+
|
|
58
|
+
API key that is being sent using the `x-goog-api-key` header.
|
|
59
|
+
It defaults to the `GOOGLE_GENERATIVE_AI_API_KEY` environment variable.
|
|
60
|
+
|
|
61
|
+
- **headers** _Record<string,string>_
|
|
62
|
+
|
|
63
|
+
Custom headers to include in the requests.
|
|
64
|
+
|
|
65
|
+
- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise<Response>_
|
|
66
|
+
|
|
67
|
+
Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
|
|
68
|
+
Defaults to the global `fetch` function.
|
|
69
|
+
You can use it as a middleware to intercept requests,
|
|
70
|
+
or to provide a custom fetch implementation for e.g. testing.
|
|
71
|
+
|
|
72
|
+
## Language Models
|
|
73
|
+
|
|
74
|
+
You can create models that call the [Google Generative AI API](https://ai.google.dev/api/rest) using the provider instance.
|
|
75
|
+
The first argument is the model id, e.g. `gemini-2.5-flash`.
|
|
76
|
+
The models support tool calls and some have multi-modal capabilities.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const model = google('gemini-2.5-flash');
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
You can use Google Generative AI language models to generate text with the `generateText` function:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { google } from '@ai-sdk/google';
|
|
86
|
+
import { generateText } from 'ai';
|
|
87
|
+
|
|
88
|
+
const { text } = await generateText({
|
|
89
|
+
model: google('gemini-2.5-flash'),
|
|
90
|
+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Google Generative AI language models can also be used in the `streamText`, `generateObject`, and `streamObject` functions
|
|
95
|
+
(see [AI SDK Core](/docs/ai-sdk-core)).
|
|
96
|
+
|
|
97
|
+
Google Generative AI also supports some model specific settings that are not part of the [standard call settings](/docs/ai-sdk-core/settings).
|
|
98
|
+
You can pass them as an options argument:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const model = google('gemini-2.5-flash');
|
|
102
|
+
|
|
103
|
+
await generateText({
|
|
104
|
+
model,
|
|
105
|
+
providerOptions: {
|
|
106
|
+
google: {
|
|
107
|
+
safetySettings: [
|
|
108
|
+
{
|
|
109
|
+
category: 'HARM_CATEGORY_UNSPECIFIED',
|
|
110
|
+
threshold: 'BLOCK_LOW_AND_ABOVE',
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The following optional provider options are available for Google Generative AI models:
|
|
119
|
+
|
|
120
|
+
- **cachedContent** _string_
|
|
121
|
+
|
|
122
|
+
Optional. The name of the cached content used as context to serve the prediction.
|
|
123
|
+
Format: cachedContents/\{cachedContent\}
|
|
124
|
+
|
|
125
|
+
- **structuredOutputs** _boolean_
|
|
126
|
+
|
|
127
|
+
Optional. Enable structured output. Default is true.
|
|
128
|
+
|
|
129
|
+
This is useful when the JSON Schema contains elements that are
|
|
130
|
+
not supported by the OpenAPI schema version that
|
|
131
|
+
Google Generative AI uses. You can use this to disable
|
|
132
|
+
structured outputs if you need to.
|
|
133
|
+
|
|
134
|
+
See [Troubleshooting: Schema Limitations](#schema-limitations) for more details.
|
|
135
|
+
|
|
136
|
+
- **safetySettings** _Array\<\{ category: string; threshold: string \}\>_
|
|
137
|
+
|
|
138
|
+
Optional. Safety settings for the model.
|
|
139
|
+
|
|
140
|
+
- **category** _string_
|
|
141
|
+
|
|
142
|
+
The category of the safety setting. Can be one of the following:
|
|
143
|
+
|
|
144
|
+
- `HARM_CATEGORY_HATE_SPEECH`
|
|
145
|
+
- `HARM_CATEGORY_DANGEROUS_CONTENT`
|
|
146
|
+
- `HARM_CATEGORY_HARASSMENT`
|
|
147
|
+
- `HARM_CATEGORY_SEXUALLY_EXPLICIT`
|
|
148
|
+
|
|
149
|
+
- **threshold** _string_
|
|
150
|
+
|
|
151
|
+
The threshold of the safety setting. Can be one of the following:
|
|
152
|
+
|
|
153
|
+
- `HARM_BLOCK_THRESHOLD_UNSPECIFIED`
|
|
154
|
+
- `BLOCK_LOW_AND_ABOVE`
|
|
155
|
+
- `BLOCK_MEDIUM_AND_ABOVE`
|
|
156
|
+
- `BLOCK_ONLY_HIGH`
|
|
157
|
+
- `BLOCK_NONE`
|
|
158
|
+
|
|
159
|
+
- **responseModalities** _string[]_
|
|
160
|
+
The modalities to use for the response. The following modalities are supported: `TEXT`, `IMAGE`. When not defined or empty, the model defaults to returning only text.
|
|
161
|
+
|
|
162
|
+
- **thinkingConfig** _\{ thinkingLevel?: 'minimal' | 'low' | 'medium' | 'high'; thinkingBudget?: number; includeThoughts?: boolean \}_
|
|
163
|
+
|
|
164
|
+
Optional. Configuration for the model's thinking process. Only supported by specific [Google Generative AI models](https://ai.google.dev/gemini-api/docs/thinking).
|
|
165
|
+
|
|
166
|
+
- **thinkingLevel** _'minimal' | 'low' | 'medium' | 'high'_
|
|
167
|
+
|
|
168
|
+
Optional. Controls the thinking depth for Gemini 3 models. Gemini 3 Pro supports 'low' and 'high', while Gemini 3 Flash supports all four levels: 'minimal', 'low', 'medium', and 'high'. Only supported by Gemini 3 models (`gemini-3-pro-preview` and later).
|
|
169
|
+
|
|
170
|
+
- **thinkingBudget** _number_
|
|
171
|
+
|
|
172
|
+
Optional. Gives the model guidance on the number of thinking tokens it can use when generating a response. Setting it to 0 disables thinking, if the model supports it.
|
|
173
|
+
For more information about the possible value ranges for each model see [Google Generative AI thinking documentation](https://ai.google.dev/gemini-api/docs/thinking#set-budget).
|
|
174
|
+
|
|
175
|
+
<Note>
|
|
176
|
+
This option is for Gemini 2.5 models. Gemini 3 models should use
|
|
177
|
+
`thinkingLevel` instead.
|
|
178
|
+
</Note>
|
|
179
|
+
|
|
180
|
+
- **includeThoughts** _boolean_
|
|
181
|
+
|
|
182
|
+
Optional. If set to true, thought summaries are returned, which are synthisized versions of the model's raw thoughts and offer insights into the model's internal reasoning process.
|
|
183
|
+
|
|
184
|
+
- **imageConfig** _\{ aspectRatio: string \}_
|
|
185
|
+
|
|
186
|
+
Optional. Configuration for the models image generation. Only supported by specific [Google Generative AI models](https://ai.google.dev/gemini-api/docs/image-generation).
|
|
187
|
+
|
|
188
|
+
- **aspectRatio** _string_
|
|
189
|
+
|
|
190
|
+
Model defaults to generate 1:1 squares, or to matching the output image size to that of your input image. Can be one of the following:
|
|
191
|
+
|
|
192
|
+
- 1:1
|
|
193
|
+
- 2:3
|
|
194
|
+
- 3:2
|
|
195
|
+
- 3:4
|
|
196
|
+
- 4:3
|
|
197
|
+
- 4:5
|
|
198
|
+
- 5:4
|
|
199
|
+
- 9:16
|
|
200
|
+
- 16:9
|
|
201
|
+
- 21:9
|
|
202
|
+
|
|
203
|
+
### Thinking
|
|
204
|
+
|
|
205
|
+
The Gemini 2.5 and Gemini 3 series models use an internal "thinking process" that significantly improves their reasoning and multi-step planning abilities, making them highly effective for complex tasks such as coding, advanced mathematics, and data analysis. For more information see [Google Generative AI thinking documentation](https://ai.google.dev/gemini-api/docs/thinking).
|
|
206
|
+
|
|
207
|
+
#### Gemini 3 Models
|
|
208
|
+
|
|
209
|
+
For Gemini 3 models, use the `thinkingLevel` parameter to control the depth of reasoning:
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
import { google, GoogleGenerativeAIProviderOptions } from '@ai-sdk/google';
|
|
213
|
+
import { generateText } from 'ai';
|
|
214
|
+
|
|
215
|
+
const model = google('gemini-3-pro-preview');
|
|
216
|
+
|
|
217
|
+
const { text, reasoning } = await generateText({
|
|
218
|
+
model: model,
|
|
219
|
+
prompt: 'What is the sum of the first 10 prime numbers?',
|
|
220
|
+
providerOptions: {
|
|
221
|
+
google: {
|
|
222
|
+
thinkingConfig: {
|
|
223
|
+
thinkingLevel: 'high',
|
|
224
|
+
includeThoughts: true,
|
|
225
|
+
},
|
|
226
|
+
} satisfies GoogleGenerativeAIProviderOptions,
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
console.log(text);
|
|
231
|
+
|
|
232
|
+
console.log(reasoning); // Reasoning summary
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
#### Gemini 2.5 Models
|
|
236
|
+
|
|
237
|
+
For Gemini 2.5 models, use the `thinkingBudget` parameter to control the number of thinking tokens:
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
import { google, GoogleGenerativeAIProviderOptions } from '@ai-sdk/google';
|
|
241
|
+
import { generateText } from 'ai';
|
|
242
|
+
|
|
243
|
+
const model = google('gemini-2.5-flash');
|
|
244
|
+
|
|
245
|
+
const { text, reasoning } = await generateText({
|
|
246
|
+
model: model,
|
|
247
|
+
prompt: 'What is the sum of the first 10 prime numbers?',
|
|
248
|
+
providerOptions: {
|
|
249
|
+
google: {
|
|
250
|
+
thinkingConfig: {
|
|
251
|
+
thinkingBudget: 8192,
|
|
252
|
+
includeThoughts: true,
|
|
253
|
+
},
|
|
254
|
+
} satisfies GoogleGenerativeAIProviderOptions,
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
console.log(text);
|
|
259
|
+
|
|
260
|
+
console.log(reasoning); // Reasoning summary
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### File Inputs
|
|
264
|
+
|
|
265
|
+
The Google Generative AI provider supports file inputs, e.g. PDF files.
|
|
266
|
+
|
|
267
|
+
```ts
|
|
268
|
+
import { google } from '@ai-sdk/google';
|
|
269
|
+
import { generateText } from 'ai';
|
|
270
|
+
|
|
271
|
+
const result = await generateText({
|
|
272
|
+
model: google('gemini-2.5-flash'),
|
|
273
|
+
messages: [
|
|
274
|
+
{
|
|
275
|
+
role: 'user',
|
|
276
|
+
content: [
|
|
277
|
+
{
|
|
278
|
+
type: 'text',
|
|
279
|
+
text: 'What is an embedding model according to this document?',
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
type: 'file',
|
|
283
|
+
data: fs.readFileSync('./data/ai.pdf'),
|
|
284
|
+
mediaType: 'application/pdf',
|
|
285
|
+
},
|
|
286
|
+
],
|
|
287
|
+
},
|
|
288
|
+
],
|
|
289
|
+
});
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
You can also use YouTube URLs directly:
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
import { google } from '@ai-sdk/google';
|
|
296
|
+
import { generateText } from 'ai';
|
|
297
|
+
|
|
298
|
+
const result = await generateText({
|
|
299
|
+
model: google('gemini-2.5-flash'),
|
|
300
|
+
messages: [
|
|
301
|
+
{
|
|
302
|
+
role: 'user',
|
|
303
|
+
content: [
|
|
304
|
+
{
|
|
305
|
+
type: 'text',
|
|
306
|
+
text: 'Summarize this video',
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
type: 'file',
|
|
310
|
+
data: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
|
|
311
|
+
mediaType: 'video/mp4',
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
},
|
|
315
|
+
],
|
|
316
|
+
});
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
<Note>
|
|
320
|
+
The AI SDK will automatically download URLs if you pass them as data, except
|
|
321
|
+
for `https://generativelanguage.googleapis.com/v1beta/files/` and YouTube
|
|
322
|
+
URLs. You can use the Google Generative AI Files API to upload larger files to
|
|
323
|
+
that location. YouTube URLs (public or unlisted videos) are supported directly
|
|
324
|
+
- you can specify one YouTube video URL per request.
|
|
325
|
+
</Note>
|
|
326
|
+
|
|
327
|
+
See [File Parts](/docs/foundations/prompts#file-parts) for details on how to use files in prompts.
|
|
328
|
+
|
|
329
|
+
### Cached Content
|
|
330
|
+
|
|
331
|
+
Google Generative AI supports both explicit and implicit caching to help reduce costs on repetitive content.
|
|
332
|
+
|
|
333
|
+
#### Implicit Caching
|
|
334
|
+
|
|
335
|
+
Gemini 2.5 models automatically provide cache cost savings without needing to create an explicit cache. When you send requests that share common prefixes with previous requests, you'll receive a 75% token discount on cached content.
|
|
336
|
+
|
|
337
|
+
To maximize cache hits with implicit caching:
|
|
338
|
+
|
|
339
|
+
- Keep content at the beginning of requests consistent
|
|
340
|
+
- Add variable content (like user questions) at the end of prompts
|
|
341
|
+
- Ensure requests meet minimum token requirements:
|
|
342
|
+
- Gemini 2.5 Flash: 1024 tokens minimum
|
|
343
|
+
- Gemini 2.5 Pro: 2048 tokens minimum
|
|
344
|
+
|
|
345
|
+
```ts
|
|
346
|
+
import { google } from '@ai-sdk/google';
|
|
347
|
+
import { generateText } from 'ai';
|
|
348
|
+
|
|
349
|
+
// Structure prompts with consistent content at the beginning
|
|
350
|
+
const baseContext =
|
|
351
|
+
'You are a cooking assistant with expertise in Italian cuisine. Here are 1000 lasagna recipes for reference...';
|
|
352
|
+
|
|
353
|
+
const { text: veggieLasagna } = await generateText({
|
|
354
|
+
model: google('gemini-2.5-pro'),
|
|
355
|
+
prompt: `${baseContext}\n\nWrite a vegetarian lasagna recipe for 4 people.`,
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
// Second request with same prefix - eligible for cache hit
|
|
359
|
+
const { text: meatLasagna, providerMetadata } = await generateText({
|
|
360
|
+
model: google('gemini-2.5-pro'),
|
|
361
|
+
prompt: `${baseContext}\n\nWrite a meat lasagna recipe for 12 people.`,
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
// Check cached token count in usage metadata
|
|
365
|
+
console.log('Cached tokens:', providerMetadata.google?.usageMetadata);
|
|
366
|
+
// e.g.
|
|
367
|
+
// {
|
|
368
|
+
// groundingMetadata: null,
|
|
369
|
+
// safetyRatings: null,
|
|
370
|
+
// usageMetadata: {
|
|
371
|
+
// cachedContentTokenCount: 2027,
|
|
372
|
+
// thoughtsTokenCount: 702,
|
|
373
|
+
// promptTokenCount: 2152,
|
|
374
|
+
// candidatesTokenCount: 710,
|
|
375
|
+
// totalTokenCount: 3564
|
|
376
|
+
// }
|
|
377
|
+
// }
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
<Note>
|
|
381
|
+
Usage metadata was added to `providerMetadata` in `@ai-sdk/google@1.2.23`. If
|
|
382
|
+
you are using an older version, usage metadata is available in the raw HTTP
|
|
383
|
+
`response` body returned as part of the return value from `generateText`.
|
|
384
|
+
</Note>
|
|
385
|
+
|
|
386
|
+
#### Explicit Caching
|
|
387
|
+
|
|
388
|
+
For guaranteed cost savings, you can still use explicit caching with Gemini 2.5 and 2.0 models. See the [models page](https://ai.google.dev/gemini-api/docs/models) to check if caching is supported for the used model:
|
|
389
|
+
|
|
390
|
+
```ts
|
|
391
|
+
import { google } from '@ai-sdk/google';
|
|
392
|
+
import { GoogleAICacheManager } from '@google/generative-ai/server';
|
|
393
|
+
import { generateText } from 'ai';
|
|
394
|
+
|
|
395
|
+
const cacheManager = new GoogleAICacheManager(
|
|
396
|
+
process.env.GOOGLE_GENERATIVE_AI_API_KEY,
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
const model = 'gemini-2.5-pro';
|
|
400
|
+
|
|
401
|
+
const { name: cachedContent } = await cacheManager.create({
|
|
402
|
+
model,
|
|
403
|
+
contents: [
|
|
404
|
+
{
|
|
405
|
+
role: 'user',
|
|
406
|
+
parts: [{ text: '1000 Lasagna Recipes...' }],
|
|
407
|
+
},
|
|
408
|
+
],
|
|
409
|
+
ttlSeconds: 60 * 5,
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
const { text: veggieLasangaRecipe } = await generateText({
|
|
413
|
+
model: google(model),
|
|
414
|
+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
|
415
|
+
providerOptions: {
|
|
416
|
+
google: {
|
|
417
|
+
cachedContent,
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
const { text: meatLasangaRecipe } = await generateText({
|
|
423
|
+
model: google(model),
|
|
424
|
+
prompt: 'Write a meat lasagna recipe for 12 people.',
|
|
425
|
+
providerOptions: {
|
|
426
|
+
google: {
|
|
427
|
+
cachedContent,
|
|
428
|
+
},
|
|
429
|
+
},
|
|
430
|
+
});
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
### Code Execution
|
|
434
|
+
|
|
435
|
+
With [Code Execution](https://ai.google.dev/gemini-api/docs/code-execution), certain models can generate and execute Python code to perform calculations, solve problems, or provide more accurate information.
|
|
436
|
+
|
|
437
|
+
You can enable code execution by adding the `code_execution` tool to your request.
|
|
438
|
+
|
|
439
|
+
```ts
|
|
440
|
+
import { google } from '@ai-sdk/google';
|
|
441
|
+
import { googleTools } from '@ai-sdk/google/internal';
|
|
442
|
+
import { generateText } from 'ai';
|
|
443
|
+
|
|
444
|
+
const { text, toolCalls, toolResults } = await generateText({
|
|
445
|
+
model: google('gemini-2.5-pro'),
|
|
446
|
+
tools: { code_execution: google.tools.codeExecution({}) },
|
|
447
|
+
prompt: 'Use python to calculate the 20th fibonacci number.',
|
|
448
|
+
});
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
The response will contain the tool calls and results from the code execution.
|
|
452
|
+
|
|
453
|
+
### Google Search
|
|
454
|
+
|
|
455
|
+
With [search grounding](https://ai.google.dev/gemini-api/docs/google-search),
|
|
456
|
+
the model has access to the latest information using Google search.
|
|
457
|
+
Google search can be used to provide answers around current events:
|
|
458
|
+
|
|
459
|
+
```ts highlight="8,17-20"
|
|
460
|
+
import { google } from '@ai-sdk/google';
|
|
461
|
+
import { GoogleGenerativeAIProviderMetadata } from '@ai-sdk/google';
|
|
462
|
+
import { generateText } from 'ai';
|
|
463
|
+
|
|
464
|
+
const { text, sources, providerMetadata } = await generateText({
|
|
465
|
+
model: google('gemini-2.5-flash'),
|
|
466
|
+
tools: {
|
|
467
|
+
google_search: google.tools.googleSearch({}),
|
|
468
|
+
},
|
|
469
|
+
prompt:
|
|
470
|
+
'List the top 5 San Francisco news from the past week.' +
|
|
471
|
+
'You must include the date of each article.',
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
// access the grounding metadata. Casting to the provider metadata type
|
|
475
|
+
// is optional but provides autocomplete and type safety.
|
|
476
|
+
const metadata = providerMetadata?.google as
|
|
477
|
+
| GoogleGenerativeAIProviderMetadata
|
|
478
|
+
| undefined;
|
|
479
|
+
const groundingMetadata = metadata?.groundingMetadata;
|
|
480
|
+
const safetyRatings = metadata?.safetyRatings;
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
When Search Grounding is enabled, the model will include sources in the response.
|
|
484
|
+
|
|
485
|
+
Additionally, the grounding metadata includes detailed information about how search results were used to ground the model's response. Here are the available fields:
|
|
486
|
+
|
|
487
|
+
- **`webSearchQueries`** (`string[] | null`)
|
|
488
|
+
|
|
489
|
+
- Array of search queries used to retrieve information
|
|
490
|
+
- Example: `["What's the weather in Chicago this weekend?"]`
|
|
491
|
+
|
|
492
|
+
- **`searchEntryPoint`** (`{ renderedContent: string } | null`)
|
|
493
|
+
|
|
494
|
+
- Contains the main search result content used as an entry point
|
|
495
|
+
- The `renderedContent` field contains the formatted content
|
|
496
|
+
|
|
497
|
+
- **`groundingSupports`** (Array of support objects | null)
|
|
498
|
+
- Contains details about how specific response parts are supported by search results
|
|
499
|
+
- Each support object includes:
|
|
500
|
+
- **`segment`**: Information about the grounded text segment
|
|
501
|
+
- `text`: The actual text segment
|
|
502
|
+
- `startIndex`: Starting position in the response
|
|
503
|
+
- `endIndex`: Ending position in the response
|
|
504
|
+
- **`groundingChunkIndices`**: References to supporting search result chunks
|
|
505
|
+
- **`confidenceScores`**: Confidence scores (0-1) for each supporting chunk
|
|
506
|
+
|
|
507
|
+
Example response:
|
|
508
|
+
|
|
509
|
+
```json
|
|
510
|
+
{
|
|
511
|
+
"groundingMetadata": {
|
|
512
|
+
"webSearchQueries": ["What's the weather in Chicago this weekend?"],
|
|
513
|
+
"searchEntryPoint": {
|
|
514
|
+
"renderedContent": "..."
|
|
515
|
+
},
|
|
516
|
+
"groundingSupports": [
|
|
517
|
+
{
|
|
518
|
+
"segment": {
|
|
519
|
+
"startIndex": 0,
|
|
520
|
+
"endIndex": 65,
|
|
521
|
+
"text": "Chicago weather changes rapidly, so layers let you adjust easily."
|
|
522
|
+
},
|
|
523
|
+
"groundingChunkIndices": [0],
|
|
524
|
+
"confidenceScores": [0.99]
|
|
525
|
+
}
|
|
526
|
+
]
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
### File Search
|
|
532
|
+
|
|
533
|
+
The [File Search tool](https://ai.google.dev/gemini-api/docs/file-search) lets Gemini retrieve context from your own documents that you have indexed in File Search stores. Only Gemini 2.5 and Gemini 3 models support this feature.
|
|
534
|
+
|
|
535
|
+
```ts highlight="9-13"
|
|
536
|
+
import { google } from '@ai-sdk/google';
|
|
537
|
+
import { generateText } from 'ai';
|
|
538
|
+
|
|
539
|
+
const { text, sources } = await generateText({
|
|
540
|
+
model: google('gemini-2.5-pro'),
|
|
541
|
+
tools: {
|
|
542
|
+
file_search: google.tools.fileSearch({
|
|
543
|
+
fileSearchStoreNames: [
|
|
544
|
+
'projects/my-project/locations/us/fileSearchStores/my-store',
|
|
545
|
+
],
|
|
546
|
+
metadataFilter: 'author = "Robert Graves"',
|
|
547
|
+
topK: 8,
|
|
548
|
+
}),
|
|
549
|
+
},
|
|
550
|
+
prompt: "Summarise the key themes of 'I, Claudius'.",
|
|
551
|
+
});
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
File Search responses include citations via the normal `sources` field and expose raw [grounding metadata](#google-search) in `providerMetadata.google.groundingMetadata`.
|
|
555
|
+
|
|
556
|
+
### URL Context
|
|
557
|
+
|
|
558
|
+
Google provides a provider-defined URL context tool.
|
|
559
|
+
|
|
560
|
+
The URL context tool allows you to provide specific URLs that you want the model to analyze directly in from the prompt.
|
|
561
|
+
|
|
562
|
+
```ts highlight="9,13-17"
|
|
563
|
+
import { google } from '@ai-sdk/google';
|
|
564
|
+
import { generateText } from 'ai';
|
|
565
|
+
|
|
566
|
+
const { text, sources, providerMetadata } = await generateText({
|
|
567
|
+
model: google('gemini-2.5-flash'),
|
|
568
|
+
prompt: `Based on the document: https://ai.google.dev/gemini-api/docs/url-context.
|
|
569
|
+
Answer this question: How many links we can consume in one request?`,
|
|
570
|
+
tools: {
|
|
571
|
+
url_context: google.tools.urlContext({}),
|
|
572
|
+
},
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
const metadata = providerMetadata?.google as
|
|
576
|
+
| GoogleGenerativeAIProviderMetadata
|
|
577
|
+
| undefined;
|
|
578
|
+
const groundingMetadata = metadata?.groundingMetadata;
|
|
579
|
+
const urlContextMetadata = metadata?.urlContextMetadata;
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
The URL context metadata includes detailed information about how the model used the URL context to generate the response. Here are the available fields:
|
|
583
|
+
|
|
584
|
+
- **`urlMetadata`** (`{ retrievedUrl: string; urlRetrievalStatus: string; }[] | null`)
|
|
585
|
+
|
|
586
|
+
- Array of URL context metadata
|
|
587
|
+
- Each object includes:
|
|
588
|
+
- **`retrievedUrl`**: The URL of the context
|
|
589
|
+
- **`urlRetrievalStatus`**: The status of the URL retrieval
|
|
590
|
+
|
|
591
|
+
Example response:
|
|
592
|
+
|
|
593
|
+
```json
|
|
594
|
+
{
|
|
595
|
+
"urlMetadata": [
|
|
596
|
+
{
|
|
597
|
+
"retrievedUrl": "https://ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai",
|
|
598
|
+
"urlRetrievalStatus": "URL_RETRIEVAL_STATUS_SUCCESS"
|
|
599
|
+
}
|
|
600
|
+
]
|
|
601
|
+
}
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
With the URL context tool, you will also get the `groundingMetadata`.
|
|
605
|
+
|
|
606
|
+
```json
|
|
607
|
+
"groundingMetadata": {
|
|
608
|
+
"groundingChunks": [
|
|
609
|
+
{
|
|
610
|
+
"web": {
|
|
611
|
+
"uri": "https://ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai",
|
|
612
|
+
"title": "Google Generative AI - AI SDK Providers"
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
],
|
|
616
|
+
"groundingSupports": [
|
|
617
|
+
{
|
|
618
|
+
"segment": {
|
|
619
|
+
"startIndex": 67,
|
|
620
|
+
"endIndex": 157,
|
|
621
|
+
"text": "**Installation**: Install the `@ai-sdk/google` module using your preferred package manager"
|
|
622
|
+
},
|
|
623
|
+
"groundingChunkIndices": [
|
|
624
|
+
0
|
|
625
|
+
]
|
|
626
|
+
},
|
|
627
|
+
]
|
|
628
|
+
}
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
<Note>You can add up to 20 URLs per request.</Note>
|
|
632
|
+
|
|
633
|
+
<Note>
|
|
634
|
+
The URL context tool is only supported for Gemini 2.0 Flash models and above.
|
|
635
|
+
Check the [supported models for URL context
|
|
636
|
+
tool](https://ai.google.dev/gemini-api/docs/url-context#supported-models).
|
|
637
|
+
</Note>
|
|
638
|
+
|
|
639
|
+
#### Combine URL Context with Search Grounding
|
|
640
|
+
|
|
641
|
+
You can combine the URL context tool with search grounding to provide the model with the latest information from the web.
|
|
642
|
+
|
|
643
|
+
```ts highlight="9-10"
|
|
644
|
+
import { google } from '@ai-sdk/google';
|
|
645
|
+
import { generateText } from 'ai';
|
|
646
|
+
|
|
647
|
+
const { text, sources, providerMetadata } = await generateText({
|
|
648
|
+
model: google('gemini-2.5-flash'),
|
|
649
|
+
prompt: `Based on this context: https://ai-sdk.dev/providers/ai-sdk-providers/google-generative-ai, tell me how to use Gemini with AI SDK.
|
|
650
|
+
Also, provide the latest news about AI SDK V5.`,
|
|
651
|
+
tools: {
|
|
652
|
+
google_search: google.tools.googleSearch({}),
|
|
653
|
+
url_context: google.tools.urlContext({}),
|
|
654
|
+
},
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
const metadata = providerMetadata?.google as
|
|
658
|
+
| GoogleGenerativeAIProviderMetadata
|
|
659
|
+
| undefined;
|
|
660
|
+
const groundingMetadata = metadata?.groundingMetadata;
|
|
661
|
+
const urlContextMetadata = metadata?.urlContextMetadata;
|
|
662
|
+
```
|
|
663
|
+
|
|
664
|
+
### Google Maps Grounding
|
|
665
|
+
|
|
666
|
+
With [Google Maps grounding](https://ai.google.dev/gemini-api/docs/maps-grounding),
|
|
667
|
+
the model has access to Google Maps data for location-aware responses. This enables providing local data and geospatial context, such as finding nearby restaurants.
|
|
668
|
+
|
|
669
|
+
```ts highlight="7-16"
|
|
670
|
+
import { google } from '@ai-sdk/google';
|
|
671
|
+
import { GoogleGenerativeAIProviderMetadata } from '@ai-sdk/google';
|
|
672
|
+
import { generateText } from 'ai';
|
|
673
|
+
|
|
674
|
+
const { text, sources, providerMetadata } = await generateText({
|
|
675
|
+
model: google('gemini-2.5-flash'),
|
|
676
|
+
tools: {
|
|
677
|
+
google_maps: google.tools.googleMaps({}),
|
|
678
|
+
},
|
|
679
|
+
providerOptions: {
|
|
680
|
+
google: {
|
|
681
|
+
retrievalConfig: {
|
|
682
|
+
latLng: { latitude: 34.090199, longitude: -117.881081 },
|
|
683
|
+
},
|
|
684
|
+
},
|
|
685
|
+
},
|
|
686
|
+
prompt:
|
|
687
|
+
'What are the best Italian restaurants within a 15-minute walk from here?',
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
const metadata = providerMetadata?.google as
|
|
691
|
+
| GoogleGenerativeAIProviderMetadata
|
|
692
|
+
| undefined;
|
|
693
|
+
const groundingMetadata = metadata?.groundingMetadata;
|
|
694
|
+
```
|
|
695
|
+
|
|
696
|
+
The optional `retrievalConfig.latLng` provider option provides location context for queries about nearby places. This configuration applies to any grounding tools that support location context, including Google Maps and Google Search.
|
|
697
|
+
|
|
698
|
+
When Google Maps grounding is enabled, the model's response will include sources pointing to Google Maps URLs. The grounding metadata includes `maps` chunks with place information:
|
|
699
|
+
|
|
700
|
+
```json
|
|
701
|
+
{
|
|
702
|
+
"groundingMetadata": {
|
|
703
|
+
"groundingChunks": [
|
|
704
|
+
{
|
|
705
|
+
"maps": {
|
|
706
|
+
"uri": "https://maps.google.com/?cid=12345",
|
|
707
|
+
"title": "Restaurant Name",
|
|
708
|
+
"placeId": "places/ChIJ..."
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
]
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
```
|
|
715
|
+
|
|
716
|
+
<Note>Google Maps grounding is supported on Gemini 2.0 and newer models.</Note>
|
|
717
|
+
|
|
718
|
+
### RAG Engine Grounding
|
|
719
|
+
|
|
720
|
+
With [RAG Engine Grounding](https://cloud.google.com/vertex-ai/generative-ai/docs/rag-engine/use-vertexai-search#generate-content-using-gemini-api),
|
|
721
|
+
the model has access to your custom knowledge base using the Vertex RAG Engine.
|
|
722
|
+
This enables the model to provide answers based on your specific data sources and documents.
|
|
723
|
+
|
|
724
|
+
<Note>
|
|
725
|
+
RAG Engine Grounding is only supported with Vertex Gemini models. You must use
|
|
726
|
+
the Google Vertex provider (`@ai-sdk/google-vertex`) instead of the standard
|
|
727
|
+
Google provider (`@ai-sdk/google`) to use this feature.
|
|
728
|
+
</Note>
|
|
729
|
+
|
|
730
|
+
```ts highlight="8,17-20"
|
|
731
|
+
import { createVertex } from '@ai-sdk/google-vertex';
|
|
732
|
+
import { GoogleGenerativeAIProviderMetadata } from '@ai-sdk/google';
|
|
733
|
+
import { generateText } from 'ai';
|
|
734
|
+
|
|
735
|
+
const vertex = createVertex({
|
|
736
|
+
project: 'my-project',
|
|
737
|
+
location: 'us-central1',
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
const { text, sources, providerMetadata } = await generateText({
|
|
741
|
+
model: vertex('gemini-2.5-flash'),
|
|
742
|
+
tools: {
|
|
743
|
+
vertex_rag_store: vertex.tools.vertexRagStore({
|
|
744
|
+
ragCorpus:
|
|
745
|
+
'projects/my-project/locations/us-central1/ragCorpora/my-rag-corpus',
|
|
746
|
+
topK: 5,
|
|
747
|
+
}),
|
|
748
|
+
},
|
|
749
|
+
prompt:
|
|
750
|
+
'What are the key features of our product according to our documentation?',
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
// access the grounding metadata. Casting to the provider metadata type
|
|
754
|
+
// is optional but provides autocomplete and type safety.
|
|
755
|
+
const metadata = providerMetadata?.google as
|
|
756
|
+
| GoogleGenerativeAIProviderMetadata
|
|
757
|
+
| undefined;
|
|
758
|
+
const groundingMetadata = metadata?.groundingMetadata;
|
|
759
|
+
const safetyRatings = metadata?.safetyRatings;
|
|
760
|
+
```
|
|
761
|
+
|
|
762
|
+
When RAG Engine Grounding is enabled, the model will include sources from your RAG corpus in the response.
|
|
763
|
+
|
|
764
|
+
Additionally, the grounding metadata includes detailed information about how RAG results were used to ground the model's response. Here are the available fields:
|
|
765
|
+
|
|
766
|
+
- **`groundingChunks`** (Array of chunk objects | null)
|
|
767
|
+
|
|
768
|
+
- Contains the retrieved context chunks from your RAG corpus
|
|
769
|
+
- Each chunk includes:
|
|
770
|
+
- **`retrievedContext`**: Information about the retrieved context
|
|
771
|
+
- `uri`: The URI or identifier of the source document
|
|
772
|
+
- `title`: The title of the source document (optional)
|
|
773
|
+
- `text`: The actual text content of the chunk
|
|
774
|
+
|
|
775
|
+
- **`groundingSupports`** (Array of support objects | null)
|
|
776
|
+
|
|
777
|
+
- Contains details about how specific response parts are supported by RAG results
|
|
778
|
+
- Each support object includes:
|
|
779
|
+
- **`segment`**: Information about the grounded text segment
|
|
780
|
+
- `text`: The actual text segment
|
|
781
|
+
- `startIndex`: Starting position in the response
|
|
782
|
+
- `endIndex`: Ending position in the response
|
|
783
|
+
- **`groundingChunkIndices`**: References to supporting RAG result chunks
|
|
784
|
+
- **`confidenceScores`**: Confidence scores (0-1) for each supporting chunk
|
|
785
|
+
|
|
786
|
+
Example response:
|
|
787
|
+
|
|
788
|
+
```json
|
|
789
|
+
{
|
|
790
|
+
"groundingMetadata": {
|
|
791
|
+
"groundingChunks": [
|
|
792
|
+
{
|
|
793
|
+
"retrievedContext": {
|
|
794
|
+
"uri": "gs://my-bucket/docs/product-guide.pdf",
|
|
795
|
+
"title": "Product User Guide",
|
|
796
|
+
"text": "Our product includes advanced AI capabilities, real-time processing, and enterprise-grade security features."
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
],
|
|
800
|
+
"groundingSupports": [
|
|
801
|
+
{
|
|
802
|
+
"segment": {
|
|
803
|
+
"startIndex": 0,
|
|
804
|
+
"endIndex": 45,
|
|
805
|
+
"text": "Our product includes advanced AI capabilities and real-time processing."
|
|
806
|
+
},
|
|
807
|
+
"groundingChunkIndices": [0],
|
|
808
|
+
"confidenceScores": [0.95]
|
|
809
|
+
}
|
|
810
|
+
]
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
```
|
|
814
|
+
|
|
815
|
+
#### Configuration Options
|
|
816
|
+
|
|
817
|
+
The `vertexRagStore` tool accepts the following configuration options:
|
|
818
|
+
|
|
819
|
+
- **`ragCorpus`** (`string`, required)
|
|
820
|
+
|
|
821
|
+
- The RagCorpus resource name in the format: `projects/{project}/locations/{location}/ragCorpora/{rag_corpus}`
|
|
822
|
+
- This identifies your specific RAG corpus to search against
|
|
823
|
+
|
|
824
|
+
- **`topK`** (`number`, optional)
|
|
825
|
+
|
|
826
|
+
- The number of top contexts to retrieve from your RAG corpus
|
|
827
|
+
- Defaults to the corpus configuration if not specified
|
|
828
|
+
|
|
829
|
+
### Image Outputs
|
|
830
|
+
|
|
831
|
+
Gemini models with image generation capabilities (`gemini-2.5-flash-image-preview`) support image generation. Images are exposed as files in the response.
|
|
832
|
+
|
|
833
|
+
```ts
|
|
834
|
+
import { google } from '@ai-sdk/google';
|
|
835
|
+
import { generateText } from 'ai';
|
|
836
|
+
|
|
837
|
+
const result = await generateText({
|
|
838
|
+
model: google('gemini-2.5-flash-image-preview'),
|
|
839
|
+
prompt:
|
|
840
|
+
'Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme',
|
|
841
|
+
});
|
|
842
|
+
|
|
843
|
+
for (const file of result.files) {
|
|
844
|
+
if (file.mediaType.startsWith('image/')) {
|
|
845
|
+
console.log('Generated image:', file);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
```
|
|
849
|
+
|
|
850
|
+
### Safety Ratings
|
|
851
|
+
|
|
852
|
+
The safety ratings provide insight into the safety of the model's response.
|
|
853
|
+
See [Google AI documentation on safety settings](https://ai.google.dev/gemini-api/docs/safety-settings).
|
|
854
|
+
|
|
855
|
+
Example response excerpt:
|
|
856
|
+
|
|
857
|
+
```json
|
|
858
|
+
{
|
|
859
|
+
"safetyRatings": [
|
|
860
|
+
{
|
|
861
|
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
|
862
|
+
"probability": "NEGLIGIBLE",
|
|
863
|
+
"probabilityScore": 0.11027937,
|
|
864
|
+
"severity": "HARM_SEVERITY_LOW",
|
|
865
|
+
"severityScore": 0.28487435
|
|
866
|
+
},
|
|
867
|
+
{
|
|
868
|
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
|
869
|
+
"probability": "HIGH",
|
|
870
|
+
"blocked": true,
|
|
871
|
+
"probabilityScore": 0.95422274,
|
|
872
|
+
"severity": "HARM_SEVERITY_MEDIUM",
|
|
873
|
+
"severityScore": 0.43398145
|
|
874
|
+
},
|
|
875
|
+
{
|
|
876
|
+
"category": "HARM_CATEGORY_HARASSMENT",
|
|
877
|
+
"probability": "NEGLIGIBLE",
|
|
878
|
+
"probabilityScore": 0.11085559,
|
|
879
|
+
"severity": "HARM_SEVERITY_NEGLIGIBLE",
|
|
880
|
+
"severityScore": 0.19027223
|
|
881
|
+
},
|
|
882
|
+
{
|
|
883
|
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
|
884
|
+
"probability": "NEGLIGIBLE",
|
|
885
|
+
"probabilityScore": 0.22901751,
|
|
886
|
+
"severity": "HARM_SEVERITY_NEGLIGIBLE",
|
|
887
|
+
"severityScore": 0.09089675
|
|
888
|
+
}
|
|
889
|
+
]
|
|
890
|
+
}
|
|
891
|
+
```
|
|
892
|
+
|
|
893
|
+
### Troubleshooting
|
|
894
|
+
|
|
895
|
+
#### Schema Limitations
|
|
896
|
+
|
|
897
|
+
The Google Generative AI API uses a subset of the OpenAPI 3.0 schema,
|
|
898
|
+
which does not support features such as unions.
|
|
899
|
+
The errors that you get in this case look like this:
|
|
900
|
+
|
|
901
|
+
`GenerateContentRequest.generation_config.response_schema.properties[occupation].type: must be specified`
|
|
902
|
+
|
|
903
|
+
By default, structured outputs are enabled (and for tool calling they are required).
|
|
904
|
+
You can disable structured outputs for object generation as a workaround:
|
|
905
|
+
|
|
906
|
+
```ts highlight="3,8"
|
|
907
|
+
const { object } = await generateObject({
|
|
908
|
+
model: google('gemini-2.5-flash'),
|
|
909
|
+
providerOptions: {
|
|
910
|
+
google: {
|
|
911
|
+
structuredOutputs: false,
|
|
912
|
+
},
|
|
913
|
+
},
|
|
914
|
+
schema: z.object({
|
|
915
|
+
name: z.string(),
|
|
916
|
+
age: z.number(),
|
|
917
|
+
contact: z.union([
|
|
918
|
+
z.object({
|
|
919
|
+
type: z.literal('email'),
|
|
920
|
+
value: z.string(),
|
|
921
|
+
}),
|
|
922
|
+
z.object({
|
|
923
|
+
type: z.literal('phone'),
|
|
924
|
+
value: z.string(),
|
|
925
|
+
}),
|
|
926
|
+
]),
|
|
927
|
+
}),
|
|
928
|
+
prompt: 'Generate an example person for testing.',
|
|
929
|
+
});
|
|
930
|
+
```
|
|
931
|
+
|
|
932
|
+
The following Zod features are known to not work with Google Generative AI:
|
|
933
|
+
|
|
934
|
+
- `z.union`
|
|
935
|
+
- `z.record`
|
|
936
|
+
|
|
937
|
+
### Model Capabilities
|
|
938
|
+
|
|
939
|
+
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming | Google Search | URL Context |
|
|
940
|
+
| ------------------------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
|
|
941
|
+
| `gemini-3-pro-preview` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
942
|
+
| `gemini-2.5-pro` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
943
|
+
| `gemini-2.5-flash` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
944
|
+
| `gemini-2.5-flash-lite` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
945
|
+
| `gemini-2.5-flash-lite-preview-06-17` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
946
|
+
| `gemini-2.0-flash` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
947
|
+
| `gemini-1.5-pro` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
948
|
+
| `gemini-1.5-pro-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
949
|
+
| `gemini-1.5-flash` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
950
|
+
| `gemini-1.5-flash-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
951
|
+
| `gemini-1.5-flash-8b` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
952
|
+
| `gemini-1.5-flash-8b-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
|
|
953
|
+
|
|
954
|
+
<Note>
|
|
955
|
+
The table above lists popular models. Please see the [Google Generative AI
|
|
956
|
+
docs](https://ai.google.dev/gemini-api/docs/models/) for a full list of
|
|
957
|
+
available models. The table above lists popular models. You can also pass any
|
|
958
|
+
available provider model ID as a string if needed.
|
|
959
|
+
</Note>
|
|
960
|
+
|
|
961
|
+
## Gemma Models
|
|
962
|
+
|
|
963
|
+
You can use [Gemma models](https://deepmind.google/models/gemma/) with the Google Generative AI API.
|
|
964
|
+
|
|
965
|
+
Gemma models don't natively support the `systemInstruction` parameter, but the provider automatically handles system instructions by prepending them to the first user message. This allows you to use system instructions with Gemma models seamlessly:
|
|
966
|
+
|
|
967
|
+
```ts
|
|
968
|
+
import { google } from '@ai-sdk/google';
|
|
969
|
+
import { generateText } from 'ai';
|
|
970
|
+
|
|
971
|
+
const { text } = await generateText({
|
|
972
|
+
model: google('gemma-3-27b-it'),
|
|
973
|
+
system: 'You are a helpful assistant that responds concisely.',
|
|
974
|
+
prompt: 'What is machine learning?',
|
|
975
|
+
});
|
|
976
|
+
```
|
|
977
|
+
|
|
978
|
+
The system instruction is automatically formatted and included in the conversation, so Gemma models can follow the guidance without any additional configuration.
|
|
979
|
+
|
|
980
|
+
## Embedding Models
|
|
981
|
+
|
|
982
|
+
You can create models that call the [Google Generative AI embeddings API](https://ai.google.dev/gemini-api/docs/embeddings)
|
|
983
|
+
using the `.embedding()` factory method.
|
|
984
|
+
|
|
985
|
+
```ts
|
|
986
|
+
const model = google.embedding('gemini-embedding-001');
|
|
987
|
+
```
|
|
988
|
+
|
|
989
|
+
The Google Generative AI provider sends API calls to the right endpoint based on the type of embedding:
|
|
990
|
+
|
|
991
|
+
- **Single embeddings**: When embedding a single value with `embed()`, the provider uses the single `:embedContent` endpoint, which typically has higher rate limits compared to the batch endpoint.
|
|
992
|
+
- **Batch embeddings**: When embedding multiple values with `embedMany()` or multiple values in `embed()`, the provider uses the `:batchEmbedContents` endpoint.
|
|
993
|
+
|
|
994
|
+
Google Generative AI embedding models support aditional settings. You can pass them as an options argument:
|
|
995
|
+
|
|
996
|
+
```ts
|
|
997
|
+
import { google } from '@ai-sdk/google';
|
|
998
|
+
import { embed } from 'ai';
|
|
999
|
+
|
|
1000
|
+
const model = google.embedding('gemini-embedding-001');
|
|
1001
|
+
|
|
1002
|
+
const { embedding } = await embed({
|
|
1003
|
+
model,
|
|
1004
|
+
value: 'sunny day at the beach',
|
|
1005
|
+
providerOptions: {
|
|
1006
|
+
google: {
|
|
1007
|
+
outputDimensionality: 512, // optional, number of dimensions for the embedding
|
|
1008
|
+
taskType: 'SEMANTIC_SIMILARITY', // optional, specifies the task type for generating embeddings
|
|
1009
|
+
},
|
|
1010
|
+
},
|
|
1011
|
+
});
|
|
1012
|
+
```
|
|
1013
|
+
|
|
1014
|
+
The following optional provider options are available for Google Generative AI embedding models:
|
|
1015
|
+
|
|
1016
|
+
- **outputDimensionality**: _number_
|
|
1017
|
+
|
|
1018
|
+
Optional reduced dimension for the output embedding. If set, excessive values in the output embedding are truncated from the end.
|
|
1019
|
+
|
|
1020
|
+
- **taskType**: _string_
|
|
1021
|
+
|
|
1022
|
+
Optional. Specifies the task type for generating embeddings. Supported task types include:
|
|
1023
|
+
|
|
1024
|
+
- `SEMANTIC_SIMILARITY`: Optimized for text similarity.
|
|
1025
|
+
- `CLASSIFICATION`: Optimized for text classification.
|
|
1026
|
+
- `CLUSTERING`: Optimized for clustering texts based on similarity.
|
|
1027
|
+
- `RETRIEVAL_DOCUMENT`: Optimized for document retrieval.
|
|
1028
|
+
- `RETRIEVAL_QUERY`: Optimized for query-based retrieval.
|
|
1029
|
+
- `QUESTION_ANSWERING`: Optimized for answering questions.
|
|
1030
|
+
- `FACT_VERIFICATION`: Optimized for verifying factual information.
|
|
1031
|
+
- `CODE_RETRIEVAL_QUERY`: Optimized for retrieving code blocks based on natural language queries.
|
|
1032
|
+
|
|
1033
|
+
### Model Capabilities
|
|
1034
|
+
|
|
1035
|
+
| Model | Default Dimensions | Custom Dimensions |
|
|
1036
|
+
| ---------------------- | ------------------ | ------------------- |
|
|
1037
|
+
| `gemini-embedding-001` | 3072 | <Check size={18} /> |
|
|
1038
|
+
| `text-embedding-004` | 768 | <Check size={18} /> |
|
|
1039
|
+
|
|
1040
|
+
## Image Models
|
|
1041
|
+
|
|
1042
|
+
You can create [Imagen](https://ai.google.dev/gemini-api/docs/imagen) models that call the Google Generative AI API using the `.image()` factory method.
|
|
1043
|
+
For more on image generation with the AI SDK see [generateImage()](/docs/reference/ai-sdk-core/generate-image).
|
|
1044
|
+
|
|
1045
|
+
```ts
|
|
1046
|
+
import { google } from '@ai-sdk/google';
|
|
1047
|
+
import { generateImage } from 'ai';
|
|
1048
|
+
|
|
1049
|
+
const { image } = await generateImage({
|
|
1050
|
+
model: google.image('imagen-4.0-generate-001'),
|
|
1051
|
+
prompt: 'A futuristic cityscape at sunset',
|
|
1052
|
+
aspectRatio: '16:9',
|
|
1053
|
+
});
|
|
1054
|
+
```
|
|
1055
|
+
|
|
1056
|
+
Further configuration can be done using Google provider options. You can validate the provider options using the `GoogleGenerativeAIImageProviderOptions` type.
|
|
1057
|
+
|
|
1058
|
+
```ts
|
|
1059
|
+
import { google } from '@ai-sdk/google';
|
|
1060
|
+
import { GoogleGenerativeAIImageProviderOptions } from '@ai-sdk/google';
|
|
1061
|
+
import { generateImage } from 'ai';
|
|
1062
|
+
|
|
1063
|
+
const { image } = await generateImage({
|
|
1064
|
+
model: google.image('imagen-4.0-generate-001'),
|
|
1065
|
+
providerOptions: {
|
|
1066
|
+
google: {
|
|
1067
|
+
personGeneration: 'dont_allow',
|
|
1068
|
+
} satisfies GoogleGenerativeAIImageProviderOptions,
|
|
1069
|
+
},
|
|
1070
|
+
// ...
|
|
1071
|
+
});
|
|
1072
|
+
```
|
|
1073
|
+
|
|
1074
|
+
The following provider options are available:
|
|
1075
|
+
|
|
1076
|
+
- **personGeneration** `allow_adult` | `allow_all` | `dont_allow`
|
|
1077
|
+
Whether to allow person generation. Defaults to `allow_adult`.
|
|
1078
|
+
|
|
1079
|
+
<Note>
|
|
1080
|
+
Imagen models do not support the `size` parameter. Use the `aspectRatio`
|
|
1081
|
+
parameter instead.
|
|
1082
|
+
</Note>
|
|
1083
|
+
|
|
1084
|
+
#### Model Capabilities
|
|
1085
|
+
|
|
1086
|
+
| Model | Aspect Ratios |
|
|
1087
|
+
| ------------------------- | ------------------------- |
|
|
1088
|
+
| `imagen-4.0-generate-001` | 1:1, 3:4, 4:3, 9:16, 16:9 |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/google",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.12",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,11 +8,15 @@
|
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist/**/*",
|
|
11
|
+
"docs/**/*",
|
|
11
12
|
"src",
|
|
12
13
|
"CHANGELOG.md",
|
|
13
14
|
"README.md",
|
|
14
15
|
"internal.d.ts"
|
|
15
16
|
],
|
|
17
|
+
"directories": {
|
|
18
|
+
"doc": "./docs"
|
|
19
|
+
},
|
|
16
20
|
"exports": {
|
|
17
21
|
"./package.json": "./package.json",
|
|
18
22
|
".": {
|
|
@@ -28,16 +32,16 @@
|
|
|
28
32
|
}
|
|
29
33
|
},
|
|
30
34
|
"dependencies": {
|
|
31
|
-
"@ai-sdk/provider
|
|
32
|
-
"@ai-sdk/provider": "
|
|
35
|
+
"@ai-sdk/provider": "3.0.4",
|
|
36
|
+
"@ai-sdk/provider-utils": "4.0.8"
|
|
33
37
|
},
|
|
34
38
|
"devDependencies": {
|
|
35
39
|
"@types/node": "20.17.24",
|
|
36
40
|
"tsup": "^8",
|
|
37
41
|
"typescript": "5.8.3",
|
|
38
42
|
"zod": "3.25.76",
|
|
39
|
-
"@ai-
|
|
40
|
-
"@
|
|
43
|
+
"@vercel/ai-tsconfig": "0.0.0",
|
|
44
|
+
"@ai-sdk/test-server": "1.0.2"
|
|
41
45
|
},
|
|
42
46
|
"peerDependencies": {
|
|
43
47
|
"zod": "^3.25.76 || ^4.1.8"
|
|
@@ -62,7 +66,7 @@
|
|
|
62
66
|
"scripts": {
|
|
63
67
|
"build": "pnpm clean && tsup --tsconfig tsconfig.build.json",
|
|
64
68
|
"build:watch": "pnpm clean && tsup --watch",
|
|
65
|
-
"clean": "del-cli dist *.tsbuildinfo",
|
|
69
|
+
"clean": "del-cli dist docs *.tsbuildinfo",
|
|
66
70
|
"lint": "eslint \"./**/*.ts*\"",
|
|
67
71
|
"type-check": "tsc --build",
|
|
68
72
|
"prettier-check": "prettier --check \"./**/*.ts*\"",
|