Skip to content

Live API: no way to observe session close or connection errors after setup #1153

Description

@subhashpolisetti

Summary

After a Live session is established, a caller has no way to be notified that it ended. When the server closes the session (e.g. after GoAway) or the WebSocket drops, AsyncLive.GenAiWebSocketClient only logs onClosed/onFailure nothing reaches the receive() message consumer or any future, so the application can't react. receive() itself returns an already-completed future (its Javadoc notes it only signals registration), so it can't carry the signal either.

Root cause

In AsyncLive.GenAiWebSocketClient, once sessionFuture is done (i.e. for the whole active session) these just log:

public void onClosed(WebSocket webSocket, int code, String reason) {
    logger.log(Level.INFO, "Live session closed ...");
    if (!sessionFuture.isDone()) { sessionFuture.completeExceptionally(...); }  // else: only logged
}

public void onFailure(WebSocket webSocket, Throwable t, @Nullable Response response) {
    logger.log(Level.SEVERE, "Error during live session", t);
    if (!sessionFuture.isDone()) { sessionFuture.completeExceptionally(t); }    // else: only logged
}

Impact

Found while building the Gemini Live API integration for langchain4j: the wrapper can't tell the application when a session ends. A listen-only voice agent is never notified that the connection dropped the only signal today is an exception on the next send.

Suggested fix

A backward-compatible overload of receive(...), with onError/onClose invoked from onFailure/onClosed after setup (terminal, mutually exclusive, nullable); the existing receive(Consumer) delegates to it:

public CompletableFuture<Void> receive(
    Consumer<LiveServerMessage> onMessage, Consumer<Throwable> onError, Runnable onClose)

Open to a different shape (e.g. a listener interface) if you'd prefer.

Environment

com.google.genai:google-genai 1.62.0, and unchanged on current main (1.65.0-SNAPSHOT). Gemini Live API, Java.

Metadata

Metadata

Assignees

Labels

priority: p3Desirable enhancement or fix. May not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions