@google/genai 1.33.0 → 1.35.0
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/README.md +233 -1
- package/dist/genai.d.ts +117 -201
- package/dist/index.cjs +304 -293
- package/dist/index.mjs +305 -294
- package/dist/index.mjs.map +1 -1
- package/dist/node/index.cjs +304 -296
- package/dist/node/index.mjs +305 -297
- package/dist/node/index.mjs.map +1 -1
- package/dist/node/node.d.ts +117 -201
- package/dist/web/index.mjs +305 -297
- package/dist/web/index.mjs.map +1 -1
- package/dist/web/web.d.ts +117 -201
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,12 @@ The Google Gen AI SDK is designed to work with Gemini 2.0+ features.
|
|
|
19
19
|
> **API Key Security:** Avoid exposing API keys in client-side code.
|
|
20
20
|
> Use server-side implementations in production environments.
|
|
21
21
|
|
|
22
|
+
## Code Generation
|
|
23
|
+
|
|
24
|
+
Generative models are often unaware of recent API and SDK updates and may suggest outdated or legacy code.
|
|
25
|
+
|
|
26
|
+
We recommend using our Code Generation instructions [`codegen_instructions.md`](https://raw.githubusercontent.com/googleapis/js-genai/refs/heads/main/codegen_instructions.md) when generating Google Gen AI SDK code to guide your model towards using the more recent SDK features. Copy and paste the instructions into your development environment to provide the model with the necessary context.
|
|
27
|
+
|
|
22
28
|
## Prerequisites
|
|
23
29
|
|
|
24
30
|
1. Node.js version 20 or later
|
|
@@ -363,6 +369,233 @@ async function main() {
|
|
|
363
369
|
main();
|
|
364
370
|
```
|
|
365
371
|
|
|
372
|
+
## Interactions (Preview)
|
|
373
|
+
|
|
374
|
+
> **Warning:** The Interactions API is in **Beta**. This is a preview of an
|
|
375
|
+
experimental feature. Features and schemas are subject to **breaking changes**.
|
|
376
|
+
|
|
377
|
+
The Interactions API is a unified interface for interacting with Gemini models
|
|
378
|
+
and agents. It simplifies state management, tool orchestration, and long-running
|
|
379
|
+
tasks.
|
|
380
|
+
|
|
381
|
+
See the [documentation site](https://ai.google.dev/gemini-api/docs/interactions)
|
|
382
|
+
for more details.
|
|
383
|
+
|
|
384
|
+
### Basic Interaction
|
|
385
|
+
|
|
386
|
+
```typescript
|
|
387
|
+
const interaction = await ai.interactions.create({
|
|
388
|
+
model: 'gemini-2.5-flash',
|
|
389
|
+
input: 'Hello, how are you?',
|
|
390
|
+
});
|
|
391
|
+
console.debug(interaction);
|
|
392
|
+
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
### Stateful Conversation
|
|
396
|
+
|
|
397
|
+
The Interactions API supports server-side state management. You can continue a
|
|
398
|
+
conversation by referencing the `previous_interaction_id`.
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
// 1. First turn
|
|
402
|
+
const interaction1 = await ai.interactions.create({
|
|
403
|
+
model: 'gemini-2.5-flash',
|
|
404
|
+
input: 'Hi, my name is Amir.',
|
|
405
|
+
});
|
|
406
|
+
console.debug(interaction1);
|
|
407
|
+
|
|
408
|
+
// 2. Second turn (passing previous_interaction_id)
|
|
409
|
+
const interaction2 = await ai.interactions.create({
|
|
410
|
+
model: 'gemini-2.5-flash',
|
|
411
|
+
input: 'What is my name?',
|
|
412
|
+
previous_interaction_id: interaction1.id,
|
|
413
|
+
});
|
|
414
|
+
console.debug(interaction2);
|
|
415
|
+
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### Agents (Deep Research)
|
|
419
|
+
|
|
420
|
+
You can use specialized agents like `deep-research-pro-preview-12-2025` for
|
|
421
|
+
complex tasks.
|
|
422
|
+
|
|
423
|
+
```typescript
|
|
424
|
+
function sleep(ms: number): Promise<void> {
|
|
425
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// 1. Start the Deep Research Agent
|
|
429
|
+
const initialInteraction = await ai.interactions.create({
|
|
430
|
+
input:
|
|
431
|
+
'Research the history of the Google TPUs with a focus on 2025 and 2026.',
|
|
432
|
+
agent: 'deep-research-pro-preview-12-2025',
|
|
433
|
+
background: true,
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
console.log(`Research started. Interaction ID: ${initialInteraction.id}`);
|
|
437
|
+
|
|
438
|
+
// 2. Poll for results
|
|
439
|
+
while (true) {
|
|
440
|
+
const interaction = await ai.interactions.get(initialInteraction.id);
|
|
441
|
+
console.log(`Status: ${interaction.status}`);
|
|
442
|
+
|
|
443
|
+
if (interaction.status === 'completed') {
|
|
444
|
+
console.debug('\nFinal Report:\n', interaction.outputs);
|
|
445
|
+
break;
|
|
446
|
+
} else if (['failed', 'cancelled'].includes(interaction.status)) {
|
|
447
|
+
console.log(`Failed with status: ${interaction.status}`);
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
await sleep(10000); // Sleep for 10 seconds
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
### Multimodal Input
|
|
457
|
+
|
|
458
|
+
You can provide multimodal data (text, images, audio, etc.) in the input list.
|
|
459
|
+
|
|
460
|
+
```typescript
|
|
461
|
+
import base64
|
|
462
|
+
|
|
463
|
+
// Assuming you have a base64 string
|
|
464
|
+
// const base64Image = ...;
|
|
465
|
+
|
|
466
|
+
const interaction = await ai.interactions.create({
|
|
467
|
+
model: 'gemini-2.5-flash',
|
|
468
|
+
input: [
|
|
469
|
+
{ type: 'text', text: 'Describe the image.' },
|
|
470
|
+
{ type: 'image', data: base64Image, mime_type: 'image/png' },
|
|
471
|
+
],
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
console.debug(interaction);
|
|
475
|
+
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
### Function Calling
|
|
479
|
+
|
|
480
|
+
You can define custom functions for the model to use. The Interactions API
|
|
481
|
+
handles the tool selection, and you provide the execution result back to the
|
|
482
|
+
model.
|
|
483
|
+
|
|
484
|
+
```typescript
|
|
485
|
+
// 1. Define the tool
|
|
486
|
+
const getWeather = (location: string) => {
|
|
487
|
+
/* Gets the weather for a given location. */
|
|
488
|
+
return `The weather in ${location} is sunny.`;
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// 2. Send the request with tools
|
|
492
|
+
let interaction = await ai.interactions.create({
|
|
493
|
+
model: 'gemini-2.5-flash',
|
|
494
|
+
input: 'What is the weather in Mountain View, CA?',
|
|
495
|
+
tools: [
|
|
496
|
+
{
|
|
497
|
+
type: 'function',
|
|
498
|
+
name: 'get_weather',
|
|
499
|
+
description: 'Gets the weather for a given location.',
|
|
500
|
+
parameters: {
|
|
501
|
+
type: 'object',
|
|
502
|
+
properties: {
|
|
503
|
+
location: {
|
|
504
|
+
type: 'string',
|
|
505
|
+
description: 'The city and state, e.g. San Francisco, CA',
|
|
506
|
+
},
|
|
507
|
+
},
|
|
508
|
+
required: ['location'],
|
|
509
|
+
},
|
|
510
|
+
},
|
|
511
|
+
],
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
// 3. Handle the tool call
|
|
515
|
+
for (const output of interaction.outputs!) {
|
|
516
|
+
if (output.type === 'function_call') {
|
|
517
|
+
console.log(
|
|
518
|
+
`Tool Call: ${output.name}(${JSON.stringify(output.arguments)})`);
|
|
519
|
+
|
|
520
|
+
// Execute your actual function here
|
|
521
|
+
// Note: ensure arguments match your function signature
|
|
522
|
+
const result = getWeather(JSON.stringify(output.arguments.location));
|
|
523
|
+
|
|
524
|
+
// Send result back to the model
|
|
525
|
+
interaction = await ai.interactions.create({
|
|
526
|
+
model: 'gemini-2.5-flash',
|
|
527
|
+
previous_interaction_id: interaction.id,
|
|
528
|
+
input: [
|
|
529
|
+
{
|
|
530
|
+
type: 'function_result',
|
|
531
|
+
name: output.name,
|
|
532
|
+
call_id: output.id,
|
|
533
|
+
result: result,
|
|
534
|
+
},
|
|
535
|
+
],
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
console.debug(`Response: ${JSON.stringify(interaction)}`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
```
|
|
543
|
+
|
|
544
|
+
### Built-in Tools
|
|
545
|
+
You can also use Google's built-in tools, such as **Google Search** or **Code
|
|
546
|
+
Execution**.
|
|
547
|
+
|
|
548
|
+
#### Grounding with Google Search
|
|
549
|
+
|
|
550
|
+
```typescript
|
|
551
|
+
const interaction = await ai.interactions.create({
|
|
552
|
+
model: 'gemini-2.5-flash',
|
|
553
|
+
input: 'Who won the last Super Bowl',
|
|
554
|
+
tools: [{ type: 'google_search' }],
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
console.debug(interaction);
|
|
558
|
+
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
#### Code Execution
|
|
562
|
+
|
|
563
|
+
```typescript
|
|
564
|
+
const interaction = await ai.interactions.create({
|
|
565
|
+
model: 'gemini-2.5-flash',
|
|
566
|
+
input: 'Calculate the 50th Fibonacci number.',
|
|
567
|
+
tools: [{ type: 'code_execution' }],
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
console.debug(interaction);
|
|
571
|
+
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
### Multimodal Output
|
|
575
|
+
|
|
576
|
+
The Interactions API can generate multimodal outputs, such as images. You must
|
|
577
|
+
specify the `response_modalities`.
|
|
578
|
+
|
|
579
|
+
```typescript
|
|
580
|
+
import * as fs from 'fs';
|
|
581
|
+
|
|
582
|
+
const interaction = await ai.interactions.create({
|
|
583
|
+
model: 'gemini-3-pro-image-preview',
|
|
584
|
+
input: 'Generate an image of a futuristic city.',
|
|
585
|
+
response_modalities: ['image'],
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
for (const output of interaction.outputs!) {
|
|
589
|
+
if (output.type === 'image') {
|
|
590
|
+
console.log(`Generated image with mime_type: ${output.mime_type}`);
|
|
591
|
+
// Save the image
|
|
592
|
+
fs.writeFileSync(
|
|
593
|
+
'generated_city.png', Buffer.from(output.data!, 'base64'));
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
```
|
|
598
|
+
|
|
366
599
|
## How is this different from the other Google AI SDKs
|
|
367
600
|
This SDK (`@google/genai`) is Google Deepmind’s "vanilla" SDK for its generative
|
|
368
601
|
AI offerings, and is where Google Deepmind adds new AI features.
|
|
@@ -374,4 +607,3 @@ be targeting specific project environments (like Firebase).
|
|
|
374
607
|
|
|
375
608
|
The `@google/generative_language` and `@google-cloud/vertexai` SDKs are previous
|
|
376
609
|
iterations of this SDK and are no longer receiving new Gemini 2.0+ features.
|
|
377
|
-
|