From c4d5e3b29802268e13dfd7119c4605534df3d844 Mon Sep 17 00:00:00 2001 From: Hangfei Lin Date: Tue, 13 May 2025 10:56:37 -0700 Subject: [PATCH] feat: Align run_live interface with run_async etc PiperOrigin-RevId: 758289318 --- src/google/adk/runners.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/google/adk/runners.py b/src/google/adk/runners.py index 529c467..28cf655 100644 --- a/src/google/adk/runners.py +++ b/src/google/adk/runners.py @@ -19,6 +19,7 @@ import logging import queue import threading from typing import AsyncGenerator, Generator, Optional +import warnings from deprecated import deprecated from google.genai import types @@ -244,25 +245,52 @@ class Runner: async def run_live( self, *, - session: Session, + user_id: Optional[str] = None, + session_id: Optional[str] = None, live_request_queue: LiveRequestQueue, run_config: RunConfig = RunConfig(), + session: Optional[Session] = None, ) -> AsyncGenerator[Event, None]: """Runs the agent in live mode (experimental feature). Args: - session: The session to use. + session: The session to use. This parameter is deprecated, please use + `user_id` and `session_id` instead. + user_id: The user ID for the session. Required if `session` is None. + session_id: The session ID for the session. Required if `session` is + None. live_request_queue: The queue for live requests. run_config: The run config for the agent. Yields: - The events generated by the agent. + AsyncGenerator[Event, None]: An asynchronous generator that yields + `Event` + objects as they are produced by the agent during its live execution. .. warning:: This feature is **experimental** and its API or behavior may change in future releases. + + .. note:: + Either `session` or both `user_id` and `session_id` must be provided. """ - # TODO: right now, only works for a single audio agent without FC. + if session is None and (user_id is None or session_id is None): + raise ValueError( + 'Either session or user_id and session_id must be provided.' + ) + if session is not None: + warnings.warn( + 'The `session` parameter is deprecated. Please use `user_id` and' + ' `session_id` instead.', + DeprecationWarning, + stacklevel=2, + ) + if not session: + session = self.session_service.get_session( + app_name=self.app_name, user_id=user_id, session_id=session_id + ) + if not session: + raise ValueError(f'Session not found: {session_id}') invocation_context = self._new_invocation_context_for_live( session, live_request_queue=live_request_queue,