Skip to content
Closed
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
16 changes: 16 additions & 0 deletions src/google/adk/agents/live_request_queue.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,6 +53,14 @@ class LiveRequest(BaseModel):
activity_end: Optional[types.ActivityEnd] = None
"""If set, signal the end of user activity to the model.

When multiple fields are set, they are processed by priority (highest first):
activity_start > activity_end > blob > content. state_delta, if set, is always
applied regardless of the other fields.
"""
audio_stream_end: Optional[bool] = None
"""If set, signal the end of the audio stream to force flush buffered audio
when VAD (voice activity detection) is enabled.

When multiple fields are set, they are processed by priority (highest first):
activity_start > activity_end > blob > content. state_delta, if set, is always
applied regardless of the other fields.
Expand DownExpand Up@@ -92,6 +100,14 @@ def send_activity_end(self) -> None:
"""Sends an activity end signal to mark the end of user input."""
self._queue.put_nowait(LiveRequest(activity_end=types.ActivityEnd()))

def send_audio_stream_end(self) -> None:
"""Sends an audio stream end signal to force flush buffered audio.

Use this to signal the end of an audio stream when VAD is enabled,
which causes the model to process any buffered audio immediately.
"""
self._queue.put_nowait(LiveRequest(audio_stream_end=True))

def send(self, req: LiveRequest) -> None:
self._queue.put_nowait(req)

Expand Down
2 changes: 2 additions & 0 deletions src/google/adk/flows/llm_flows/base_llm_flow.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -827,6 +827,8 @@ async def _send_to_model(
await llm_connection.send_realtime(types.ActivityStart())
elif live_request.activity_end:
await llm_connection.send_realtime(types.ActivityEnd())
elif live_request.audio_stream_end:
await llm_connection.send_realtime(True)
elif live_request.blob:
# Cache input audio chunks before flushing
self.audio_cache_manager.cache_audio(
Expand Down
5 changes: 4 additions & 1 deletion src/google/adk/models/gemini_llm_connection.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,7 +29,7 @@

logger = logging.getLogger('google_adk.' + __name__)

RealtimeInput = Union[types.Blob, types.ActivityStart, types.ActivityEnd]
RealtimeInput = Union[types.Blob, types.ActivityStart, types.ActivityEnd, bool]
from typing import TYPE_CHECKING

if TYPE_CHECKING:
Expand DownExpand Up@@ -173,6 +173,9 @@ async def send_realtime(self, input: RealtimeInput) -> None:
elif isinstance(input, types.ActivityEnd):
logger.debug('Sending LLM activity end signal.')
await self._gemini_session.send_realtime_input(activity_end=input)
elif isinstance(input, bool) and input:
logger.debug('Sending LLM audio stream end signal.')
await self._gemini_session.send_realtime_input(audio_stream_end=True)
else:
raise ValueError('Unsupported input type: %s' % type(input))

Expand Down
12 changes: 12 additions & 0 deletions tests/unittests/models/test_gemini_llm_connection.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -89,6 +89,18 @@ async def test_send_realtime_audio_uses_audio_channel_for_live_translate(
)


@pytest.mark.asyncio
async def test_send_realtime_audio_stream_end(
gemini_connection, mock_gemini_session
):
"""Test send_realtime with audio_stream_end=True sends the flush signal."""
await gemini_connection.send_realtime(True)

mock_gemini_session.send_realtime_input.assert_called_once_with(
audio_stream_end=True
)


@pytest.mark.asyncio
async def test_send_history(gemini_connection, mock_gemini_session):
"""Test send_history method."""
Expand Down