from __future__ import annotations import asyncio import gc import logging from collections.abc import AsyncIterator from contextlib import asynccontextmanager from typing import Any import pytest from google.auth.credentials import AnonymousCredentials from google.genai import types from livekit.agents import llm, utils from livekit.agents.metrics import RealtimeModelMetrics from livekit.plugins.google.realtime.api_proto import ClientEvents from livekit.plugins.google.realtime.realtime_api import RealtimeModel, RealtimeSession from livekit.plugins.google.utils import create_function_response pytestmark = pytest.mark.unit @pytest.mark.parametrize("model", ["gemini-3.8-live", "models/gemini-3.8-live"]) def test_gemini_3_8_live_rejects_thinking_level( monkeypatch: pytest.MonkeyPatch, model: str ) -> None: monkeypatch.setenv("GOOGLE_GENAI_USE_VERTEXAI", "true") with pytest.raises(ValueError, match="does not support thinking_level on the Gemini API"): RealtimeModel( model=model, vertexai=False, api_key="fake-key", thinking_config=types.ThinkingConfig(thinking_level=types.ThinkingLevel.LOW), ) @pytest.mark.parametrize( ("model", "vertexai"), [ ("gemini-3.8-live", True), ("publishers/google/models/gemini-3.8-live", True), ("gemini-3.8-live-extended-thinking", False), ("models/gemini-3.8-live-extended-thinking", False), ("gemini-3.1-flash-live-preview", False), ], ) async def test_thinking_level_reaches_connect_config( monkeypatch: pytest.MonkeyPatch, model: str, vertexai: bool ) -> None: thinking_config = types.ThinkingConfig(thinking_level=types.ThinkingLevel.LOW) async with _make_configured_session( monkeypatch, model=model, vertexai=vertexai, project="test-project", location="eu", credentials=AnonymousCredentials() if vertexai else None, thinking_config=thinking_config, ) as session: config = session._build_connect_config() assert config.generation_config is not None assert config.generation_config.thinking_config == thinking_config def _is_genai_client_teardown(task: asyncio.Task[Any]) -> bool: """Whether this task is a genai client's ``aclose()`` left behind by a finalizer. Keyed on the coroutine's defining module, not its name alone -- ``aclose`` is a common method name and this must not touch unrelated tasks. Coroutine objects carry no ``__module__``, hence the walk through ``cr_frame``. """ coro = task.get_coro() if not (getattr(coro, "__qualname__", "") or "").endswith(".aclose"): return False frame = getattr(coro, "cr_frame", None) module = frame.f_globals.get("__name__", "") if frame else "" return module.startswith("google.genai") @pytest.fixture(autouse=True) async def _settle_genai_finalizers() -> AsyncIterator[None]: """Finish the genai client teardown this test started, before the next one. ``AsyncClient.__del__`` schedules ``aclose()`` on whatever event loop is running when the collector reaches it, with no check for a client that was already closed explicitly -- so even the sessions this module closes properly leave a finalizer behind. Settled here, while this test still owns the loop, those tasks would otherwise surface as leaked tasks in an unrelated test in a later module. """ yield gc.collect() if pending := [ task for task in asyncio.all_tasks() if not task.done() and _is_genai_client_teardown(task) ]: await asyncio.gather(*pending, return_exceptions=True) # 10ms of silence at the output sample rate (24kHz mono, 16-bit) _PCM_FRAME = b"\x00\x01" * 240 @asynccontextmanager async def _make_session(monkeypatch: pytest.MonkeyPatch) -> AsyncIterator[RealtimeSession]: """A session whose background connect loop is stopped before it hits the network. Closed on exit so the genai http clients are released here instead of by ``AsyncClient.__del__``, which schedules ``aclose()`` on whatever event loop is running when the collector happens to reach them. """ monkeypatch.setenv("GOOGLE_API_KEY", "fake-key") session = RealtimeModel().session() # cancel the connect loop before the event loop ever schedules it, so no # websocket connection is attempted session._msg_ch.close() await utils.aio.cancel_and_wait(session._main_atask) try: yield session finally: await session.aclose() @asynccontextmanager async def _make_configured_session( monkeypatch: pytest.MonkeyPatch, **options: object ) -> AsyncIterator[RealtimeSession]: monkeypatch.setenv("GOOGLE_API_KEY", "fake-key") session = RealtimeModel(**options).session() # type: ignore[arg-type] session._msg_ch.close() await utils.aio.cancel_and_wait(session._main_atask) try: yield session finally: await session.aclose() def _audio_content(**kwargs: object) -> types.LiveServerContent: return types.LiveServerContent( model_turn=types.Content( parts=[types.Part(inline_data=types.Blob(data=_PCM_FRAME, mime_type="audio/pcm"))] ), **kwargs, # type: ignore[arg-type] ) async def _drain_generation( event: llm.GenerationCreatedEvent, ) -> tuple[str, int, list[str]]: text = "" audio_frames = 0 async for message in event.message_stream: async for chunk in message.text_stream: text += chunk async for _frame in message.audio_stream: audio_frames += 1 function_calls = [call.name async for call in event.function_stream] return text, audio_frames, function_calls @pytest.mark.parametrize("vertexai", [False, True]) @pytest.mark.parametrize("with_input_transcript", [False, True]) async def test_ttft_is_unavailable_without_a_response_start_event( monkeypatch: pytest.MonkeyPatch, vertexai: bool, with_input_transcript: bool ) -> None: async with _make_configured_session( monkeypatch, model="gemini-3.8-live", vertexai=vertexai, project="test-project", location="us", credentials=AnonymousCredentials() if vertexai else None, ) as session: collected: list[RealtimeModelMetrics] = [] session.on("metrics_collected", collected.append) session._start_new_generation() if with_input_transcript: session._handle_server_content( types.LiveServerContent(input_transcription=types.Transcription(text="Hello")) ) session._handle_server_content(_audio_content()) session._handle_usage_metadata( types.UsageMetadata(prompt_token_count=10, response_token_count=5, total_token_count=15) ) assert len(collected) == 1 assert collected[0].ttft is None assert collected[0].tokens.input_tokens == 10 assert collected[0].tokens.output_tokens == 5 assert collected[0].duration >= 0 async def test_usage_counts_tool_prompts_in_the_input_and_thoughts_in_the_output( monkeypatch: pytest.MonkeyPatch, ) -> None: # Gemini reports built-in tool prompts outside prompt_token_count and thinking outside # response_token_count. async with _make_session(monkeypatch) as session: collected: list[RealtimeModelMetrics] = [] session.on("metrics_collected", collected.append) session._start_new_generation() session._handle_usage_metadata( types.UsageMetadata( prompt_token_count=100, prompt_tokens_details=[ types.ModalityTokenCount(modality=types.MediaModality.AUDIO, token_count=100) ], tool_use_prompt_token_count=40, tool_use_prompt_tokens_details=[ types.ModalityTokenCount(modality=types.MediaModality.TEXT, token_count=40) ], response_token_count=20, thoughts_token_count=10, ) ) tokens = collected[0].tokens assert (tokens.input_tokens, tokens.input_audio_tokens, tokens.input_text_tokens) == ( 140, 100, 40, ) assert (tokens.output_tokens, tokens.output_reasoning_tokens) == (30, 10) async def test_unspoken_model_text_is_omitted_in_audio_session( monkeypatch: pytest.MonkeyPatch, ) -> None: async with _make_session(monkeypatch) as session: session._start_new_generation() gen = session._current_generation assert gen is not None session._handle_server_content( types.LiveServerContent( model_turn=types.Content( parts=[types.Part(text="call:getWeather{location:Seattle")] ), output_transcription=types.Transcription(text="Let me check."), ) ) assert gen.output_text == "Let me check." assert gen.text_ch.recv_nowait() == "Let me check." assert gen.text_ch.empty() async def test_model_text_is_forwarded_in_text_modality( monkeypatch: pytest.MonkeyPatch, ) -> None: async with _make_configured_session(monkeypatch, modalities=[types.Modality.TEXT]) as session: session._start_new_generation() gen = session._current_generation assert gen is not None session._handle_server_content( types.LiveServerContent( model_turn=types.Content(parts=[types.Part(text="Hello there.")]) ) ) assert gen.output_text == "Hello there." assert gen.text_ch.recv_nowait() == "Hello there." async def test_model_text_is_forwarded_without_output_transcription( monkeypatch: pytest.MonkeyPatch, ) -> None: async with _make_configured_session(monkeypatch, output_audio_transcription=None) as session: session._start_new_generation() gen = session._current_generation assert gen is not None session._handle_server_content( types.LiveServerContent( model_turn=types.Content(parts=[types.Part(text="Hello there.")]) ) ) assert gen.output_text == "Hello there." assert gen.text_ch.recv_nowait() == "Hello there." async def test_transcript_contains_only_output_transcription_with_audio( monkeypatch: pytest.MonkeyPatch, ) -> None: async with _make_session(monkeypatch) as session: generations: list[llm.GenerationCreatedEvent] = [] session.on("generation_created", generations.append) session._start_new_generation() session._handle_server_content( types.LiveServerContent( model_turn=types.Content(parts=[types.Part(text="call:assetGenerator{context:")]) ) ) session._handle_server_content(_audio_content()) session._handle_server_content( types.LiveServerContent(output_transcription=types.Transcription(text="Tako je!")) ) session._handle_server_content(types.LiveServerContent(generation_complete=True)) session._handle_server_content(types.LiveServerContent(turn_complete=True)) assert len(generations) == 1 assert await _drain_generation(generations[0]) == ("Tako je!", 1, []) async def test_tool_call_is_delivered_without_written_call_text( monkeypatch: pytest.MonkeyPatch, ) -> None: async with _make_session(monkeypatch) as session: generations: list[llm.GenerationCreatedEvent] = [] session.on("generation_created", generations.append) session._start_new_generation() session._handle_server_content( types.LiveServerContent( model_turn=types.Content(parts=[types.Part(text="call:getWeather{location:")]) ) ) session._handle_tool_calls( types.LiveServerToolCall( function_calls=[ types.FunctionCall(id="fc-1", name="getWeather", args={"location": "Seattle"}) ] ) ) assert len(generations) == 1 assert await _drain_generation(generations[0]) == ("", 0, ["getWeather"]) async def test_transcript_keeps_model_text_in_text_modality( monkeypatch: pytest.MonkeyPatch, ) -> None: async with _make_configured_session(monkeypatch, modalities=[types.Modality.TEXT]) as session: generations: list[llm.GenerationCreatedEvent] = [] session.on("generation_created", generations.append) session._start_new_generation() session._handle_server_content( types.LiveServerContent( model_turn=types.Content(parts=[types.Part(text="Hello there.")]), turn_complete=True, ) ) assert len(generations) == 1 assert await _drain_generation(generations[0]) == ("Hello there.", 0, []) async def test_transcript_keeps_model_text_without_output_transcription( monkeypatch: pytest.MonkeyPatch, ) -> None: async with _make_configured_session(monkeypatch, output_audio_transcription=None) as session: generations: list[llm.GenerationCreatedEvent] = [] session.on("generation_created", generations.append) session._start_new_generation() session._handle_server_content( types.LiveServerContent( model_turn=types.Content(parts=[types.Part(text="Hello there.")]), turn_complete=True, ) ) assert len(generations) == 1 assert await _drain_generation(generations[0]) == ("Hello there.", 0, []) async def test_output_streams_close_on_generation_complete( monkeypatch: pytest.MonkeyPatch, ) -> None: """generation_complete ends the audio/text segment; finalization waits for turn_complete. Gemini delays turn_complete until it estimates client-side playback has finished, so keying the stream close off turn_complete makes AudioSegmentEnd (and the finalized transcript) arrive seconds late (issue #6421). Both streams must close on generation_complete, while the generation stays open until turn_complete for input transcription and metrics. """ async with _make_session(monkeypatch) as session: session._start_new_generation() gen = session._current_generation assert gen is not None session._handle_server_content( _audio_content( output_transcription=types.Transcription(text="hello"), generation_complete=True, ) ) # audio and text were consumed and both segments ended immediately assert bytes(gen.audio_ch.recv_nowait().data) == _PCM_FRAME assert gen.output_text == "hello" assert gen.audio_ch.closed assert gen.text_ch.closed # but the generation is still open for trailing input transcription until turn_complete assert not gen.done assert not gen.message_ch.closed session._handle_server_content(types.LiveServerContent(turn_complete=True)) assert gen.done assert gen.message_ch.closed async def test_late_content_after_generation_complete_is_dropped( monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, ) -> None: """Stray audio/text after generation_complete is dropped (not pushed to a closed stream).""" async with _make_session(monkeypatch) as session: session._start_new_generation() gen = session._current_generation assert gen is not None session._handle_server_content(_audio_content(generation_complete=True)) assert gen.audio_ch.closed and gen.text_ch.closed with caplog.at_level(logging.WARNING): # must not raise ChanClosed, must not append to the transcript, and must warn session._handle_server_content( _audio_content(output_transcription=types.Transcription(text="late")) ) assert gen.audio_ch.closed and gen.text_ch.closed assert gen.output_text == "" assert not gen.done assert any("after generation completed" in r.message for r in caplog.records) async def test_input_transcription_uses_generation_timestamp( monkeypatch: pytest.MonkeyPatch, ) -> None: """Interim and final transcripts stay on the timeline before the reply they prompted.""" async with _make_session(monkeypatch) as session: transcripts: list[llm.InputTranscriptionCompleted] = [] session.on("input_audio_transcription_completed", transcripts.append) session._start_new_generation() gen = session._current_generation assert gen is not None gen.created_timestamp = 1234.5 session._handle_server_content( types.LiveServerContent(input_transcription=types.Transcription(text="hello")) ) session._handle_server_content(types.LiveServerContent(turn_complete=True)) assert [(event.is_final, event.turn_started_at) for event in transcripts] == [ (False, 1234.5), (True, 1234.5), ] async def test_session_close_releases_the_genai_client( monkeypatch: pytest.MonkeyPatch, ) -> None: """aclose() must release the genai http clients. Otherwise they live until the collector runs ``AsyncClient.__del__``, which does ``asyncio.get_running_loop().create_task(self.aclose())`` - creating pending tasks on whatever event loop is running at that moment. """ closed = False async with _make_session(monkeypatch) as session: real_aclose = session._client.aio.aclose async def _spy() -> None: nonlocal closed closed = True await real_aclose() monkeypatch.setattr(session._client.aio, "aclose", _spy) assert closed def _tool_call(call_id: str | None = "fc_1", name: str = "lookup") -> types.LiveServerToolCall: return types.LiveServerToolCall( function_calls=[types.FunctionCall(id=call_id, name=name, args={})] ) def _tool_output( call_id: str = "fc_1", name: str = "lookup", *, reply_required: bool = True ) -> llm.FunctionCallOutput: return llm.FunctionCallOutput( call_id=call_id, name=name, output="42", is_error=False, reply_required=reply_required, ) @asynccontextmanager async def _make_connected_session( monkeypatch: pytest.MonkeyPatch, *, non_blocking_tools: bool = False ) -> AsyncIterator[RealtimeSession]: """A session that believes it is connected, so update_chat_ctx actually emits. The placeholder is never called: the send task is not running, so client events just queue up in `_msg_ch` for the test to inspect. `_make_session` closes that channel to stop the connect loop, so it is replaced with an open one first. """ async with _make_session(monkeypatch) as session: if non_blocking_tools: session._opts.tool_behavior = types.Behavior.NON_BLOCKING session._msg_ch = utils.aio.Chan[ClientEvents]() session._active_session = object() # type: ignore[assignment] try: yield session finally: # the placeholder has no close(), drop it before aclose() reaches for one session._active_session = None async def _drain_sent(session: RealtimeSession) -> list[object]: sent: list[object] = [] while not session._msg_ch.empty(): sent.append(session._msg_ch.recv_nowait()) return sent @pytest.mark.parametrize( "reply_required, scheduling", [(False, types.FunctionResponseScheduling.SILENT), (True, None)], ) async def test_tool_response_scheduling_follows_the_output( monkeypatch: pytest.MonkeyPatch, reply_required: bool, scheduling: types.FunctionResponseScheduling | None, ) -> None: """A result owed by an interrupted turn goes out SILENT; a normal one keeps the default. Gemini blocks the turn until every call is answered and offers no cancel, so dropping the result strands the session (issue #6569). SILENT records it without prompting speech. """ async with _make_connected_session(monkeypatch, non_blocking_tools=True) as session: session._start_new_generation() session._handle_tool_calls(_tool_call()) await _drain_sent(session) chat_ctx = session.chat_ctx.copy() chat_ctx.items.append(_tool_output(reply_required=reply_required)) await session.update_chat_ctx(chat_ctx) sent = await _drain_sent(session) responses = [m for m in sent if isinstance(m, types.LiveClientToolResponse)] assert len(responses) == 1, f"expected the tool response to be sent, got {sent}" assert responses[0].function_responses is not None assert responses[0].function_responses[0].id == "fc_1" assert responses[0].function_responses[0].scheduling == scheduling async def test_blocking_tools_send_the_response_and_warn_it_cannot_be_silent( monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, ) -> None: """Gemini ignores scheduling on BLOCKING declarations, so the reply cannot be prevented. It is sent anyway, since unblocking the turn matters more, and every one is reported. """ async with _make_connected_session(monkeypatch) as session: session._start_new_generation() session._handle_tool_calls( types.LiveServerToolCall( function_calls=[ types.FunctionCall(id="fc_1", name="lookup", args={}), types.FunctionCall(id="fc_2", name="search", args={}), ] ) ) await _drain_sent(session) with caplog.at_level(logging.WARNING): for call_id, name in (("fc_1", "lookup"), ("fc_2", "search")): chat_ctx = session.chat_ctx.copy() chat_ctx.items.append(_tool_output(call_id, name, reply_required=False)) await session.update_chat_ctx(chat_ctx) responses = [ m for m in await _drain_sent(session) if isinstance(m, types.LiveClientToolResponse) ] assert len(responses) == 2 assert all(r.function_responses[0].scheduling is None for r in responses) # type: ignore[index] warnings = [r for r in caplog.records if "wants no reply" in r.message] assert len(warnings) == 2, "every update reports what it could not keep quiet" assert [r.functions for r in warnings] == [["lookup"], ["search"]] # type: ignore[attr-defined] @pytest.mark.parametrize("vertexai", [False, True]) def test_function_response_scheduling_only_for_gemini_api(vertexai: bool) -> None: """Vertex AI rejects `scheduling`, so it is set only for the Gemini API.""" res = create_function_response( _tool_output(), vertexai=vertexai, tool_response_scheduling=types.FunctionResponseScheduling.SILENT, ) if vertexai: assert res.scheduling is None else: assert res.scheduling == types.FunctionResponseScheduling.SILENT @pytest.mark.parametrize("vertexai", [False, True]) async def test_tool_response_carries_the_call_id( monkeypatch: pytest.MonkeyPatch, vertexai: bool ) -> None: """The response names the call it answers on both APIs. gemini-3.8-live on Vertex AI drops a response to a BLOCKING call that has no id and never replies, so the turn stalls after the first tool call. """ async with _make_connected_session(monkeypatch) as session: session._opts.vertexai = vertexai session._opts.tool_behavior = types.Behavior.BLOCKING session._start_new_generation() session._handle_tool_calls(_tool_call()) await _drain_sent(session) chat_ctx = session.chat_ctx.copy() chat_ctx.items.append(_tool_output()) await session.update_chat_ctx(chat_ctx) responses = [ m for m in await _drain_sent(session) if isinstance(m, types.LiveClientToolResponse) ] assert len(responses) == 1 assert responses[0].function_responses is not None assert responses[0].function_responses[0].id == "fc_1" @pytest.mark.parametrize("vertexai", [False, True]) async def test_tool_response_omits_a_locally_made_call_id( monkeypatch: pytest.MonkeyPatch, vertexai: bool ) -> None: """A call the server sent without an id is answered without one. The id we make up to track the call locally names nothing on the server. """ async with _make_connected_session(monkeypatch) as session: session._opts.vertexai = vertexai session._opts.tool_behavior = types.Behavior.BLOCKING session._start_new_generation() session._handle_tool_calls(_tool_call(call_id=None)) await _drain_sent(session) assert len(session._synthetic_call_ids) == 1 (call_id,) = session._synthetic_call_ids chat_ctx = session.chat_ctx.copy() chat_ctx.items.append(_tool_output(call_id=call_id)) await session.update_chat_ctx(chat_ctx) responses = [ m for m in await _drain_sent(session) if isinstance(m, types.LiveClientToolResponse) ] assert len(responses) == 1 assert responses[0].function_responses is not None assert responses[0].function_responses[0].id is None # a resumption replays the response from the chat context; it still carries no id await session._sync_chat_ctx(chat_ctx, known=llm.ChatContext.empty()) replayed = [ m for m in await _drain_sent(session) if isinstance(m, types.LiveClientToolResponse) ] assert len(replayed) == 1 assert replayed[0].function_responses is not None assert replayed[0].function_responses[0].id is None class _FakeVoiceSession: """Stands in for the genai live session and records what the plugin sends.""" def __init__(self) -> None: self.sent: list[tuple[str, object]] = [] self._closed = asyncio.Event() async def send_client_content(self, *, turns: object, turn_complete: bool) -> None: self.sent.append(("content", turns)) async def send_tool_response(self, *, function_responses: object) -> None: self.sent.append(("tool_response", function_responses)) async def send_realtime_input(self, **kwargs: object) -> None: pass async def receive(self) -> AsyncIterator[types.LiveServerMessage]: await self._closed.wait() return yield # pragma: no cover - makes this an async generator async def close(self) -> None: self._closed.set() @asynccontextmanager async def _connected_session( monkeypatch: pytest.MonkeyPatch, *, handle: str | None, known: llm.ChatContext | None = None, sent_after_handle: llm.ChatContext | None = None, pending: llm.ChatContext | None = None, caller_handle: bool = False, ) -> AsyncIterator[tuple[RealtimeSession, _FakeVoiceSession]]: """Connect once onto a fake socket. `known` is the state the handle stands for, `sent_after_handle` what the previous socket synced after the handle arrived, `pending` the update that arrives before the connect loop runs. `caller_handle` passes the handle through `RealtimeModel` instead, so its baseline is unknown. """ from google.genai.live import AsyncLive fake = _FakeVoiceSession() @asynccontextmanager async def _connect(self: AsyncLive, **kwargs: object) -> AsyncIterator[_FakeVoiceSession]: yield fake monkeypatch.setenv("GOOGLE_API_KEY", "fake-key") monkeypatch.setattr(AsyncLive, "connect", _connect) if caller_handle: session = RealtimeModel( session_resumption=types.SessionResumptionConfig(handle=handle) ).session() else: session = RealtimeModel().session() session._session_resumption_handle = handle if known is not None: session._resumption_chat_ctx = known session._chat_ctx = sent_after_handle if sent_after_handle is not None else known if pending is not None: await session.update_chat_ctx(pending) try: while session._active_session is None: await asyncio.sleep(0.01) # let the send task drain the queued events onto the fake socket await asyncio.sleep(0.05) yield session, fake finally: await session.aclose() def _texts(sent: list[tuple[str, object]]) -> list[list[str]]: return [ [p.text for c in turns for p in c.parts] # type: ignore[attr-defined] for kind, turns in sent if kind == "content" ] async def test_fresh_session_replays_chat_ctx(monkeypatch: pytest.MonkeyPatch) -> None: ctx = llm.ChatContext.empty() ctx.add_message(role="user", content="hello") ctx.add_message(role="assistant", content="hi") async with _connected_session(monkeypatch, handle=None, pending=ctx) as (session, fake): assert _texts(fake.sent) == [["hello", "hi"]] assert session._pending_chat_ctx is None async def test_resumed_session_skips_chat_ctx_replay(monkeypatch: pytest.MonkeyPatch) -> None: """The server restored the conversation from the handle; replaying it would duplicate it.""" ctx = llm.ChatContext.empty() ctx.add_message(role="user", content="hello") async with _connected_session(monkeypatch, handle="resume-1", known=ctx, pending=ctx) as ( session, fake, ): assert fake.sent == [] assert session._pending_chat_ctx is None assert [m.text_content for m in session.chat_ctx.messages()] == ["hello"] async def test_resumed_session_sends_only_the_disconnected_update( monkeypatch: pytest.MonkeyPatch, ) -> None: """Items appended during the restart are new to the resumed session; the rest is not.""" known = llm.ChatContext.empty() known.add_message(role="user", content="hello") updated = known.copy() updated.add_message(role="user", content="one more thing") async with _connected_session(monkeypatch, handle="resume-1", known=known, pending=updated) as ( session, fake, ): assert _texts(fake.sent) == [["one more thing"]] assert [m.text_content for m in session.chat_ctx.messages()] == [ "hello", "one more thing", ] async def test_resumed_session_delivers_the_tool_result_from_the_restart( monkeypatch: pytest.MonkeyPatch, ) -> None: """The resumed session still holds the call open, so the result produced meanwhile answers it.""" known = llm.ChatContext.empty() known.add_message(role="user", content="book it") known.items.append(llm.FunctionCall(call_id="call-1", name="book", arguments="{}")) updated = known.copy() updated.items.append( llm.FunctionCallOutput(call_id="call-1", name="book", output="done", is_error=False) ) async with _connected_session(monkeypatch, handle="resume-1", known=known, pending=updated) as ( _, fake, ): assert [kind for kind, _ in fake.sent] == ["tool_response"] responses = fake.sent[0][1] assert [r.id for r in responses] == ["call-1"] # type: ignore[attr-defined] async def test_resumed_session_resends_what_the_handle_missed( monkeypatch: pytest.MonkeyPatch, ) -> None: """A message synced after the last handle is not in the server snapshot; resend it.""" known = llm.ChatContext.empty() known.add_message(role="user", content="hello") later = known.copy() later.add_message(role="user", content="sent before the socket dropped") async with _connected_session( monkeypatch, handle="resume-1", known=known, sent_after_handle=later ) as (session, fake): assert _texts(fake.sent) == [["sent before the socket dropped"]] assert session._pending_chat_ctx is None async def test_caller_provided_handle_adopts_the_history_without_replay( monkeypatch: pytest.MonkeyPatch, ) -> None: """With a handle from the constructor the baseline is unknown; the history is the server's.""" history = llm.ChatContext.empty() history.add_message(role="user", content="from the previous process") history.add_message(role="assistant", content="noted") async with _connected_session( monkeypatch, handle="resume-1", pending=history, caller_handle=True ) as (session, fake): assert fake.sent == [] assert [m.text_content for m in session.chat_ctx.messages()] == [ "from the previous process", "noted", ] @llm.function_tool async def _restart_tool() -> str: """Any new tool makes update_tools restart the socket.""" return "" async def test_handle_does_not_claim_a_queued_but_unsent_update( monkeypatch: pytest.MonkeyPatch, ) -> None: """A handle that lands while a diff is still queued must not cover it; a restart re-sends it.""" from google.genai.live import AsyncLive known = llm.ChatContext.empty() known.add_message(role="user", content="hello") updated = known.copy() updated.add_message(role="user", content="queued behind the handle") class _GatedSession(_FakeVoiceSession): """First socket: the connect-time handle arrives while the send is still blocked.""" def __init__(self) -> None: super().__init__() self.gate = asyncio.Event() async def send_client_content(self, *, turns: object, turn_complete: bool) -> None: await self.gate.wait() await super().send_client_content(turns=turns, turn_complete=turn_complete) async def receive(self) -> AsyncIterator[types.LiveServerMessage]: yield types.LiveServerMessage( session_resumption_update=types.LiveServerSessionResumptionUpdate( new_handle="resume-2", resumable=True ) ) await self._closed.wait() sockets: list[_FakeVoiceSession] = [_GatedSession(), _FakeVoiceSession()] opened: list[_FakeVoiceSession] = [] @asynccontextmanager async def _connect(self: AsyncLive, **kwargs: object) -> AsyncIterator[_FakeVoiceSession]: fake = sockets[len(opened)] opened.append(fake) yield fake monkeypatch.setenv("GOOGLE_API_KEY", "fake-key") monkeypatch.setattr(AsyncLive, "connect", _connect) session = RealtimeModel().session() session._session_resumption_handle = "resume-1" session._resumption_chat_ctx = known session._chat_ctx = known await session.update_chat_ctx(updated) try: while session._session_resumption_handle != "resume-2": await asyncio.sleep(0.01) # the diff is queued but its send is blocked, so the new handle must not cover it assert [m.text_content for m in session._resumption_chat_ctx.messages()] == ["hello"] # restart before the send completes: the channel drain drops the queued diff await session.update_tools([_restart_tool]) while len(opened) < 2: await asyncio.sleep(0.01) await asyncio.sleep(0.05) assert _texts(opened[1].sent) == [["queued behind the handle"]] finally: await session.aclose() async def test_failed_send_with_a_queued_update_replays_each_item_once( monkeypatch: pytest.MonkeyPatch, ) -> None: """An error restart drops the queue; the resume diff must be the only thing that re-sends.""" from google.genai.live import AsyncLive known = llm.ChatContext.empty() known.add_message(role="user", content="hello") first = known.copy() first.add_message(role="user", content="first update") class _FailingSession(_FakeVoiceSession): """Blocks the first send until released, then fails it.""" def __init__(self) -> None: super().__init__() self.blocked = asyncio.Event() self.release = asyncio.Event() async def send_client_content(self, *, turns: object, turn_complete: bool) -> None: self.blocked.set() await self.release.wait() raise RuntimeError("socket gone") sockets: list[_FakeVoiceSession] = [_FailingSession(), _FakeVoiceSession()] opened: list[_FakeVoiceSession] = [] @asynccontextmanager async def _connect(self: AsyncLive, **kwargs: object) -> AsyncIterator[_FakeVoiceSession]: fake = sockets[len(opened)] opened.append(fake) yield fake monkeypatch.setenv("GOOGLE_API_KEY", "fake-key") monkeypatch.setattr(AsyncLive, "connect", _connect) session = RealtimeModel().session() session._session_resumption_handle = "resume-1" session._resumption_chat_ctx = known session._chat_ctx = known await session.update_chat_ctx(first) try: failing = sockets[0] assert isinstance(failing, _FailingSession) await asyncio.wait_for(failing.blocked.wait(), timeout=2) # a second update queues behind the blocked send second = first.copy() second.add_message(role="user", content="second update") await session.update_chat_ctx(second) failing.release.set() while len(opened) < 2: await asyncio.sleep(0.01) await asyncio.sleep(0.05) assert opened[0].sent == [] assert _texts(opened[1].sent) == [["first update", "second update"]] assert session._unsent_item_ids == set() finally: await session.aclose() async def test_context_updates_preserve_order_while_image_preparation_waits( monkeypatch: pytest.MonkeyPatch, ) -> None: entered = asyncio.Event() release = asyncio.Event() preparations: list[list[str]] = [] async def prepare( context: llm.ChatContext, *args: object, **kwargs: object ) -> tuple[list[dict], None]: ids = [item.id for item in context.items] preparations.append(ids) if len(preparations) == 1: entered.set() await release.wait() return [ {"role": "user", "parts": [{"text": message.text_content}]} for message in context.messages() ], None monkeypatch.setattr(llm.ChatContext, "to_provider_format", prepare) async with _make_connected_session(monkeypatch) as session: first = llm.ChatContext.empty() first.add_message(role="user", content="first", id="first") second = first.copy() second.add_message(role="user", content="second", id="second") one = asyncio.create_task(session.update_chat_ctx(first)) await asyncio.wait_for(entered.wait(), 2) two = asyncio.create_task(session.update_chat_ctx(second)) try: await asyncio.sleep(0) assert preparations == [["first"]] release.set() await asyncio.gather(one, two) assert preparations == [["first"], ["second"]] assert [item.id for item in session.chat_ctx.items] == ["first", "second"] finally: release.set() await asyncio.gather(one, two, return_exceptions=True) async def test_session_close_does_not_wait_for_context_image_preparation( monkeypatch: pytest.MonkeyPatch, ) -> None: entered = asyncio.Event() release = asyncio.Event() async def prepare( context: llm.ChatContext, *args: object, **kwargs: object ) -> tuple[list[dict], None]: entered.set() await release.wait() return [], None monkeypatch.setattr(llm.ChatContext, "to_provider_format", prepare) async with _make_connected_session(monkeypatch) as session: session._active_session = _FakeVoiceSession() # type: ignore[assignment] context = llm.ChatContext.empty() context.add_message(role="user", content="image", id="slow-image") update = asyncio.create_task(session.update_chat_ctx(context)) try: await asyncio.wait_for(entered.wait(), 2) await asyncio.wait_for(session.aclose(), 0.5) assert not release.is_set(), "close must not wait for image preparation" release.set() await update assert session._chat_ctx.get_by_id("slow-image") is None finally: release.set() await asyncio.gather(update, return_exceptions=True) async def test_restart_during_initial_image_preparation_is_not_a_connection_failure( monkeypatch: pytest.MonkeyPatch, ) -> None: from google.genai.live import AsyncLive entered = asyncio.Event() release = asyncio.Event() opened: list[_FakeVoiceSession] = [] @asynccontextmanager async def connect(self: AsyncLive, **kwargs: object) -> AsyncIterator[_FakeVoiceSession]: socket = _FakeVoiceSession() opened.append(socket) try: yield socket finally: await socket.close() async def prepare( context: llm.ChatContext, *args: object, **kwargs: object ) -> tuple[list[dict], None]: if len(opened) == 1: entered.set() await release.wait() return [], None monkeypatch.setenv("GOOGLE_API_KEY", "fake-key") monkeypatch.setattr(AsyncLive, "connect", connect) monkeypatch.setattr(llm.ChatContext, "to_provider_format", prepare) session = RealtimeModel().session() errors: list[object] = [] session.on("error", errors.append) try: await asyncio.wait_for(entered.wait(), 2) session.update_options(voice="Kore") release.set() async with asyncio.timeout(2): while len(opened) < 2 or session._active_session is None: await asyncio.sleep(0) assert session._num_retries == 0 assert errors == [] finally: release.set() await session.aclose() @pytest.mark.parametrize("during_preparation", [False, True]) async def test_context_update_during_restart_is_preserved_for_the_next_connection( monkeypatch: pytest.MonkeyPatch, during_preparation: bool, ) -> None: entered = asyncio.Event() release = asyncio.Event() async def prepare( context: llm.ChatContext, *args: object, **kwargs: object ) -> tuple[list[dict], None]: entered.set() await release.wait() return [], None monkeypatch.setattr(llm.ChatContext, "to_provider_format", prepare) async with _make_connected_session(monkeypatch) as session: context = llm.ChatContext.empty() context.add_message(role="user", content="keep this update", id="pending") if during_preparation: update = asyncio.create_task(session.update_chat_ctx(context)) try: await asyncio.wait_for(entered.wait(), 2) session._session_should_close.set() release.set() await update finally: release.set() await asyncio.gather(update, return_exceptions=True) else: session._session_should_close.set() await session.update_chat_ctx(context) assert session.chat_ctx.get_by_id("pending") is not None assert session._pending_chat_ctx is not None async def test_video_dequeue_releases_the_latest_frame_slot_before_session_check( monkeypatch: pytest.MonkeyPatch, ) -> None: from livekit import rtc async with _make_connected_session(monkeypatch) as session: frame = rtc.VideoFrame(1, 1, rtc.VideoBufferType.RGBA, bytes(4)) session.push_video(frame) session._active_session = None await session._send_task(object()) # type: ignore[arg-type] assert session._pending_video is None session.push_video(frame) assert not session._msg_ch.empty(), "a later frame must enter the outgoing queue" async def test_bad_video_frame_does_not_restart_the_session( monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, ) -> None: from livekit import rtc from livekit.agents.utils import media as images encoded = asyncio.Event() sent = asyncio.Event() calls = 0 async def encode(frame: rtc.VideoFrame, options: images.EncodeOptions) -> bytes: nonlocal calls calls += 1 if calls <= 2: encoded.set() raise ValueError("invalid video frame") return b"jpeg" class Socket(_FakeVoiceSession): async def send_realtime_input(self, **kwargs: object) -> None: sent.set() monkeypatch.setattr(images, "encode", encode) async with _make_connected_session(monkeypatch) as session: socket = Socket() session._active_session = socket # type: ignore[assignment] frame = rtc.VideoFrame(1, 1, rtc.VideoBufferType.RGBA, bytes(4)) session.push_video(frame) pump = asyncio.create_task(session._send_task(socket)) # type: ignore[arg-type] try: for _ in range(2): await asyncio.wait_for(encoded.wait(), 2) encoded.clear() session.push_video(frame) await asyncio.wait_for(sent.wait(), 1) assert not session._session_should_close.is_set() assert sum("invalid video frame" in record.message for record in caplog.records) == 1 finally: await utils.aio.cancel_and_wait(pump)