Skip to content

fix(live): resolve Protocol 1007 collisions during active audio sessions#5097

Open
jordac wants to merge 2 commits intogoogle:mainfrom
jordac:fix/live-session-1007-protocol-collision
Open

fix(live): resolve Protocol 1007 collisions during active audio sessions#5097
jordac wants to merge 2 commits intogoogle:mainfrom
jordac:fix/live-session-1007-protocol-collision

Conversation

@jordac
Copy link
Copy Markdown

@jordac jordac commented Apr 2, 2026

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Or, if no issue exists, describe the change:

Problem:
I noticed while experimenting with the 3.1 live api + ADK that the websockets were disconnecting and I couldn't maintain a connection to the adk after trying on 2.0.2a and 1.27 and some debugging identified where the issue was occurring. I had no problems when using the gemini genai api directly so it must be ADK? So I dug into the issue and patched it to get my use-case going (yay). While i was drafting this PR I saw the recent contribution to the main branch, (commit 80828936 closing #5018) successfully fixing the path for text-routing strictness for the 3.1 API endpoint. My findings where that whenever bidirectional audio is enabled by the live content config (needed for newer models), attempting to transport text was causing the 1007 error. So sending text before the audio instantly triggered 1007 collisions.

Solution:
To resolve this websocket connectivity issue, this PR adds on to the changes in 80828936:

  1. Dynamic Media Blob Gating: Introduces an _audio_active boolean initialized directly from live_config.response_modalities so that GeminiLlmConnection more defensively routes media blobs during what it considers audio only sessions.
  2. Universal Routing Logic: Synthesizes alongside the existing 3.1 check to ensure all text and Blob payloads are safely multiplexed and re-routed through send_realtime_input() during any active audio scenario, preventing session corruption (those 1007's).
  3. Hardened Typing: Further hardens the realtime blob ingestion pipeline with strict isinstance(..., str) evaluations to eliminate generic truthiness exceptions.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
  • Local integration tests utilizing this as part of a seperate project as well where these fixes were required.

Please include a summary of passed pytest results.

Executed full local pytest suite ensuring 100% downstream passing output organically (75 passed).

  • Patched and updated test_gemini_llm_connection.py to evaluate dynamic routing paths for explicit audio logic execution natively.
  • Implemented targeted tests verifying 100% coverage on the legacy media= routing fallback conditional pathway.

Manual End-to-End (E2E) Tests:

Verified via custom hardware/PyAudio ingestion script tracking active Live API routing manually against the gemini-3.1-flash-live-preview engine.

  • Result: Zero 1007 or 1011 protocol crashes occur. The handshake succeeds natively, the mic stream routes cleanly over the Bidi channel, and text injections trigger natively over send_realtime_input mitigating the session closures entirely.

I've run successful e2e testing confirming payloads/messages successfully in an isolated test for iterating on the fix and also as part of a seperate project the instantiates the ADK for a usecase.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

This PR builds directly on top of the recent upstream merges (specifically commit 80828936 closing #5018) that introduced send_realtime_input() routing for Gemini 3.1 Flash Live, ensuring complete multiplexing compatibility uniformly across the ADK surface.

cc @wuliang229 — Thanks for getting this fix out, this is gonna to enable some cool use-cases. I was about to share my changes in a PR for the 1007 collision errors but saw you had just pushed a fix, so I incorporated your changes generalizing them slightly, into my branch, my changes do some extra type checking and leverage the LiveContentConfig.response_modalities as a deterministic logic gate. Doing that shifts the responsbility more towards whoever is invoking it upstream, if there is a different approach planned, I can go ahead and close this PR. Thanks for reading.

@adk-bot adk-bot added the live [Component] This issue is related to live, voice and video chat label Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

live [Component] This issue is related to live, voice and video chat

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ADK does not support the newest gemini-3.1-flash-live-preview

2 participants