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/google-realtime-restart-on-abnormal-close.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/agents-plugin-google': patch
---

Gemini realtime: restart the session when the WebSocket closes abnormally (e.g. code 1006), mirroring the network-error path - the main task reconnects and re-seeds the chat context instead of leaving the session parked on a dead socket. The emitted error is marked recoverable when a restart will happen.
10 changes: 9 additions & 1 deletion plugins/google/src/realtime/realtime_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,11 @@ export class RealtimeSession extends llm.RealtimeSession {
const errorMsg = event.reason || `WebSocket closed with code ${event.code}`;
this.#logger.error(`Gemini Live session error: ${errorMsg}${truncationNote}`);

// An abnormal close is the same failure as `onerror`'s
// network-level errors: restart so the main task reconnects and
// re-seeds the chat context, instead of leaving the session
// parked forever on a dead socket.
const willRestart = !this.sessionShouldClose.isSet;
this.emitError(
new APIStatusError({
message: `${errorMsg}${truncationNote}`,
Expand All @@ -1068,8 +1073,11 @@ export class RealtimeSession extends llm.RealtimeSession {
: null,
},
}),
false,
willRestart,
);
if (willRestart) {
this.markRestartNeeded();
}
} else {
this.#logger.debug('Gemini Live session closed:', event.code, event.reason);
}
Expand Down
85 changes: 85 additions & 0 deletions plugins/google/src/realtime/realtime_reconnect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import type * as genai from '@google/genai';
import { describe, expect, it, vi } from 'vitest';
import { RealtimeModel } from './realtime_api.js';

type LiveCallbacks = {
onopen: () => void;
onmessage: (message: unknown) => void;
onerror: (error: unknown) => void;
onclose: (event: { code: number; reason: string }) => void;
};

const { connects } = vi.hoisted(() => ({
connects: [] as Array<{ callbacks: LiveCallbacks }>,
}));

vi.mock('@google/genai', async (importOriginal) => {
const actual = await importOriginal<typeof genai>();
return {
...actual,
GoogleGenAI: class {
live = {
connect: async ({ callbacks }: { callbacks: LiveCallbacks }) => {
connects.push({ callbacks });
callbacks.onopen();
return {
sendClientContent: () => {},
sendRealtimeInput: () => {},
sendToolResponse: () => {},
close: () => {},
};
},
};
},
};
});

/**
* An abnormal WebSocket close is the same failure as `onerror`'s network-level
* errors: without a restart the main task stays parked on
* `sessionShouldClose.wait()` with a dead socket, and the session never
* recovers.
*/
describe('RealtimeSession on abnormal WebSocket close', () => {
it('restarts the session and emits the error as recoverable', async () => {
connects.length = 0;
const session = new RealtimeModel({
model: 'gemini-2.0-flash-live-001',
apiKey: 'test-key',
}).session();
const errors: Array<{ error: Error; recoverable: boolean }> = [];
session.on('error', (ev) => errors.push(ev));

await vi.waitFor(() => expect(connects).toHaveLength(1));
connects[0]!.callbacks.onclose({ code: 1006, reason: '' });

await vi.waitFor(() => expect(connects).toHaveLength(2));
expect(errors).toHaveLength(1);
expect(errors[0]!.recoverable).toBe(true);
expect(errors[0]!.error.message).toContain('WebSocket closed with code 1006');

await session.close();
});

it('does not restart or emit on a normal close', async () => {
connects.length = 0;
const session = new RealtimeModel({
model: 'gemini-2.0-flash-live-001',
apiKey: 'test-key',
}).session();
const errors: Array<{ error: Error; recoverable: boolean }> = [];
session.on('error', (ev) => errors.push(ev));

await vi.waitFor(() => expect(connects).toHaveLength(1));
connects[0]!.callbacks.onclose({ code: 1000, reason: '' });

await new Promise((resolve) => setTimeout(resolve, 100));
expect(connects).toHaveLength(1);
expect(errors).toHaveLength(0);

await session.close();
});
});