### Establish Asynchronous Connection to Gemini Live API Source: https://context7_llms This section provides code examples for establishing a basic asynchronous connection to the Gemini Live API. It shows how to initialize the client, specify the model, and configure response modalities. The examples cover both Python and JavaScript, demonstrating the initial setup required before sending content or interacting with the model. ```Python import asyncio from google import genai client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" config = {"response_modalities": ["TEXT"]} async def main(): async with client.aio.live.connect(model=model, config=config) as session: print("Session started") if __name__ == "__main__": asyncio.run(main()) ``` ```JavaScript import { GoogleGenAI, Modality } from '@google/genai'; const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" }); const model = 'gemini-2.0-flash-live-001'; const config = { responseModalities: [Modality.TEXT] }; async function main() { const session = await ai.live.connect({ model: model, callbacks: { onopen: function () { console.debug('Opened'); }, onmessage: function (message) { console.debug(message); }, onerror: function (e) { console.debug('Error:', e.message); }, onclose: function (e) { console.debug('Close:', e.reason); }, }, config: config, }); // Send content... session.close(); } main(); ``` -------------------------------- ### Google AI Studio Documentation Source: https://ai.google.dev/gemini-api/docs/ephemeral-tokens Quickstart guides, LearnLM, troubleshooting, and Google Workspace integration for Google AI Studio. ```APIDOC AI Studio: Google AI Studio quickstart: /gemini-api/docs/ai-studio-quickstart LearnLM: /gemini-api/docs/learnlm AI Studio troubleshooting: /gemini-api/docs/troubleshoot-ai-studio Google Workspace: /gemini-api/docs/workspace ``` -------------------------------- ### Send Real-time PCM Audio to AI Model (Python) Source: https://ai.google.dev/gemini-api/docs/live-guide This Python example demonstrates how to establish a live connection with an AI model, send a PCM audio file as real-time input, and receive text responses. It also shows how to signal the end of an audio stream if it gets paused. ```Python # example audio file to try: # URL = "https://storage.googleapis.com/generativeai-downloads/data/hello_are_you_there.pcm" # !wget -q $URL -O sample.pcm import asyncio from pathlib import Path from google import genai from google.genai import types client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" config = {"response_modalities": ["TEXT"]} async def main(): async with client.aio.live.connect(model=model, config=config) as session: audio_bytes = Path("sample.pcm").read_bytes() await session.send_realtime_input( audio=types.Blob(data=audio_bytes, mime_type="audio/pcm;rate=16000") ) # if stream gets paused, send: # await session.send_realtime_input(audio_stream_end=True) async for response in session.receive(): if response.text is not None: print(response.text) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Gemini API Guides Source: https://ai.google.dev/gemini-api/docs/ephemeral-tokens Guides for prompt engineering strategies. ```APIDOC Guides: Prompt engineering: /gemini-api/docs/prompting-strategies ``` -------------------------------- ### Gemini Live API Documentation Source: https://ai.google.dev/gemini-api/docs/ephemeral-tokens Documentation for the Gemini Live API, covering getting started, capabilities, tool use, session management, and ephemeral tokens. ```APIDOC Live API: Get started: /gemini-api/docs/live Capabilities: /gemini-api/docs/live-guide Tool use: /gemini-api/docs/live-tools Session management: /gemini-api/docs/live-session Ephemeral tokens: /gemini-api/docs/ephemeral-tokens ``` -------------------------------- ### Send and Receive Text with Live API Source: https://ai.google.dev/gemini-api/docs/live-guide This example demonstrates how to send user text input to the Live API and asynchronously receive text responses from the model. It configures the session for text modality and prints the received text. ```Python import asyncio from google import genai client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" config = {"response_modalities": ["TEXT"]} async def main(): async with client.aio.live.connect(model=model, config=config) as session: message = "Hello, how are you?" await session.send_client_content( turns={"role": "user", "parts": [{"text": message}]}, turn_complete=True ) async for response in session.receive(): if response.text is not None: print(response.text, end="") if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Configure and Send Real-time Audio Input Source: https://ai.google.dev/gemini-api/docs/live-guide This snippet demonstrates how to configure a Live API session for real-time input, including disabling automatic activity detection, and sending audio data along with activity start and end signals. It shows how to set up the session configuration and use `send_realtime_input` (Python) or `sendRealtimeInput` (JavaScript) to stream audio. ```Python config = { "response_modalities": ["TEXT"], "realtime_input_config": {"automatic_activity_detection": {"disabled": True}}, } async with client.aio.live.connect(model=model, config=config) as session: # ... await session.send_realtime_input(activity_start=types.ActivityStart()) await session.send_realtime_input( audio=types.Blob(data=audio_bytes, mime_type="audio/pcm;rate=16000") ) await session.send_realtime_input(activity_end=types.ActivityEnd()) # ... ``` ```JavaScript const config = { responseModalities: [Modality.TEXT], realtimeInputConfig: { automaticActivityDetection: { disabled: true, } } }; session.sendRealtimeInput({ activityStart: {} }) session.sendRealtimeInput( { audio: { data: base64Audio, mimeType: "audio/pcm;rate=16000" } } ); session.sendRealtimeInput({ activityEnd: {} }) ``` -------------------------------- ### Connect and Transcribe Audio with Google GenAI Live API in JavaScript Source: https://ai.google.dev/gemini-api/docs/live-guide This JavaScript snippet demonstrates how to establish a real-time connection to the Google GenAI Live API, send a WAV audio file for transcription, and process the incoming text and transcription responses. It includes setup for audio processing (resampling, bit depth conversion) to meet API requirements. ```JavaScript import { GoogleGenAI, Modality } from '@google/genai'; import *s fs from "node:fs"; import pkg from 'wavefile'; const { WaveFile } = pkg; const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" }); const model = 'gemini-2.0-flash-live-001'; const config = { responseModalities: [Modality.TEXT], inputAudioTranscription: {} }; async function live() { const responseQueue = []; async function waitMessage() { let done = false; let message = undefined; while (!done) { message = responseQueue.shift(); if (message) { done = true; } else { await new Promise((resolve) => setTimeout(resolve, 100)); } } return message; } async function handleTurn() { const turns = []; let done = false; while (!done) { const message = await waitMessage(); turns.push(message); if (message.serverContent && message.serverContent.turnComplete) { done = true; } } return turns; } const session = await ai.live.connect({ model: model, callbacks: { onopen: function () { console.debug('Opened'); }, onmessage: function (message) { responseQueue.push(message); }, onerror: function (e) { console.debug('Error:', e.message); }, onclose: function (e) { console.debug('Close:', e.reason); } }, config: config }); // Send Audio Chunk const fileBuffer = fs.readFileSync("16000.wav"); // Ensure audio conforms to API requirements (16-bit PCM, 16kHz, mono) const wav = new WaveFile(); wav.fromBuffer(fileBuffer); wav.toSampleRate(16000); wav.toBitDepth("16"); const base64Audio = wav.toBase64(); // If already in correct format, you can use this: // const fileBuffer = fs.readFileSync("sample.pcm"); // const base64Audio = Buffer.from(fileBuffer).toString('base64'); session.sendRealtimeInput( { audio: { data: base64Audio, mimeType: "audio/pcm;rate=16000" } } ); const turns = await handleTurn(); for (const turn of turns) { if (turn.serverContent && turn.serverContent.outputTranscription) { console.log("Transcription") console.log(turn.serverContent.outputTranscription.text); } } for (const turn of turns) { if (turn.text) { console.debug('Received text: %s\n', turn.text); } else if (turn.data) { console.debug('Received inline data: %s\n', turn.data); } else if (turn.serverContent && turn.serverContent.inputTranscription) { console.debug('Received input transcription: %s\n', turn.serverContent.inputTranscription.text); } } session.close(); } async function main() { await live().catch((e) => console.error('got error', e)); } main(); ``` -------------------------------- ### Connect to Native Audio Model for Live Session Source: https://ai.google.dev/gemini-api/docs/live-guide These snippets demonstrate how to connect to a native audio model (e.g., `gemini-2.5-flash-preview-native-audio-dialog`) and configure the live session to handle audio modalities. The `response_modalities` is set to `AUDIO` to enable native audio output. ```Python model = "gemini-2.5-flash-preview-native-audio-dialog" config = types.LiveConnectConfig(response_modalities=["AUDIO"]) async with client.aio.live.connect(model=model, config=config) as session: # Send audio input and receive audio ``` ```JavaScript const model = 'gemini-2.5-flash-preview-native-audio-dialog'; const config = { responseModalities: [Modality.AUDIO] }; async function main() { const session = await ai.live.connect({ model: model, config: config, callbacks: ..., }); // Send audio input and receive audio session.close(); } main(); ``` -------------------------------- ### Go: Upload File, Create & Use Cached Content Source: https://ai.google.dev/api/caching This Go example illustrates the process of uploading a file to the Gemini API, creating a content cache based on the uploaded document with a system instruction, and subsequently retrieving and utilizing this cached content for content generation. It showcases the Go client library's approach to managing persistent content for model interactions. ```Go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } modelName := "gemini-1.5-flash-001" document, err := client.Files.UploadFromPath( ctx, filepath.Join(getMedia(), "a11.txt"), &genai.UploadFileConfig{ MIMEType : "text/plain", }, ) if err != nil { log.Fatal(err) } parts := []*genai.Part{ genai.NewPartFromURI(document.URI, document.MIMEType), } contents := []*genai.Content{ genai.NewContentFromParts(parts, "user"), } cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{ Contents: contents, SystemInstruction: genai.NewContentFromText( "You are an expert analyzing transcripts.", "user", ), }) if err != nil { log.Fatal(err) } cacheName := cache.Name // Later retrieve the cache. cache, err = client.Caches.Get(ctx, cacheName, &genai.GetCachedContentConfig{}) if err != nil { log.Fatal(err) } response, err := client.Models.GenerateContent( ctx, modelName, genai.Text("Find a lighthearted moment from this transcript"), &genai.GenerateContentConfig{ CachedContent: cache.Name, }, ) if err != nil { log.Fatal(err) } fmt.Println("Response from cache (create from name):") printResponse(response) ``` -------------------------------- ### Send Incremental Content Updates to Google GenAI Live Session Source: https://ai.google.dev/gemini-api/docs/live-guide This example demonstrates how to send conversational turns incrementally to the Google GenAI Live API. It shows how to use the `turn_complete` (Python) or `turnComplete` (JavaScript) flag to indicate whether a turn is finished or if more content is expected, allowing for multi-part user inputs or continuous conversation. ```Python turns = [ {"role": "user", "parts": [{"text": "What is the capital of France?"}]}, {"role": "model", "parts": [{"text": "Paris"}]}, ] await session.send_client_content(turns=turns, turn_complete=False) turns = [{"role": "user", "parts": [{"text": "What is the capital of Germany?"}]}] await session.send_client_content(turns=turns, turn_complete=True) ``` ```JavaScript let inputTurns = [ { "role": "user", "parts": [{ "text": "What is the capital of France?" }] }, { "role": "model", "parts": [{ "text": "Paris" }] } ] session.sendClientContent({ turns: inputTurns, turnComplete: false }) inputTurns = [{ "role": "user", "parts": [{ "text": "What is the capital of Germany?" }] }] session.sendClientContent({ turns: inputTurns, turnComplete: true }) ``` -------------------------------- ### Gemini Live API: Basic Function Calling in Python Source: https://context7_llms This Python example demonstrates how to define and use function declarations with the Gemini Live API. It shows how to send client content, receive tool calls, and respond with `FunctionResponse` objects, highlighting the manual handling of tool responses required by the Live API. ```Python import asyncio from google import genai from google.genai import types client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" # Simple function definitions turn_on_the_lights = {"name": "turn_on_the_lights"} turn_off_the_lights = {"name": "turn_off_the_lights"} tools = [{"function_declarations": [turn_on_the_lights, turn_off_the_lights]}] config = {"response_modalities": ["TEXT"], "tools": tools} async def main(): async with client.aio.live.connect(model=model, config=config) as session: prompt = "Turn on the lights please" await session.send_client_content(turns={"parts": [{"text": prompt}]}) async for chunk in session.receive(): if chunk.server_content: if chunk.text is not None: print(chunk.text) elif chunk.tool_call: function_responses = [] for fc in chunk.tool_call.function_calls: function_response = types.FunctionResponse( id=fc.id, name=fc.name, response={"result": "ok"} # simple, hard-coded function response ) function_responses.append(function_response) await session.send_tool_response(function_responses=function_responses) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Configure Gemini Live API with Multiple Tools (Python) Source: https://context7_llms This Python example illustrates how to set up a Gemini Live API session to utilize a combination of tools: Google Search, code execution, and custom function declarations. It shows the `tools` configuration for a prompt that requires diverse capabilities. This snippet focuses on the tool configuration aspect, assuming the connection and interaction logic are handled elsewhere. ```Python prompt = """ Hey, I need you to do three things for me. 1. Compute the largest prime palindrome under 100000. 2. Then use Google Search to look up information about the largest earthquake in California the week of Dec 5 2024? 3. Turn on the lights Thanks! """ tools = [ {"google_search": {}}, {"code_execution": {}}, {"function_declarations": [turn_on_the_lights, turn_off_the_lights]}, ] config = {"response_modalities": ["TEXT"], "tools": tools} # ... remaining model call ``` -------------------------------- ### Establish Live API Connection with API Key Source: https://ai.google.dev/gemini-api/docs/live-guide This snippet demonstrates how to create an asynchronous connection to the Gemini Live API using an API key. It initializes the client, specifies the model ('gemini-2.0-flash-live-001'), and configures the session for text response modalities. ```Python import asyncio from google import genai client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" config = {"response_modalities": ["TEXT"]} async def main(): async with client.aio.live.connect(model=model, config=config) as session: print("Session started") if __name__ == "__main__": asyncio.run(main()) ``` ```JavaScript import { GoogleGenAI, Modality } from '@google/genai'; const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" }); const model = 'gemini-2.0-flash-live-001'; const config = { responseModalities: [Modality.TEXT] }; async function main() { const session = await ai.live.connect({ model: model, callbacks: { onopen: function () { console.debug('Opened'); }, onmessage: function (message) { console.debug(message); }, onerror: function (e) { console.debug('Error:', e.message); }, onclose: function (e) { console.debug('Close:', e.reason); } }, config: config }); // Send content... session.close(); } main(); ``` -------------------------------- ### Configure Automatic Voice Activity Detection (VAD) Parameters Source: https://ai.google.dev/gemini-api/docs/live-guide These snippets show how to configure automatic Voice Activity Detection (VAD) parameters for real-time audio input, including sensitivity levels for start and end of speech, prefix padding, and silence duration. This allows fine-grained control over when the API detects speech activity. ```Python from google.genai import types config = { "response_modalities": ["TEXT"], "realtime_input_config": { "automatic_activity_detection": { "disabled": False, # default "start_of_speech_sensitivity": types.StartSensitivity.START_SENSITIVITY_LOW, "end_of_speech_sensitivity": types.EndSensitivity.END_SENSITIVITY_LOW, "prefix_padding_ms": 20, "silence_duration_ms": 100 } } } ``` ```JavaScript import { GoogleGenAI, Modality, StartSensitivity, EndSensitivity } from '@google/genai'; const config = { responseModalities: [Modality.TEXT], realtimeInputConfig: { automaticActivityDetection: { disabled: false, // default startOfSpeechSensitivity: StartSensitivity.START_SENSITIVITY_LOW, endOfSpeechSensitivity: EndSensitivity.END_SENSITIVITY_LOW, prefixPaddingMs: 20, silenceDurationMs: 100 } } }; ``` -------------------------------- ### Gemini Live API: Enabling and Using Code Execution in Python Source: https://context7_llms This Python example demonstrates how to enable and utilize code execution with the Gemini Live API. It shows how to configure the session to allow code execution, send a prompt that requires computation, and then print both the generated executable code and its execution result from the model's response. ```Python import asyncio from google import genai from google.genai import types client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" tools = [{'code_execution': {}}] config = {"response_modalities": ["TEXT"], "tools": tools} async def main(): async with client.aio.live.connect(model=model, config=config) as session: prompt = "Compute the largest prime palindrome under 100000." await session.send_client_content(turns={"parts": [{"text": prompt}]}) async for chunk in session.receive(): if chunk.server_content: if chunk.text is not None: print(chunk.text) model_turn = chunk.server_content.model_turn if model_turn: for part in model_turn.parts: if part.executable_code is not None: print(part.executable_code.code) if part.code_execution_result is not None: print(part.code_execution_result.output) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Enable Input Audio Transcription Source: https://context7_llms Configure the model to transcribe incoming audio input. This is achieved by including 'input_audio_transcription' in the setup configuration and then retrieving the transcript from 'msg.server_content.input_transcription' as messages are received. ```Python config = { "response_modalities": ["TEXT"], "input_audio_transcription": {}, } async def main(): async with client.aio.live.connect(model=model, config=config) as session: # ... send audio data ... async for msg in session.receive(): if msg.server_content.input_transcription: print('Transcript:', msg.server_content.input_transcription.text) ``` -------------------------------- ### Connect to Native Audio Model with Thinking Capabilities Source: https://ai.google.dev/gemini-api/docs/live-guide These snippets show how to connect to a native audio model that supports thinking capabilities, specifically `gemini-2.5-flash-exp-native-audio-thinking-dialog`. The `response_modalities` is set to `AUDIO` to enable native audio output with enhanced reasoning. ```Python model = "gemini-2.5-flash-exp-native-audio-thinking-dialog" config = types.LiveConnectConfig(response_modalities=["AUDIO"]) async with client.aio.live.connect(model=model, config=config) as session: # Send audio input and receive audio ``` ```JavaScript const model = 'gemini-2.5-flash-exp-native-audio-thinking-dialog'; const config = { responseModalities: [Modality.AUDIO] }; async function main() { const session = await ai.live.connect({ model: model, config: config, callbacks: ..., }); // Send audio input and receive audio session.close(); } main(); ``` -------------------------------- ### Implement Function Calling with Gemini Live API in Python Source: https://ai.google.dev/gemini-api/docs/live-tools This Python asynchronous example demonstrates how to connect to the Gemini Live API, define simple function declarations (e.g., `turn_on_the_lights`, `turn_off_the_lights`), send a prompt that triggers a tool call, and manually handle the `FunctionResponse` by sending it back to the session. It uses `google.genai` library and `asyncio`. ```Python import asyncio from google import genai from google.genai import types client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" # Simple function definitions turn_on_the_lights = {"name": "turn_on_the_lights"} turn_off_the_lights = {"name": "turn_off_the_lights"} tools = [{"function_declarations": [turn_on_the_lights, turn_off_the_lights]}] config = {"response_modalities": ["TEXT"], "tools": tools} async def main(): async with client.aio.live.connect(model=model, config=config) as session: prompt = "Turn on the lights please" await session.send_client_content(turns={"parts": [{"text": prompt}]}) async for chunk in session.receive(): if chunk.server_content: if chunk.text is not None: print(chunk.text) elif chunk.tool_call: function_responses = [] for fc in chunk.tool_call.function_calls: function_response = types.FunctionResponse( id=fc.id, name=fc.name, response={ "result": "ok" } # simple, hard-coded function response ) function_responses.append(function_response) await session.send_tool_response(function_responses=function_responses) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Configure Automatic Voice Activity Detection Parameters Source: https://context7_llms Provides examples for configuring detailed parameters of automatic Voice Activity Detection (VAD) within the Gemini API session's `realtime_input_config`. This includes setting `start_of_speech_sensitivity`, `end_of_speech_sensitivity`, `prefix_padding_ms`, and `silence_duration_ms` for more precise control over VAD behavior. ```Python from google.genai import types config = { "response_modalities": ["TEXT"], "realtime_input_config": { "automatic_activity_detection": { "disabled": False, # default "start_of_speech_sensitivity": types.StartSensitivity.START_SENSITIVITY_LOW, "end_of_speech_sensitivity": types.EndSensitivity.END_SENSITIVITY_LOW, "prefix_padding_ms": 20, "silence_duration_ms": 100, } } } ``` ```JavaScript import { GoogleGenAI, Modality, StartSensitivity, EndSensitivity } from '@google/genai'; const config = { responseModalities: [Modality.TEXT], realtimeInputConfig: { automaticActivityDetection: { disabled: false, // default startOfSpeechSensitivity: StartSensitivity.START_SENSITIVITY_LOW, endOfSpeechSensitivity: EndSensitivity.END_SENSITIVITY_LOW, prefixPaddingMs: 20, silenceDurationMs: 100, } } }; ``` -------------------------------- ### Connect to Google GenAI Live Session and Handle Text Interactions (JavaScript) Source: https://ai.google.dev/gemini-api/docs/live-guide This snippet demonstrates how to establish a live connection with the Google GenAI API using JavaScript. It shows how to configure the session, send initial text content, and asynchronously process incoming text or data responses from the model, managing the message flow with a queue. ```JavaScript import { GoogleGenAI, Modality } from '@google/genai'; const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" }); const model = 'gemini-2.0-flash-live-001'; const config = { responseModalities: [Modality.TEXT] }; async function live() { const responseQueue = []; async function waitMessage() { let done = false; let message = undefined; while (!done) { message = responseQueue.shift(); if (message) { done = true; } else { await new Promise((resolve) => setTimeout(resolve, 100)); } } return message; } async function handleTurn() { const turns = []; let done = false; while (!done) { const message = await waitMessage(); turns.push(message); if (message.serverContent && message.serverContent.turnComplete) { done = true; } } return turns; } const session = await ai.live.connect({ model: model, callbacks: { onopen: function () { console.debug('Opened'); }, onmessage: function (message) { responseQueue.push(message); }, onerror: function (e) { console.debug('Error:', e.message); }, onclose: function (e) { console.debug('Close:', e.reason); } }, config: config }); const inputTurns = 'Hello how are you?'; session.sendClientContent({ turns: inputTurns }); const turns = await handleTurn(); for (const turn of turns) { if (turn.text) { console.debug('Received text: %s\n', turn.text); } else if (turn.data) { console.debug('Received inline data: %s\n', turn.data); } } session.close(); } async function main() { await live().catch((e) => console.error('got error', e)); } main(); ``` -------------------------------- ### Enable Output Audio Transcription Source: https://context7_llms Configure the model to transcribe its audio output. This involves setting 'output_audio_transcription' in the setup configuration and then accessing the transcript from the 'response.server_content.output_transcription' field during session reception. ```Python config = {"response_modalities": ["AUDIO"], "output_audio_transcription": {} } async def main(): async with client.aio.live.connect(model=model, config=config) as session: # ... send content ... async for response in session.receive(): if response.server_content.output_transcription: print("Transcript:", response.server_content.output_transcription.text) ``` -------------------------------- ### Connect to Google GenAI Live API with JavaScript for Code Execution Source: https://ai.google.dev/gemini-api/docs/live-tools This JavaScript example demonstrates how to establish a live connection to the Google GenAI API, configure it with a `codeExecution` tool, send a prompt, and process streaming responses. It shows how to handle text, executable code, and code execution results from the model, as well as managing the response queue and session lifecycle. ```JavaScript import { GoogleGenAI, Modality } from '@google/genai'; const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" }); const model = 'gemini-2.0-flash-live-001'; const tools = [{codeExecution: {}}] const config = { responseModalities: [Modality.TEXT], tools: tools } async function live() { const responseQueue = []; async function waitMessage() { let done = false; let message = undefined; while (!done) { message = responseQueue.shift(); if (message) { done = true; } else { await new Promise((resolve) => setTimeout(resolve, 100)); } } return message; } async function handleTurn() { const turns = []; let done = false; while (!done) { const message = await waitMessage(); turns.push(message); if (message.serverContent && message.serverContent.turnComplete) { done = true; } else if (message.toolCall) { done = true; } } return turns; } const session = await ai.live.connect({ model: model, callbacks: { onopen: function () { console.debug('Opened'); }, onmessage: function (message) { responseQueue.push(message); }, onerror: function (e) { console.debug('Error:', e.message); }, onclose: function (e) { console.debug('Close:', e.reason); } }, config: config }); const inputTurns = 'Compute the largest prime palindrome under 100000.'; session.sendClientContent({ turns: inputTurns }); const turns = await handleTurn(); for (const turn of turns) { if (turn.serverContent && turn.serverContent.modelTurn && turn.serverContent.modelTurn.parts) { for (const part of turn.serverContent.modelTurn.parts) { if (part.text) { console.debug('Received text: %s\n', part.text); } else if (part.executableCode) { console.debug('executableCode: %s\n', part.executableCode.code); } else if (part.codeExecutionResult) { console.debug('codeExecutionResult: %s\n', part.codeExecutionResult.output); } } } } session.close(); } async function main() { await live().catch((e) => console.error('got error', e)); } main(); ``` -------------------------------- ### Configure Voice for Google GenAI Live API Source: https://ai.google.dev/gemini-api/docs/live-guide These snippets demonstrate how to set a specific prebuilt voice for audio responses when using the Google GenAI Live API. They specify the response modality as AUDIO and include the `speech_config` for voice selection in both Python and JavaScript. ```Python config = { "response_modalities": ["AUDIO"], "speech_config": { "voice_config": {"prebuilt_voice_config": {"voice_name": "Kore"}} } } ``` ```JavaScript const config = { responseModalities: [Modality.AUDIO], speechConfig: { voiceConfig: { prebuiltVoiceConfig: { voiceName: "Kore" } } } }; ``` -------------------------------- ### Live API UsageMetadata Structure Source: https://ai.google.dev/gemini-api/docs/live-guide Describes the structure of the `usageMetadata` field returned in Live API server messages, which provides details on token consumption. It includes the total token count and a breakdown of tokens used per modality. ```APIDOC usageMetadata: totalTokenCount: number responseTokensDetails: Array ModalityTokenCount: modality: string (e.g., "TEXT", "AUDIO") token_count: number ``` -------------------------------- ### Live API Supported Languages and BCP-47 Codes Source: https://ai.google.dev/gemini-api/docs/live-guide Lists the languages and their corresponding BCP-47 codes supported by the Live API. Native audio output models automatically choose the language and do not support explicit language code settings. Languages marked with an asterisk are not available for Native audio. ```APIDOC Language BCP-47 Code German (Germany) de-DE English (Australia)* en-AU English (UK)* en-GB English (India) en-IN English (US) en-US Spanish (US) es-US French (France) fr-FR Hindi (India) hi-IN Portuguese (Brazil) pt-BR Arabic (Generic) ar-XA Spanish (Spain)* es-ES French (Canada)* fr-CA Indonesian (Indonesia) id-ID Italian (Italy) it-IT Japanese (Japan) ja-JP Turkish (Turkey) tr-TR Vietnamese (Vietnam) vi-VN Bengali (India) bn-IN Gujarati (India)* gu-IN Kannada (India)* kn-IN Marathi (India) mr-IN Malayalam (India)* ml-IN Tamil (India) ta-IN Telugu (India) te-IN Dutch (Netherlands) nl-NL Korean (South Korea) ko-KR Mandarin Chinese (China)* cmn-CN Polish (Poland) pl-PL Russian (Russia) ru-RU Thai (Thailand) th-TH ``` -------------------------------- ### Connect to Google GenAI Live API with Python and Google Search Grounding Source: https://ai.google.dev/gemini-api/docs/live-tools This Python example demonstrates how to connect to the Google GenAI Live API, configuring the session to use `google_search` as a tool for grounding. It shows how to send a prompt and asynchronously iterate through streaming responses, printing text, executable code, and code execution results generated by the model. ```Python import asyncio from google import genai from google.genai import types client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" tools = [{'google_search': {}}] config = {"response_modalities": ["TEXT"], "tools": tools} async def main(): async with client.aio.live.connect(model=model, config=config) as session: prompt = "When did the last Brazil vs. Argentina soccer match happen?" await session.send_client_content(turns={"parts": [{"text": prompt}]}) async for chunk in session.receive(): if chunk.server_content: if chunk.text is not None: print(chunk.text) # The model might generate and execute Python code to use Search model_turn = chunk.server_content.model_turn if model_turn: for part in model_turn.parts: if part.executable_code is not None: print(part.executable_code.code) if part.code_execution_result is not None: print(part.code_execution_result.output) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Configure Thinking Budget for Gemini 2.5 Pro in Go Source: https://ai.google.dev/gemini-api/docs/thinking This Go example demonstrates how to use the `google.golang.org/genai` library to generate content with the Gemini 2.5 Pro model. It shows how to set the `ThinkingBudget` parameter to 1024 within the `GenerateContentConfig` and includes commented-out options for disabling or enabling dynamic thinking. ```Go package main import ( "context" "fmt" "google.golang.org/genai" "os" ) func main() { ctx := context.Background() client, _ := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GOOGLE_API_KEY"), Backend: genai.BackendGeminiAPI, }) thinkingBudgetVal := int32(1024) contents := genai.Text("Provide a list of 3 famous physicists and their key contributions") model := "gemini-2.5-pro" resp, _ := client.Models.GenerateContent(ctx, model, contents, &genai.GenerateContentConfig{ ThinkingConfig: &genai.ThinkingConfig{ ThinkingBudget: &thinkingBudgetVal, // Turn off thinking: // ThinkingBudget: int32(0), // Turn on dynamic thinking: // ThinkingBudget: int32(-1), }, }) fmt.Println(resp.Text()) } ``` -------------------------------- ### Python Example: Send and Receive WAV Audio with Live API Source: https://ai.google.dev/gemini-api/docs/live This Python script demonstrates how to interact with the Live API to stream audio. It reads a WAV file, converts it to 16-bit PCM, 16kHz, mono format, sends it via a Live API session, and then receives and saves the output audio (24kHz) as a new WAV file. It uses `librosa` and `soundfile` for audio processing. ```Python # Test file: https://storage.googleapis.com/generativeai-downloads/data/16000.wav # Install helpers for converting files: pip install librosa soundfile import asyncio import io from pathlib import Path import wave from google import genai from google.genai import types import soundfile as sf import librosa client = genai.Client(api_key="GEMINI_API_KEY") # Half cascade model: # model = "gemini-2.0-flash-live-001" # Native audio output model: model = "gemini-2.5-flash-preview-native-audio-dialog" config = { "response_modalities": ["AUDIO"], "system_instruction": "You are a helpful assistant and answer in a friendly tone.", } async def main(): async with client.aio.live.connect(model=model, config=config) as session: buffer = io.BytesIO() y, sr = librosa.load("sample.wav", sr=16000) sf.write(buffer, y, sr, format='RAW', subtype='PCM_16') buffer.seek(0) audio_bytes = buffer.read() # If already in correct format, you can use this: # audio_bytes = Path("sample.pcm").read_bytes() await session.send_realtime_input( audio=types.Blob(data=audio_bytes, mime_type="audio/pcm;rate=16000") ) wf = wave.open("audio.wav", "wb") wf.setnchannels(1) wf.setsampwidth(2) wf.setframerate(24000) # Output is 24kHz async for response in session.receive(): if response.data is not None: wf.writeframes(response.data) # Un-comment this code to print audio data info # if response.server_content.model_turn is not None: # print(response.server_content.model_turn.parts[0].inline_data.mime_type) wf.close() if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### JavaScript: Real-time Audio Interaction with Google GenAI Live API Source: https://ai.google.dev/gemini-api/docs/live-guide This snippet demonstrates how to establish a real-time connection with the Google GenAI Live API, send audio chunks (e.g., PCM data), and process streaming text responses. It includes error handling, message queuing, and session management for live audio interactions. ```JavaScript // example audio file to try: // URL = "https://storage.googleapis.com/generativeai-downloads/data/hello_are_you_there.pcm" // !wget -q $URL -O sample.pcm import { GoogleGenAI, Modality } from '@google/genai'; import * as fs from "node:fs"; const ai = new GoogleGenAI({ apiKey: "GOOGLE_API_KEY" }); const model = 'gemini-2.0-flash-live-001'; const config = { responseModalities: [Modality.TEXT] }; async function live() { const responseQueue = []; async function waitMessage() { let done = false; let message = undefined; while (!done) { message = responseQueue.shift(); if (message) { done = true; } else { await new Promise((resolve) => setTimeout(resolve, 100)); } } return message; } async function handleTurn() { const turns = []; let done = false; while (!done) { const message = await waitMessage(); turns.push(message); if (message.serverContent && message.serverContent.turnComplete) { done = true; } } return turns; } const session = await ai.live.connect({ model: model, callbacks: { onopen: function () { console.debug('Opened'); }, onmessage: function (message) { responseQueue.push(message); }, onerror: function (e) { console.debug('Error:', e.message); }, onclose: function (e) { console.debug('Close:', e.reason); } }, config: config }); // Send Audio Chunk const fileBuffer = fs.readFileSync("sample.pcm"); const base64Audio = Buffer.from(fileBuffer).toString('base64'); session.sendRealtimeInput( { audio: { data: base64Audio, mimeType: "audio/pcm;rate=16000" } } ); // if stream gets paused, send: // session.sendRealtimeInput({ audioStreamEnd: true }) const turns = await handleTurn(); for (const turn of turns) { if (turn.text) { console.debug('Received text: %s\n', turn.text); } else if (turn.data) { console.debug('Received inline data: %s\n', turn.data); } } session.close(); } async function main() { await live().catch((e) => console.error('got error', e)); } main(); ``` -------------------------------- ### Go: Upload File and Use Cached Content with Gemini API Source: https://ai.google.dev/api/caching This Go snippet initializes the Gemini API client, uploads a local text file, creates a cached content entry with a specified system instruction, and then uses this cache to generate a summary from the Gemini model. It demonstrates the full lifecycle of cached content usage. ```Go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } modelName := "gemini-1.5-flash-001" document, err := client.Files.UploadFromPath( ctx, filepath.Join(getMedia(), "a11.txt"), &genai.UploadFileConfig{ MIMEType : "text/plain", }, ) if err != nil { log.Fatal(err) } parts := []*genai.Part{ genai.NewPartFromURI(document.URI, document.MIMEType), } contents := []*genai.Content{ genai.NewContentFromParts(parts, "user"), } cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{ Contents: contents, SystemInstruction: genai.NewContentFromText( "You are an expert analyzing transcripts.", "user", ), }) if err != nil { log.Fatal(err) } fmt.Println("Cache created:") fmt.Println(cache) // Use the cache for generating content. response, err := client.Models.GenerateContent( ctx, modelName, genai.Text("Please summarize this transcript"), &genai.GenerateContentConfig{ CachedContent: cache.Name, }, ) if err != nil { log.Fatal(err) } printResponse(response) ``` -------------------------------- ### Send Real-time Audio Input to Gemini API (Python) Source: https://ai.google.dev/gemini-api/docs/live-guide This Python snippet demonstrates how to connect to the Google Gemini API for real-time audio input. It loads a WAV file, converts it to 16kHz PCM using `librosa` and `soundfile`, sends the audio bytes to the API, and prints the received text responses. Requires `asyncio` for asynchronous operations. ```python # Test file: https://storage.googleapis.com/generativeai-downloads/data/16000.wav # Install helpers for converting files: pip install librosa soundfile import asyncio import io from pathlib import Path from google import genai from google.genai import types import soundfile as sf import librosa client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" config = {"response_modalities": ["TEXT"]} async def main(): async with client.aio.live.connect(model=model, config=config) as session: buffer = io.BytesIO() y, sr = librosa.load("sample.wav", sr=16000) sf.write(buffer, y, sr, format='RAW', subtype='PCM_16') buffer.seek(0) audio_bytes = buffer.read() # If already in correct format, you can use this: # audio_bytes = Path("sample.pcm").read_bytes() await session.send_realtime_input( audio=types.Blob(data=audio_bytes, mime_type="audio/pcm;rate=16000") ) async for response in session.receive(): if response.text is not None: print(response.text) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Shell: Create and Use Cached Content via Gemini REST API Source: https://ai.google.dev/api/caching This shell script demonstrates how to interact with the Gemini API directly using `wget` and `curl`. It shows how to download a file, prepare a JSON request with base64 encoded content for creating cached content, and then use the generated cache name to make a `generateContent` call. ```Shell wget https://storage.googleapis.com/generativeai-downloads/data/a11.txt echo '{ "model": "models/gemini-1.5-flash-001", "contents":[ { "parts":[ { "inline_data": { "mime_type":"text/plain", "data": "'$(base64 $B64FLAGS a11.txt)'" } } ], "role": "user" } ], "systemInstruction": { "parts": [ { "text": "You are an expert at analyzing transcripts." } ] }, "ttl": "300s" }' > request.json curl -X POST "https://generativelanguage.googleapis.com/v1beta/cachedContents?key=$GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -d @request.json \ > cache.json CACHE_NAME=$(cat cache.json | grep '"name":' | cut -d '"' -f 4 | head -n 1) curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-001:generateContent?key=$GEMINI_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "contents": [ { "parts":[{ "text": "Please summarize this transcript" }], "role": "user" }, ], "cachedContent": "'$CACHE_NAME'" }' ``` -------------------------------- ### Gemini Live API: Overview of Supported Tools by Model Source: https://context7_llms This table provides an overview of the tools supported by different Gemini Live API models, including Google Search, Function calling, Code execution, and URL context. It indicates which tools are available for `gemini-2.0-flash-live-001`, `gemini-2.5-flash-preview-native-audio-dialog`, and `gemini-2.5-flash-exp-native-audio-thinking-dialog`. ```APIDOC Tool | `gemini-2.0-flash-live-001` | `gemini-2.5-flash-preview-native-audio-dialog` | `gemini-2.5-flash-exp-native-audio-thinking-dialog` --- | --- | --- | --- Google Search | Yes | Yes | Yes Function calling | Yes | Yes | No Code execution | Yes | No | No URL context | Yes | No | No ``` -------------------------------- ### Send Audio Input for Transcription (Python) Source: https://ai.google.dev/gemini-api/docs/live-guide This Python example illustrates how to send raw audio data to the Gemini Live API for real-time transcription. It configures the session to expect text responses and enable input audio transcription, then sends a PCM audio file and prints the received transcription. ```Python import asyncio from pathlib import Path from google import genai from google.genai import types client = genai.Client(api_key="GEMINI_API_KEY") model = "gemini-2.0-flash-live-001" config = { "response_modalities": ["TEXT"], "input_audio_transcription": {} } async def main(): async with client.aio.live.connect(model=model, config=config) as session: audio_data = Path("16000.pcm").read_bytes() await session.send_realtime_input( audio=types.Blob(data=audio_data, mime_type='audio/pcm;rate=16000') ) async for msg in session.receive(): if msg.server_content.input_transcription: print('Transcript:', msg.server_content.input_transcription.text) if __name__ == "__main__": asyncio.run(main()) ```