Good Example
Parent State: {text}
{/* Pass state and handler down as props */}
);
}
export default ParentComponent;
```
**Gemini API guidance**
# @google/genai Coding Guidelines
This library is sometimes called:
- Google Gemini API
- Google GenAI API
- Google GenAI SDK
- Gemini API
- @google/genai
The Google GenAI SDK can be used to call Gemini models.
Do *not* use or import the types below from `@google/genai`; these are deprecated APIs and no longer work.
- **Incorrect** `GoogleGenerativeAI`
- **Incorrect** `google.generativeai`
- **Incorrect** `models.create`
- **Incorrect** `ai.models.create`
- **Incorrect** `models.getGenerativeModel`
- **Incorrect** `ai.models.getModel`
- **Incorrect** `ai.models['model_name']`
- **Incorrect** `generationConfig`
- **Incorrect** `GoogleGenAIError`
- **Incorrect** `GenerateContentResult`; **Correct** `GenerateContentResponse`.
- **Incorrect** `GenerateContentRequest`; **Correct** `GenerateContentParameters`.
When using generate content for text answers, do *not* define the model first and call generate content later. You must use `ai.models.generateContent` to query GenAI with both the model name and prompt.
## Initialization
- Always use `const ai = new GoogleGenAI({apiKey: process.env.API_KEY});`.
- **Incorrect** `const ai = new GoogleGenAI(process.env.API_KEY);` // Must use a named parameter.
## API Key
- The API key **must** be obtained **exclusively** from the environment variable `process.env.API_KEY`. Assume this variable is pre-configured, valid, and accessible in the execution context where the API client is initialized.
- Use this `process.env.API_KEY` string **directly** when initializing the `@google/genai` client instance (must use `new GoogleGenAI({ apiKey: process.env.API_KEY })`).
- Do **not** generate any UI elements (input fields, forms, prompts, configuration sections) or code snippets for entering or managing the API key. Do **not** define `process.env` or request that the user update the API_KEY in the code. The key's availability is handled externally and is a hard requirement. The application **must not** ask the user for it under any circumstances.
## Model
- If the user provides a full model name with hyphens, version, and date (e.g., `gemini-2.5-flash-preview-09-2025`), use it directly.
- If the user provides a common name or alias, use the following full model name.
- gemini flash: 'gemini-flash-latest'
- gemini lite or flash lite: 'gemini-flash-lite-latest'
- gemini pro: 'gemini-2.5-pro'
- nano banana or gemini flash image: 'gemini-2.5-flash-image'
- native audio or gemini flash audio: 'gemini-2.5-flash-native-audio-preview-09-2025'
- gemini tts or gemini text-to-speech: 'gemini-2.5-flash-preview-tts'
- Veo or Veo fast: 'veo-3.1-fast-generate-preview'
- If the user does not specify any model, select the following model based on the task type.
- Basic Text Tasks (e.g., summarization, proofreading, and simple Q&A): 'gemini-2.5-flash'
- Complex Text Tasks (e.g., advanced reasoning, coding, math, and STEM): 'gemini-2.5-pro'
- High-Quality Image Generation Tasks: 'imagen-4.0-generate-001'
- General Image Generation and Editing Tasks: 'gemini-2.5-flash-image'
- High-Quality Video Generation Tasks: 'veo-3.1-generate-preview'
- General Video Generation Tasks: 'veo-3.1-fast-generate-preview'
- Real-time audio & video conversation tasks: 'gemini-2.5-flash-native-audio-preview-09-2025'
- Text-to-speech tasks: 'gemini-2.5-flash-preview-tts'
- Do not use the following deprecated models.
- **Prohibited:** `gemini-1.5-flash`
- **Prohibited:** `gemini-1.5-pro`
- **Prohibited:** `gemini-pro`
## Import
- Always use `import {GoogleGenAI} from "@google/genai";`.
- **Prohibited:** `import { GoogleGenerativeAI } from "@google/genai";`
- **Prohibited:** `import type { GoogleGenAI} from "@google/genai";`
- **Prohibited:** `declare var GoogleGenAI`.
## Generate Content
Generate a response from the model.
```ts
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'why is the sky blue?',
});
console.log(response.text);
```
Generate content with multiple parts, for example, by sending an image and a text prompt to the model.
```ts
import { GoogleGenAI, GenerateContentResponse } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const imagePart = {
inlineData: {
mimeType: 'image/png', // Could be any other IANA standard MIME type for the source data.
data: base64EncodeString, // base64 encoded string
},
};
const textPart = {
text: promptString // text prompt
};
const response: GenerateContentResponse = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: { parts: [imagePart, textPart] },
});
```
---
## Extracting Text Output from `GenerateContentResponse`
When you use `ai.models.generateContent`, it returns a `GenerateContentResponse` object.
The simplest and most direct way to get the generated text content is by accessing the `.text` property on this object.
### Correct Method
- The `GenerateContentResponse` object has a property called `text` that directly provides the string output.
```ts
import { GoogleGenAI, GenerateContentResponse } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response: GenerateContentResponse = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'why is the sky blue?',
});
const text = response.text;
console.log(text);
```
### Incorrect Methods to Avoid
- **Incorrect:**`const text = response?.response?.text?;`
- **Incorrect:**`const text = response?.response?.text();`
- **Incorrect:**`const text = response?.response?.text?.()?.trim();`
- **Incorrect:**`const response = response?.response; const text = response?.text();`
- **Incorrect:** `const json = response.candidates?.[0]?.content?.parts?.[0]?.json;`
## System Instruction and Other Model Configs
Generate a response with a system instruction and other model configs.
```ts
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Tell me a story.",
config: {
systemInstruction: "You are a storyteller for kids under 5 years old.",
topK: 64,
topP: 0.95,
temperature: 1,
responseMimeType: "application/json",
seed: 42,
},
});
console.log(response.text);
```
## Max Output Tokens Config
`maxOutputTokens`: An optional config. It controls the maximum number of tokens the model can utilize for the request.
- Recommendation: Avoid setting this if not required to prevent the response from being blocked due to reaching max tokens.
- If you need to set it for the `gemini-2.5-flash` model, you must set a smaller `thinkingBudget` to reserve tokens for the final output.
**Correct Example for Setting `maxOutputTokens` and `thinkingBudget` Together**
```ts
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Tell me a story.",
config: {
// The effective token limit for the response is `maxOutputTokens` minus the `thinkingBudget`.
// In this case: 200 - 100 = 100 tokens available for the final response.
// Set both maxOutputTokens and thinkingConfig.thinkingBudget at the same time.
maxOutputTokens: 200,
thinkingConfig: { thinkingBudget: 100 },
},
});
console.log(response.text);
```
**Incorrect Example for Setting `maxOutputTokens` without `thinkingBudget`**
```ts
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Tell me a story.",
config: {
// Problem: The response will be empty since all the tokens are consumed by thinking.
// Fix: Add `thinkingConfig: { thinkingBudget: 25 }` to limit thinking usage.
maxOutputTokens: 50,
},
});
console.log(response.text);
```
## Thinking Config
- The Thinking Config is only available for the Gemini 2.5 series models. Do not use it with other models.
- The `thinkingBudget` parameter guides the model on the number of thinking tokens to use when generating a response.
A higher token count generally allows for more detailed reasoning, which can be beneficial for tackling more complex tasks.
The maximum thinking budget for 2.5 Pro is 32768, and for 2.5 Flash and Flash-Lite is 24576.
// Example code for max thinking budget.
```ts
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContent({
model: "gemini-2.5-pro",
contents: "Write Python code for a web application that visualizes real-time stock market data",
config: { thinkingConfig: { thinkingBudget: 32768 } } // max budget for 2.5-pro
});
console.log(response.text);
```
- If latency is more important, you can set a lower budget or disable thinking by setting `thinkingBudget` to 0.
// Example code for disabling thinking budget.
```ts
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "Provide a list of 3 famous physicists and their key contributions",
config: { thinkingConfig: { thinkingBudget: 0 } } // disable thinking
});
console.log(response.text);
```
- By default, you do not need to set `thinkingBudget`, as the model decides when and how much to think.
---
## JSON Response
Ask the model to return a response in JSON format.
The recommended way is to configure a `responseSchema` for the expected output.
See the available types below that can be used in the `responseSchema`.
```
export enum Type {
/**
* Not specified, should not be used.
*/
TYPE_UNSPECIFIED = 'TYPE_UNSPECIFIED',
/**
* OpenAPI string type
*/
STRING = 'STRING',
/**
* OpenAPI number type
*/
NUMBER = 'NUMBER',
/**
* OpenAPI integer type
*/
INTEGER = 'INTEGER',
/**
* OpenAPI boolean type
*/
BOOLEAN = 'BOOLEAN',
/**
* OpenAPI array type
*/
ARRAY = 'ARRAY',
/**
* OpenAPI object type
*/
OBJECT = 'OBJECT',
/**
* Null type
*/
NULL = 'NULL',
}
```
Type.OBJECT cannot be empty; it must contain other properties.
```ts
import { GoogleGenAI, Type } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: "List a few popular cookie recipes, and include the amounts of ingredients.",
config: {
responseMimeType: "application/json",
responseSchema: {
type: Type.ARRAY,
items: {
type: Type.OBJECT,
properties: {
recipeName: {
type: Type.STRING,
description: 'The name of the recipe.',
},
ingredients: {
type: Type.ARRAY,
items: {
type: Type.STRING,
},
description: 'The ingredients for the recipe.',
},
},
propertyOrdering: ["recipeName", "ingredients"],
},
},
},
});
let jsonStr = response.text.trim();
```
The `jsonStr` might look like this:
```
[
{
"recipeName": "Chocolate Chip Cookies",
"ingredients": [
"1 cup (2 sticks) unsalted butter, softened",
"3/4 cup granulated sugar",
"3/4 cup packed brown sugar",
"1 teaspoon vanilla extract",
"2 large eggs",
"2 1/4 cups all-purpose flour",
"1 teaspoon baking soda",
"1 teaspoon salt",
"2 cups chocolate chips"
]
},
...
]
```
---
## Function calling
To let Gemini to interact with external systems, you can provide `FunctionDeclaration` object as `tools`. The model can then return a structured `FunctionCall` object, asking you to call the function with the provided arguments.
```ts
import { FunctionDeclaration, GoogleGenAI, Type } from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
// Assuming you have defined a function `controlLight` which takes `brightness` and `colorTemperature` as input arguments.
const controlLightFunctionDeclaration: FunctionDeclaration = {
name: 'controlLight',
parameters: {
type: Type.OBJECT,
description: 'Set the brightness and color temperature of a room light.',
properties: {
brightness: {
type: Type.NUMBER,
description:
'Light level from 0 to 100. Zero is off and 100 is full brightness.',
},
colorTemperature: {
type: Type.STRING,
description:
'Color temperature of the light fixture such as `daylight`, `cool` or `warm`.',
},
},
required: ['brightness', 'colorTemperature'],
},
};
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'Dim the lights so the room feels cozy and warm.',
config: {
tools: [{functionDeclarations: [controlLightFunctionDeclaration]}], // You can pass multiple functions to the model.
},
});
console.debug(response.functionCalls);
```
the `response.functionCalls` might look like this:
```
[
{
args: { colorTemperature: 'warm', brightness: 25 },
name: 'controlLight',
id: 'functionCall-id-123',
}
]
```
You can then extract the arguments from the `FunctionCall` object and execute your `controlLight` function.
---
## Generate Content (Streaming)
Generate a response from the model in streaming mode.
```ts
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContentStream({
model: "gemini-2.5-flash",
contents: "Tell me a story in 300 words.",
});
for await (const chunk of response) {
console.log(chunk.text);
}
```
---
## Generate Images
Generate high-quality images with imagen.
- `aspectRatio`: Changes the aspect ratio of the generated image. Supported values are "1:1", "3:4", "4:3", "9:16", and "16:9". The default is "1:1".
```ts
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateImages({
model: 'imagen-4.0-generate-001',
prompt: 'A robot holding a red skateboard.',
config: {
numberOfImages: 1,
outputMimeType: 'image/jpeg',
aspectRatio: '1:1',
},
});
const base64ImageBytes: string = response.generatedImages[0].image.imageBytes;
const imageUrl = `data:image/png;base64,${base64ImageBytes}`;
```
Or you can generate a general image with `gemini-2.5-flash-image` (nano banana).
```ts
import { GoogleGenAI, Modality } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash-image',
contents: {
parts: [
{
text: 'A robot holding a red skateboard.',
},
],
},
config: {
responseModalities: [Modality.IMAGE], // Must be an array with a single `Modality.IMAGE` element.
},
});
for (const part of response.candidates[0].content.parts) {
if (part.inlineData) {
const base64ImageBytes: string = part.inlineData.data;
const imageUrl = `data:image/png;base64,${base64ImageBytes}`;
}
}
```
---
## Edit Images
Edit images from the model, you can prompt with text, images or a combination of both.
Do not add other configs except for the `responseModalities` config. The other configs are not supported in this model.
```ts
import { GoogleGenAI, Modality } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash-image',
contents: {
parts: [
{
inlineData: {
data: base64ImageData, // base64 encoded string
mimeType: mimeType, // IANA standard MIME type
},
},
{
text: 'can you add a llama next to the image',
},
],
},
config: {
responseModalities: [Modality.IMAGE], // Must be an array with a single `Modality.IMAGE` element.
},
});
for (const part of response.candidates[0].content.parts) {
if (part.inlineData) {
const base64ImageBytes: string = part.inlineData.data;
const imageUrl = `data:image/png;base64,${base64ImageBytes}`;
}
}
```
---
## Generate Speech
Transform text input into single-speaker or multi-speaker audio.
### Single speaker
```ts
import { GoogleGenAI, Modality } from "@google/genai";
const ai = new GoogleGenAI({});
const response = await ai.models.generateContent({
model: "gemini-2.5-flash-preview-tts",
contents: [{ parts: [{ text: 'Say cheerfully: Have a wonderful day!' }] }],
config: {
responseModalities: [Modality.AUDIO], // Must be an array with a single `Modality.AUDIO` element.
speechConfig: {
voiceConfig: {
prebuiltVoiceConfig: { voiceName: 'Kore' },
},
},
},
});
const outputAudioContext = new (window.AudioContext ||
window.webkitAudioContext)({sampleRate: 24000});
const outputNode = outputAudioContext.createGain();
const base64Audio = response.candidates?.[0]?.content?.parts?.[0]?.inlineData?.data;
const audioBuffer = await decodeAudioData(
decode(base64EncodedAudioString),
outputAudioContext,
24000,
1,
);
const source = outputAudioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(outputNode);
source.start();
```
### Multi-speakers
Use it when you need 2 speakers (the number of `speakerVoiceConfig` must equal 2)
```ts
const ai = new GoogleGenAI({});
const prompt = `TTS the following conversation between Joe and Jane:
Joe: How's it going today Jane?
Jane: Not too bad, how about you?`;
const response = await ai.models.generateContent({
model: "gemini-2.5-flash-preview-tts",
contents: [{ parts: [{ text: prompt }] }],
config: {
responseModalities: ['AUDIO'],
speechConfig: {
multiSpeakerVoiceConfig: {
speakerVoiceConfigs: [
{
speaker: 'Joe',
voiceConfig: {
prebuiltVoiceConfig: { voiceName: 'Kore' }
}
},
{
speaker: 'Jane',
voiceConfig: {
prebuiltVoiceConfig: { voiceName: 'Puck' }
}
}
]
}
}
}
});
const outputAudioContext = new (window.AudioContext ||
window.webkitAudioContext)({sampleRate: 24000});
const base64Audio = response.candidates?.[0]?.content?.parts?.[0]?.inlineData?.data;
const audioBuffer = await decodeAudioData(
decode(base64EncodedAudioString),
outputAudioContext,
24000,
1,
);
const source = outputAudioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(outputNode);
source.start();
```
### Audio Decoding
* Follow the existing example code from Live API `Audio Encoding & Decoding` section.
* The audio bytes returned by the API is raw PCM data. It is not a standard file format like `.wav` `.mpeg`, or `.mp3`, it contains no header information.
---
## Generate Videos
Generate a video from the model.
The aspect ratio can be `16:9` (landscape) or `9:16` (portrait), the resolution can be 720p or 1080p, and the number of videos must be 1.
Note: The video generation can take a few minutes. Create a set of clear and reassuring messages to display on the loading screen to improve the user experience.
```ts
let operation = await ai.models.generateVideos({
model: 'veo-3.1-fast-generate-preview',
prompt: 'A neon hologram of a cat driving at top speed',
config: {
numberOfVideos: 1,
resolution: '1080p', // Can be 720p or 1080p.
aspectRatio: '16:9', // Can be 16:9 (landscape) or 9:16 (portrait)
},
});
while (!operation.done) {
await new Promise(resolve => setTimeout(resolve, 10000));
operation = await ai.operations.getVideosOperation({operation: operation});
}
const downloadLink = operation.response?.generatedVideos?.[0]?.video?.uri;
// The response.body contains the MP4 bytes. You must append an API key when fetching from the download link.
const response = await fetch(`${downloadLink}&key=${process.env.API_KEY}`);
```
Generate a video with a text prompt and a starting image.
```ts
let operation = await ai.models.generateVideos({
model: 'veo-3.1-fast-generate-preview',
prompt: 'A neon hologram of a cat driving at top speed', // prompt is optional
image: {
imageBytes: base64EncodeString, // base64 encoded string
mimeType: 'image/png', // Could be any other IANA standard MIME type for the source data.
},
config: {
numberOfVideos: 1,
resolution: '720p',
aspectRatio: '9:16',
},
});
while (!operation.done) {
await new Promise(resolve => setTimeout(resolve, 10000));
operation = await ai.operations.getVideosOperation({operation: operation});
}
const downloadLink = operation.response?.generatedVideos?.[0]?.video?.uri;
// The response.body contains the MP4 bytes. You must append an API key when fetching from the download link.
const response = await fetch(`${downloadLink}&key=${process.env.API_KEY}`);
```
Generate a video with a starting and an ending image.
```ts
let operation = await ai.models.generateVideos({
model: 'veo-3.1-fast-generate-preview',
prompt: 'A neon hologram of a cat driving at top speed', // prompt is optional
image: {
imageBytes: base64EncodeString, // base64 encoded string
mimeType: 'image/png', // Could be any other IANA standard MIME type for the source data.
},
config: {
numberOfVideos: 1,
resolution: '720p',
lastFrame: {
imageBytes: base64EncodeString, // base64 encoded string
mimeType: 'image/png', // Could be any other IANA standard MIME type for the source data.
},
aspectRatio: '9:16',
},
});
while (!operation.done) {
await new Promise(resolve => setTimeout(resolve, 10000));
operation = await ai.operations.getVideosOperation({operation: operation});
}
const downloadLink = operation.response?.generatedVideos?.[0]?.video?.uri;
// The response.body contains the MP4 bytes. You must append an API key when fetching from the download link.
const response = await fetch(`${downloadLink}&key=${process.env.API_KEY}`);
```
Generate a video with multiple reference images (up to 3). For this feature, the model must be 'veo-3.1-generate-preview', the aspect ratio must be '16:9', and the resolution must be '720p'.
```ts
const referenceImagesPayload: VideoGenerationReferenceImage[] = [];
for (const img of refImages) {
referenceImagesPayload.push({
image: {
imageBytes: base64EncodeString, // base64 encoded string
mimeType: 'image/png', // Could be any other IANA standard MIME type for the source data.
},
referenceType: VideoGenerationReferenceType.ASSET,
});
}
let operation = await ai.models.generateVideos({
model: 'veo-3.1-generate-preview',
prompt: 'A video of this character, in this environment, using this item.', // prompt is required
config: {
numberOfVideos: 1,
referenceImages: referenceImagesPayload,
resolution: '720p',
aspectRatio: '16:9',
},
});
while (!operation.done) {
await new Promise(resolve => setTimeout(resolve, 10000));
operation = await ai.operations.getVideosOperation({operation: operation});
}
const downloadLink = operation.response?.generatedVideos?.[0]?.video?.uri;
// The response.body contains the MP4 bytes. You must append an API key when fetching from the download link.
const response = await fetch(`${downloadLink}&key=${process.env.API_KEY}`);
```
Extend a video by adding 7s at the end of it. The resolution must be '720p' and only 720p videos can be extended, must use the same aspect ratio as the previous video.
```ts
operation = await ai.models.generateVideos({
model: 'veo-3.1-generate-preview',
prompt: 'something unexpected happens', // mandatory
video: previousOperation.response?.generatedVideos?.[0]?.video, // The video from a previous generation
config: {
numberOfVideos: 1,
resolution: '720p',
aspectRatio: previousVideo?.aspectRatio, // Use the same aspect ratio
},
});
while (!operation.done) {
await new Promise(resolve => setTimeout(resolve, 5000));
operation = await ai.operations.getVideosOperation({operation: operation});
}
const downloadLink = operation.response?.generatedVideos?.[0]?.video?.uri;
// The response.body contains the MP4 bytes. You must append an API key when fetching from the download link.
const response = await fetch(`${downloadLink}&key=${process.env.API_KEY}`);
```
### API Key Selection
When using the Veo video generation models, users must select their own API key. This is a mandatory step before accessing the main app.
Use `await window.aistudio.hasSelectedApiKey()` to check whether an API key has been selected.
If not, add a button which calls `await window.aistudio.openSelectKey()` to open a dialog for the user to select their API key.
Assume `window.aistudio.hasSelectedApiKey()` and `window.aistudio.openSelectKey()` are pre-configured, valid, and accessible in the execution context.
Race condition:
* A race condition can occur where `hasSelectedApiKey()` may not immediately return true after the user selects a key after triggering `openSelectKey()`. To mitigate this, you can assume the key selection was successful after triggering `openSelectKey()`.
* If the request fails with an error message containing "Requested entity was not found.", reset the key selection state and prompt the user to select a key again via `openSelectKey()`.
* Create a new `GoogleGenAI` instance right before making an API call to ensure it always uses the most up-to-date API key from the dialog. Do not create `GoogleGenAI` when the component is first rendered.
Important:
* A link to the billing documentation (ai.google.dev/gemini-api/docs/billing) must be provided in the dialog.
* The selected API key is available via `process.env.API_KEY`. It is injected automatically, so you do not need to modify the API key code.
---
## Live
The Live API enables low-latency, real-time voice interactions with Gemini.
It can process continuous streams of audio or video input and returns human-like spoken
audio responses from the model, creating a natural conversational experience.
This API is primarily designed for audio-in (which can be supplemented with image frames) and audio-out conversations.
### Session Setup
Example code for session setup and audio streaming.
```ts
import {GoogleGenAI, LiveServerMessage, Modality, Blob} from '@google/genai';
// The `nextStartTime` variable acts as a cursor to track the end of the audio playback queue.
// Scheduling each new audio chunk to start at this time ensures smooth, gapless playback.
let nextStartTime = 0;
const inputAudioContext = new (window.AudioContext ||
window.webkitAudioContext)({sampleRate: 16000});
const outputAudioContext = new (window.AudioContext ||
window.webkitAudioContext)({sampleRate: 24000});
const inputNode = inputAudioContext.createGain();
const outputNode = outputAudioContext.createGain();
const sources = new Set