これは、Live API で利用可能な機能と構成について説明する包括的なガイドです。概要と一般的なユースケースのサンプルコードについては、Live API のスタートガイドをご覧ください。
始める前に
- コア コンセプトを理解する: まだ読んでいない場合は、まず Live API を使ってみる ページをご覧ください。このドキュメントでは、Live API の基本的な原則、仕組み、さまざまなモデルとその対応する音声生成方法(ネイティブ音声またはハーフ カスケード)の違いについて説明します。
 - AI Studio で Live API を試す: 構築を開始する前に、Google AI Studio で Live API を試してみることをおすすめします。Google AI Studio で Live API を使用するには、[ストリーム] を選択します。
 
接続を確立する
次の例は、API キーを使用して接続を作成する方法を示しています。
Python
import asyncio
from google import genai
client = genai.Client()
model = "gemini-live-2.5-flash-preview"
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({});
const model = 'gemini-live-2.5-flash-preview';
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();
インタラクション モダリティ
以降のセクションでは、Live API で使用できるさまざまな入力モードと出力モードの例と、それらをサポートするコンテキストについて説明します。
テキストの送受信
テキストの送受信方法は次のとおりです。
Python
import asyncio
from google import genai
client = genai.Client()
model = "gemini-live-2.5-flash-preview"
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())
JavaScript
import { GoogleGenAI, Modality } from '@google/genai';
const ai = new GoogleGenAI({});
const model = 'gemini-live-2.5-flash-preview';
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();
コンテンツの増分更新
増分更新を使用して、テキスト入力の送信、セッション コンテキストの確立、セッション コンテキストの復元を行います。コンテキストが短い場合は、ターンバイターンのインタラクションを送信して、イベントの正確なシーケンスを表すことができます。
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 })
コンテキストが長い場合は、1 つのメッセージの概要を提供して、後続のインタラクション用にコンテキスト ウィンドウを空けておくことをおすすめします。セッション コンテキストを読み込む別の方法については、セッションの再開をご覧ください。
音声の送受信
最も一般的な音声の例である音声から音声への変換については、スタートガイドをご覧ください。
WAV ファイルを読み取り、正しい形式で送信して、テキスト出力を受け取る音声テキスト変換の例を次に示します。
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()
model = "gemini-live-2.5-flash-preview"
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())
JavaScript
// Test file: https://storage.googleapis.com/generativeai-downloads/data/16000.wav
// Install helpers for converting files: npm install wavefile
import { GoogleGenAI, Modality } from '@google/genai';
import * as fs from "node:fs";
import pkg from 'wavefile';
const { WaveFile } = pkg;
const ai = new GoogleGenAI({});
const model = 'gemini-live-2.5-flash-preview';
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.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.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();
テキストから音声への変換の例を次に示します。AUDIO をレスポンス モダリティとして設定すると、音声を受信できます。この例では、受信したデータを WAV ファイルとして保存します。
Python
import asyncio
import wave
from google import genai
client = genai.Client()
model = "gemini-live-2.5-flash-preview"
config = {"response_modalities": ["AUDIO"]}
async def main():
    async with client.aio.live.connect(model=model, config=config) as session:
        wf = wave.open("audio.wav", "wb")
        wf.setnchannels(1)
        wf.setsampwidth(2)
        wf.setframerate(24000)
        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.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
import { GoogleGenAI, Modality } from '@google/genai';
import * as fs from "node:fs";
import pkg from 'wavefile';
const { WaveFile } = pkg;
const ai = new GoogleGenAI({});
const model = 'gemini-live-2.5-flash-preview';
const config = { responseModalities: [Modality.AUDIO] };
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();
  // Combine audio data strings and save as wave file
  const combinedAudio = turns.reduce((acc, turn) => {
    if (turn.data) {
      const buffer = Buffer.from(turn.data, 'base64');
      const intArray = new Int16Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Int16Array.BYTES_PER_ELEMENT);
      return acc.concat(Array.from(intArray));
    }
    return acc;
  }, []);
  const audioBuffer = new Int16Array(combinedAudio);
  const wf = new WaveFile();
  wf.fromScratch(1, 24000, '16', audioBuffer);
  fs.writeFileSync('output.wav', wf.toBuffer());
  session.close();
}
async function main() {
  await live().catch((e) => console.error('got error', e));
}
main();
オーディオ形式
Live API の音声データは常に RAW、リトル エンディアン、16 ビット PCM です。オーディオ出力は常に 24 kHz のサンプリング レートを使用します。入力音声はネイティブで 16 kHz ですが、必要に応じて Live API がリサンプリングするため、任意のサンプルレートを送信できます。入力音声のサンプルレートを伝えるには、音声を含む各 Blob の MIME タイプを audio/pcm;rate=16000 などの値に設定します。
音声文字起こし
設定構成で output_audio_transcription を送信することで、モデルの音声出力の文字起こしを有効にできます。文字起こし言語は、モデルのレスポンスから推測されます。
Python
import asyncio
from google import genai
from google.genai import types
client = genai.Client()
model = "gemini-live-2.5-flash-preview"
config = {"response_modalities": ["AUDIO"],
        "output_audio_transcription": {}
}
async def main():
    async with client.aio.live.connect(model=model, config=config) as session:
        message = "Hello? Gemini are you there?"
        await session.send_client_content(
            turns={"role": "user", "parts": [{"text": message}]}, turn_complete=True
        )
        async for response in session.receive():
            if response.server_content.model_turn:
                print("Model turn:", response.server_content.model_turn)
            if response.server_content.output_transcription:
                print("Transcript:", response.server_content.output_transcription.text)
if __name__ == "__main__":
    asyncio.run(main())
JavaScript
import { GoogleGenAI, Modality } from '@google/genai';
const ai = new GoogleGenAI({});
const model = 'gemini-live-2.5-flash-preview';
const config = {
  responseModalities: [Modality.AUDIO],
  outputAudioTranscription: {}
};
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.serverContent && turn.serverContent.outputTranscription) {
      console.debug('Received output transcription: %s\n', turn.serverContent.outputTranscription.text);
    }
  }
  session.close();
}
async function main() {
  await live().catch((e) => console.error('got error', e));
}
main();
セットアップ構成で input_audio_transcription を送信することで、音声入力の文字起こしを有効にできます。
Python
import asyncio
from pathlib import Path
from google import genai
from google.genai import types
client = genai.Client()
model = "gemini-live-2.5-flash-preview"
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())
JavaScript
import { GoogleGenAI, Modality } from '@google/genai';
import * as fs from "node:fs";
import pkg from 'wavefile';
const { WaveFile } = pkg;
const ai = new GoogleGenAI({});
const model = 'gemini-live-2.5-flash-preview';
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();
音声と動画をストリーミングする
音声と言語を変更する
Live API モデルはそれぞれ異なる音声セットをサポートしています。ハーフ カスケードは、Puck、Charon、Kore、Fenrir、Aoede、Leda、Orus、Zephyr をサポートしています。ネイティブ音声は、はるかに長いリストをサポートしています(TTS モデルのリストと同じ)。AI Studio で、すべての音声を聞くことができます。
音声を指定するには、セッション構成の一部として、speechConfig オブジェクト内に音声名を設定します。
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 は複数の言語をサポートしています。
言語を変更するには、セッション構成の一部として、speechConfig オブジェクト内に言語コードを設定します。
Python
config = {
    "response_modalities": ["AUDIO"],
    "speech_config": {
        "language_code": "de-DE"
    }
}
JavaScript
const config = {
  responseModalities: [Modality.AUDIO],
  speechConfig: { languageCode: "de-DE" }
};
ネイティブ オーディオ機能
次の機能は、ネイティブ オーディオでのみ使用できます。ネイティブ オーディオの詳細については、モデルと音声生成を選択するをご覧ください。
ネイティブ音声出力の使用方法
ネイティブ音声出力を使用するには、ネイティブ音声モデルのいずれかを構成し、response_modalities を AUDIO に設定します。
完全な例については、音声を送受信するをご覧ください。
Python
model = "gemini-2.5-flash-native-audio-preview-09-2025"
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-native-audio-preview-09-2025';
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();
アフェクティブ ダイアログ
この機能を使用すると、Gemini は入力された表現と口調に応じて回答スタイルを調整できます。
アフェクティブ ダイアログを使用するには、セットアップ メッセージで API バージョンを v1alpha に設定し、enable_affective_dialog を true に設定します。
Python
client = genai.Client(http_options={"api_version": "v1alpha"})
config = types.LiveConnectConfig(
    response_modalities=["AUDIO"],
    enable_affective_dialog=True
)
JavaScript
const ai = new GoogleGenAI({ httpOptions: {"apiVersion": "v1alpha"} });
const config = {
  responseModalities: [Modality.AUDIO],
  enableAffectiveDialog: true
};
感情的なダイアログは、現在、ネイティブ音声出力モデルでのみサポートされています。
コンテキストに応じた音声にのみ対応
この機能が有効になっている場合、コンテンツが関連性のないものであると Gemini が判断した場合は、応答しないようにすることができます。
これを使用するには、API バージョンを v1alpha に設定し、セットアップ メッセージの proactivity フィールドを構成して、proactive_audio を true に設定します。
Python
client = genai.Client(http_options={"api_version": "v1alpha"})
config = types.LiveConnectConfig(
    response_modalities=["AUDIO"],
    proactivity={'proactive_audio': True}
)
JavaScript
const ai = new GoogleGenAI({ httpOptions: {"apiVersion": "v1alpha"} });
const config = {
  responseModalities: [Modality.AUDIO],
  proactivity: { proactiveAudio: true }
}
プロアクティブな音声は、現在、ネイティブ音声出力モデルでのみサポートされています。
思考
最新のネイティブ音声出力モデル gemini-2.5-flash-native-audio-preview-09-2025 は、思考能力をサポートしており、動的思考がデフォルトで有効になっています。
thinkingBudget パラメータは、回答の生成時に使用する思考トークンの数に関するガイダンスをモデルに提供します。thinkingBudget を 0 に設定すると、思考を無効にできます。モデルの thinkingBudget 構成の詳細については、思考予算のドキュメントをご覧ください。
Python
model = "gemini-2.5-flash-native-audio-preview-09-2025"
config = types.LiveConnectConfig(
    response_modalities=["AUDIO"]
    thinking_config=types.ThinkingConfig(
        thinking_budget=1024,
    )
)
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-native-audio-preview-09-2025';
const config = {
  responseModalities: [Modality.AUDIO],
  thinkingConfig: {
    thinkingBudget: 1024,
  },
};
async function main() {
  const session = await ai.live.connect({
    model: model,
    config: config,
    callbacks: ...,
  });
  // Send audio input and receive audio
  session.close();
}
main();
また、構成で includeThoughts を true に設定すると、思考の要約を有効にできます。詳しくは、思考の要約をご覧ください。
Python
model = "gemini-2.5-flash-native-audio-preview-09-2025"
config = types.LiveConnectConfig(
    response_modalities=["AUDIO"]
    thinking_config=types.ThinkingConfig(
        thinking_budget=1024,
        include_thoughts=True
    )
)
JavaScript
const model = 'gemini-2.5-flash-native-audio-preview-09-2025';
const config = {
  responseModalities: [Modality.AUDIO],
  thinkingConfig: {
    thinkingBudget: 1024,
    includeThoughts: true,
  },
};
音声アクティビティ検出(VAD)
音声アクティビティ検出(VAD)により、モデルは人が話しているときを認識できます。これは、ユーザーがいつでもモデルとのやり取りを中断できるようにするため、自然な会話を作成するうえで不可欠です。
VAD が中断を検出すると、進行中の生成はキャンセルされ、破棄されます。クライアントにすでに送信された情報だけが、セッション履歴に保持されます。その後、サーバーは中断を報告する BidiGenerateContentServerContent メッセージを送信します。
Gemini サーバーは、保留中の関数呼び出しを破棄し、キャンセルされた呼び出しの ID を記載した BidiGenerateContentServerContent メッセージを送信します。
Python
async for response in session.receive():
    if response.server_content.interrupted is True:
        # The generation was interrupted
        # If realtime playback is implemented in your application,
        # you should stop playing audio and clear queued playback here.
JavaScript
const turns = await handleTurn();
for (const turn of turns) {
  if (turn.serverContent && turn.serverContent.interrupted) {
    // The generation was interrupted
    // If realtime playback is implemented in your application,
    // you should stop playing audio and clear queued playback here.
  }
}
自動 VAD
デフォルトでは、モデルは連続した音声入力ストリームに対して VAD を自動的に実行します。VAD は、セットアップ構成の realtimeInputConfig.automaticActivityDetection フィールドで構成できます。
音声ストリームが 1 秒以上一時停止すると(たとえば、ユーザーがマイクをオフにした場合)、キャッシュに保存された音声をフラッシュするために audioStreamEnd イベントを送信する必要があります。クライアントはいつでも音声データの送信を再開できます。
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()
model = "gemini-live-2.5-flash-preview"
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())
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({});
const model = 'gemini-live-2.5-flash-preview';
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();
send_realtime_input を使用すると、API は VAD に基づいて音声に自動的に応答します。send_client_content はメッセージをモデル コンテキストに順番に追加しますが、send_realtime_input は決定論的順序を犠牲にして応答性を最適化します。
VAD の自動構成
VAD アクティビティをより詳細に制御するには、次のパラメータを構成します。詳細については、API リファレンスをご覧ください。
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,
    }
  }
};
自動 VAD を無効にする
また、セットアップ メッセージで realtimeInputConfig.automaticActivityDetection.disabled を true に設定することで、自動 VAD を無効にすることもできます。この構成では、クライアントがユーザーの音声の検出と、適切なタイミングでの activityStart メッセージと activityEnd メッセージの送信を行います。この構成では audioStreamEnd は送信されません。代わりに、ストリームの中断は activityEnd メッセージでマークされます。
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: {} })
トークン数
消費されたトークンの合計数は、返されたサーバー メッセージの usageMetadata フィールドで確認できます。
Python
async for message in session.receive():
    # The server will periodically send messages that include UsageMetadata.
    if message.usage_metadata:
        usage = message.usage_metadata
        print(
            f"Used {usage.total_token_count} tokens in total. Response token breakdown:"
        )
        for detail in usage.response_tokens_details:
            match detail:
                case types.ModalityTokenCount(modality=modality, token_count=count):
                    print(f"{modality}: {count}")
JavaScript
const turns = await handleTurn();
for (const turn of turns) {
  if (turn.usageMetadata) {
    console.debug('Used %s tokens in total. Response token breakdown:\n', turn.usageMetadata.totalTokenCount);
    for (const detail of turn.usageMetadata.responseTokensDetails) {
      console.debug('%s\n', detail);
    }
  }
}
メディアの解像度
セッション構成の一部として mediaResolution フィールドを設定することで、入力メディアのメディア解像度を指定できます。
Python
from google.genai import types
config = {
    "response_modalities": ["AUDIO"],
    "media_resolution": types.MediaResolution.MEDIA_RESOLUTION_LOW,
}
JavaScript
import { GoogleGenAI, Modality, MediaResolution } from '@google/genai';
const config = {
    responseModalities: [Modality.TEXT],
    mediaResolution: MediaResolution.MEDIA_RESOLUTION_LOW,
};
制限事項
プロジェクトを計画する際は、Live API の次の制限事項を考慮してください。
応答のモダリティ
セッション構成でセッションごとに設定できるレスポンス モダリティ(TEXT または AUDIO)は 1 つだけです。両方を設定すると、構成エラー メッセージが表示されます。つまり、同じセッションでテキストまたは音声のいずれかで応答するようにモデルを構成できますが、両方で応答するように構成することはできません。
クライアント認証
Live API は、デフォルトでサーバー間認証のみを提供します。クライアントからサーバーへのアプローチを使用して Live API アプリケーションを実装する場合は、エフェメラル トークンを使用してセキュリティ リスクを軽減する必要があります。
セッション継続時間
音声のみのセッションは 15 分に制限され、音声と動画のセッションは 2 分に制限されます。ただし、セッション継続時間を無制限に延長するために、さまざまなセッション管理手法を構成できます。
コンテキスト ウィンドウ
セッションのコンテキスト ウィンドウの上限は次のとおりです。
- ネイティブ音声出力モデルの 128,000 トークン
 - 他の Live API モデルの場合は 32,000 トークン
 
サポートされている言語
Live API は次の言語をサポートしています。
| 言語 | BCP-47 コード | 言語 | BCP-47 コード | 
|---|---|---|---|
| ドイツ語(ドイツ) | de-DE | 
         英語(オーストラリア)* | en-AU | 
      
| 英語(英国)* | en-GB | 
         英語(インド) | en-IN | 
      
| 英語(米国) | en-US | 
         スペイン語(米国) | es-US | 
      
| フランス語(フランス) | fr-FR | 
         ヒンディー語(インド) | hi-IN | 
      
| ポルトガル語(ブラジル) | pt-BR | 
         アラビア語(一般) | ar-XA | 
      
| スペイン語(スペイン)* | es-ES | 
         フランス語(カナダ)* | fr-CA | 
      
| インドネシア語(インドネシア) | id-ID | 
         イタリア語(イタリア) | it-IT | 
      
| 日本語(日本) | ja-JP | 
         トルコ語(トルコ) | tr-TR | 
      
| ベトナム語(ベトナム) | vi-VN | 
         ベンガル語(インド) | bn-IN | 
      
| グジャラート語(インド)* | gu-IN | 
         カンナダ語(インド)* | kn-IN | 
      
| マラーティー語(インド) | mr-IN | 
         マラヤーラム語(インド)* | ml-IN | 
      
| タミル語(インド) | ta-IN | 
         テルグ語(インド) | te-IN | 
      
| オランダ語(オランダ) | nl-NL | 
         韓国語(韓国) | ko-KR | 
      
| 標準中国語(中国)* | cmn-CN | 
         ポーランド語(ポーランド) | pl-PL | 
      
| ロシア語(ロシア) | ru-RU | 
         タイ語(タイ) | th-TH | 
      
アスタリスク(*)の付いた言語は、ネイティブ音声ではご利用いただけません。
次のステップ
- Live API を効果的に使用するための重要な情報については、ツールの使用ガイドとセッション管理ガイドをご覧ください。
 - Google AI Studio で Live API を試す。
 - Live API モデルの詳細については、モデルページの Gemini 2.0 Flash Live と Gemini 2.5 Flash Native Audio をご覧ください。
 - Live API クックブック、Live API Tools クックブック、Live API スタートガイド スクリプトで、他の例をお試しください。