Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gemini-tts-31-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/agents-plugin-google': patch
---

Update Gemini TTS to default to Gemini 3.1 Flash TTS preview and stream generated audio chunks.
42 changes: 42 additions & 0 deletions examples/src/google_gemini_tts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { type JobContext, ServerOptions, cli, defineAgent, voice } from '@livekit/agents';
import * as deepgram from '@livekit/agents-plugin-deepgram';
import * as google from '@livekit/agents-plugin-google';
import { BackgroundVoiceCancellation } from '@livekit/noise-cancellation-node';
import { fileURLToPath } from 'node:url';

class GeminiTTSAgent extends voice.Agent {
async onEnter() {
this.session.generateReply({ instructions: 'greet the user and introduce yourself' });
}
}

export default defineAgent({
entry: async (ctx: JobContext) => {
const agent = new GeminiTTSAgent({
instructions: 'Your name is Kelly. Respond briefly and concisely using voice conversation.',
});

const session = new voice.AgentSession({
stt: new deepgram.STT(),
llm: new google.LLM({ model: 'gemini-2.5-flash' }),
tts: new google.beta.TTS({
apiKey: process.env.GOOGLE_API_KEY,
voiceName: 'Kore',
model: 'gemini-3.1-flash-tts-preview',
}),
});

await session.start({
agent,
room: ctx.room,
inputOptions: {
noiseCancellation: BackgroundVoiceCancellation(),
},
});
},
});

cli.runApp(new ServerOptions({ agent: fileURLToPath(import.meta.url) }));
62 changes: 59 additions & 3 deletions plugins/google/src/beta/gemini_tts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,65 @@
// SPDX-License-Identifier: Apache-2.0
import { STT } from '@livekit/agents-plugin-openai';
import { tts } from '@livekit/agents-plugins-test';
import { describe } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { TTS } from './gemini_tts.js';

describe.skip('Google Gemini TTS', async () => {
await tts(new TTS(), new STT());
const { generateContentStream } = vi.hoisted(() => ({
generateContentStream: vi.fn(),
}));

vi.mock('@google/genai', () => ({
GoogleGenAI: vi.fn(function GoogleGenAI() {
return {
models: {
generateContentStream,
},
};
}),
}));

describe('Google Gemini TTS integration', () => {
it.skip('synthesizes with live providers', async () => {
await tts(new TTS(), new STT());
});
});

describe('Google Gemini TTS', () => {
it('synthesizes audio from a streamed Gemini response', async () => {
const audioChunk = Buffer.alloc(4800);

generateContentStream.mockImplementation(async function* () {
yield buildResponseChunk(audioChunk);
yield buildResponseChunk(audioChunk);
});

const stream = new TTS({ apiKey: 'test-api-key' }).synthesize('Hello world');
let audioCount = 0;

for await (const _frame of stream) {
audioCount += 1;
}

expect(generateContentStream).toHaveBeenCalledOnce();
expect(audioCount).toBeGreaterThan(0);
});
});

function buildResponseChunk(data: Buffer) {
return {
candidates: [
{
content: {
parts: [
{
inlineData: {
data: data.toString('base64'),
mimeType: 'audio/pcm',
},
},
],
},
},
],
};
}
52 changes: 27 additions & 25 deletions plugins/google/src/beta/gemini_tts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type GeminiVoices =
| 'Sadaltager'
| 'Sulafat';

const DEFAULT_MODEL: GeminiTTSModels = 'gemini-2.5-flash-lite-preview-tts';
const DEFAULT_MODEL: GeminiTTSModels = 'gemini-3.1-flash-tts-preview';
const DEFAULT_VOICE: GeminiVoices = 'Kore';
const DEFAULT_SAMPLE_RATE = 24000; // not configurable
const NUM_CHANNELS = 1;
Expand Down Expand Up @@ -234,15 +234,38 @@ export class ChunkedStream extends tts.ChunkedStream {
];

try {
let lastFrame: AudioFrame | undefined;
const sendLastFrame = (final: boolean) => {
if (lastFrame) {
this.queue.put({
requestId,
frame: lastFrame,
segmentId: requestId,
final,
});
lastFrame = undefined;
}
};

const responseStream = await this.#tts.client.models.generateContentStream({
model: this.#tts.opts.model,
contents,
config,
});

for await (const response of responseStream) {
await this.#processResponse(response, bstream, requestId);
await this.#processResponse(response, bstream, (frame) => {
sendLastFrame(false);
lastFrame = frame;
});
}

for (const frame of bstream.flush()) {
sendLastFrame(false);
lastFrame = frame;
}
Comment on lines +263 to +266

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 AudioByteStream.flush() produces 0-sample frame when buffer is empty

When all audio data aligns perfectly with the AudioByteStream frame size (bytesPerFrame = 4800 bytes for 24kHz mono at 100ms), the internal buffer is empty at flush time. AudioByteStream.flush() at agents/src/audio.ts:76-93 doesn't guard against an empty buffer — it always returns an array with one frame, even if that frame has 0 samples. This 0-sample frame becomes lastFrame and is emitted as the final: true frame. The resemble plugin guards against this (plugins/resemble/src/tts.ts:155: if (frame.samplesPerChannel === 0) continue), but most other plugins don't. This is a pre-existing issue not introduced by this PR — the old Gemini code had the same exposure.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.


sendLastFrame(true);
} catch (error: unknown) {
if (error instanceof Error && error.name === 'AbortError') {
return;
Expand Down Expand Up @@ -298,7 +321,7 @@ export class ChunkedStream extends tts.ChunkedStream {
async #processResponse(
response: types.GenerateContentResponse,
bstream: AudioByteStream,
requestId: string,
onFrame: (frame: AudioFrame) => void,
) {
if (!response.candidates || response.candidates.length === 0) {
return;
Expand All @@ -309,36 +332,15 @@ export class ChunkedStream extends tts.ChunkedStream {
return;
}

let lastFrame: AudioFrame | undefined;
const sendLastFrame = (final: boolean) => {
if (lastFrame) {
this.queue.put({
requestId,
frame: lastFrame,
segmentId: requestId,
final,
});
lastFrame = undefined;
}
};

for (const part of candidate.content.parts) {
if (part.inlineData?.data && part.inlineData.mimeType?.startsWith('audio/')) {
const audioBuffer = Buffer.from(part.inlineData.data, 'base64');

for (const frame of bstream.write(audioBuffer)) {
sendLastFrame(false);
lastFrame = frame;
onFrame(frame);
}
}
}

for (const frame of bstream.flush()) {
sendLastFrame(false);
lastFrame = frame;
}

sendLastFrame(true);
}
}

Expand Down