Repository navigation
fix(live): handle goAway and voice activity notifications - #1878
YuqiGuo105 wants to merge 1 commit into
Conversation
|
Hi @YuqiGuo105, Thank you for your contribution and for taking the time to submit this pull request. Our team is currently reviewing your changes, and we will reach out if we need any further information. |
| /** A Live server warning that the connection will close; does not complete the model turn. */ | ||
| @JsonProperty("goAway") | ||
| public Optional<LiveServerGoAway> goAway() { | ||
| return Optional.ofNullable(goAway); | ||
| } | ||
|
|
||
| public void setGoAway(@Nullable LiveServerGoAway goAway) { | ||
| this.goAway = goAway; | ||
| } |
There was a problem hiding this comment.
DESIGN (HIGH): This exposes go-away as a public Event field that callers receive and the session stores. ADK Python consumes go_away in the live flow and reconnects (v1, main). ADK Kotlin does the same and has no goAway on Event. Once Java adds reconnection, this field will go unused or need removal.
In my live run, the server closed the socket 30 s after goAway, and runLive neither completed nor failed. For now, this event is the caller's only warning.
Handling goAway inside BaseLlmFlow is better alternative here
| && goAway().isEmpty() | ||
| && voiceActivity().isEmpty() |
There was a problem hiding this comment.
DESIGN (MEDIUM): Please drop this change to finalResponse(). ADK Python's is_final_response() and ADK Kotlin's isFinalResponse both return true for these events (google-adk 2.15.1: voice_activity=True go_away=True). Java's transcription-only and usage-only events stay final too. Nothing in the repo relies on this change: removing those two lines fails only the new assertions. It also affects events that carry content: a model response with text and voiceActivity makes runAsync loop until maxLlmCalls (500).
| // This lower-level VAD signal has no ADK event representation. Recognize it | ||
| // without treating it as an error or as the end of the model turn. |
There was a problem hiding this comment.
CLEANUP (LOW): A one-line comment is enough here, for example: // No ADK event carries this signal; recognize it so it is not reported as unknown.
| public void event_liveControlFields_roundTripAndCopy_preservePayloads() { | ||
| LiveServerGoAway goAway = LiveServerGoAway.builder().timeLeft(Duration.ofMillis(30500)).build(); | ||
| VoiceActivity voiceActivity = | ||
| VoiceActivity.builder() | ||
| .voiceActivityType("ACTIVITY_END") | ||
| .audioOffset(Duration.ofMillis(1250)) | ||
| .build(); | ||
| Event event = EVENT.toBuilder().goAway(goAway).voiceActivity(voiceActivity).build(); | ||
|
|
||
| Event restored = Event.fromJson(event.toJson()); | ||
| assertThat(restored).isEqualTo(event); | ||
| assertThat(restored.goAway()).hasValue(goAway); | ||
| assertThat(restored.voiceActivity()).hasValue(voiceActivity); | ||
| Event copied = event.toBuilder().build(); | ||
| assertThat(copied).isEqualTo(event); | ||
| assertThat(copied.hashCode()).isEqualTo(event.hashCode()); | ||
| assertThat(copied.toBuilder().goAway(null).voiceActivity(null).build()).isEqualTo(EVENT); | ||
| } | ||
|
|
||
| @Test | ||
| public void event_liveControlFields_defaultAndNull_areAbsent() { | ||
| for (String json : ImmutableList.of("{}", "{\"goAway\":null,\"voiceActivity\":null}")) { | ||
| Event event = Event.fromJson(json); | ||
| assertThat(event.goAway()).isEmpty(); | ||
| assertThat(event.voiceActivity()).isEmpty(); | ||
| assertThat(event.toJson()).doesNotContain("\"goAway\""); | ||
| assertThat(event.toJson()).doesNotContain("\"voiceActivity\""); | ||
|
|
||
| event.setGoAway(LiveServerGoAway.builder().build()); | ||
| event.setVoiceActivity(VoiceActivity.builder().build()); | ||
| assertThat(event.goAway()).isPresent(); | ||
| assertThat(event.voiceActivity()).isPresent(); | ||
| event.setGoAway(null); | ||
| event.setVoiceActivity(null); | ||
| assertThat(event.goAway()).isEmpty(); | ||
| assertThat(event.voiceActivity()).isEmpty(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void event_equals_distinguishesLiveControlFields() { | ||
| assertThat(EVENT.toBuilder().goAway(LiveServerGoAway.builder().build()).build()) | ||
| .isNotEqualTo(EVENT); | ||
| assertThat(EVENT.toBuilder().voiceActivity(VoiceActivity.builder().build()).build()) | ||
| .isNotEqualTo(EVENT); | ||
| } | ||
|
|
||
| @Test | ||
| public void event_finalResponse_liveNotifications_areNotFinalAnswers() { | ||
| assertThat(Event.builder().goAway(LiveServerGoAway.builder().build()).build().finalResponse()) | ||
| .isFalse(); | ||
| for (String type : ImmutableList.of("ACTIVITY_START", "ACTIVITY_END")) { | ||
| assertThat( | ||
| Event.builder() | ||
| .voiceActivity(VoiceActivity.builder().voiceActivityType(type).build()) | ||
| .build() | ||
| .finalResponse()) | ||
| .isFalse(); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
CLEANUP (LOW): Several new tests loop over cases or check multiple behaviors at once. For instance, event_liveControlFields_roundTripAndCopy_preservePayloads tests JSON, copying, equals, hashCode, and resetting to null. Please split these into one behavior per test, or parameterize them. Some tests also overlap: controlMessages_preserveEmptyPayloads duplicates controlsAlongsideContentAndUsage_preservesAllFields, and receive_goAway_doesNotCloseConnectionOrStopLaterResponses guards a close path the connection does not have.
Link to Issue or Description of Change
Closes #1872.
Problem
GeminiLlmConnectiontreats valid LivegoAway,voiceActivity, andvoiceActivityDetectionSignalmessages as unknown-message errors. In particular, thegoAway.timeLeftwarning never reaches callers. Simply recognizing these messages in the connection is insufficient: the flow also discards contentless responses unless their metadata is explicitly preserved.Solution
goAwayandvoiceActivityfields toLlmResponseandEvent, following ADK Python's response fields. Preserve them through the flow, JSON round-trips, builders, and event equality.goAwayandvoiceActivitynotifications without reporting an error. Recognize the lower-levelvoiceActivityDetectionSignalwithout emitting an event, since it has no ADK event representation.This change exposes the warning; it does not implement automatic reconnection/session resumption or address SDK-level wire parsing issues.
Testing Plan
Automated tests
GeminiLlmConnectionTest,LlmResponseTest,EventTest,BaseLlmFlowTest, andRunnerTest.Runner.runLive.goAwaydoes not close the connection or prevent later responses.Validation on the latest fetched
main(dde4b23d), using Maven 3.9.16 and Temurin 25.0.3 (Java release 17):mvn -o -B -pl core -am clean testBoth main core test executions (
default-testandbasic) report 4,177 tests, 0 failures, 0 errors, and 35 skipped. The additional configured Vertex AI/Apigee/tracking-header executions also pass. These are repeated/configured executions, not distinct-test counts to be added together.The illegal-Optional-parameter check passes for all changed production files. Running that optional profile across all core sources previously reported 12 violations in seven unchanged files; those are outside this patch.
Manual End-to-End (E2E) Tests
Not performed against a real Gemini Live endpoint; automated tests use simulated messages and a fake transport/model. To verify manually:
Runner.runLivesession and inspect emitted events.goAwaywarning, verifyevent.goAway()preservestimeLeft, with no unknown-message error and no immediate client-initiated close.event.voiceActivity()carries the activity type without being classified as a final answer.Checklist