Files
livekit-agents/python/tests/test_plugin_google_llm.py
T

600 lines
23 KiB
Python

from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from google.genai import types
from livekit.agents import APIConnectionError
from livekit.agents.llm import ChatContext, function_tool
from livekit.agents.types import APIConnectOptions
from livekit.plugins.google.llm import LLM, LLMStream
from livekit.plugins.google.tools import GoogleSearch
pytestmark = pytest.mark.plugin("google")
@pytest.fixture
def llm_stream():
mock_llm = MagicMock()
mock_llm._thought_signatures = {}
with patch.object(LLMStream, "__init__", lambda self, *a, **kw: None):
stream = LLMStream.__new__(LLMStream)
stream._llm = mock_llm
stream._thought_signatures = mock_llm._thought_signatures
stream._model = "gemini-2.0-flash"
return stream
class TestParsePartFunctionCall:
def test_function_call_with_text_returns_none_content(self, llm_stream: LLMStream):
part = types.Part(
function_call=types.FunctionCall(name="get_weather", args={"city": "Paris"}),
text="get_weather",
)
chunk = llm_stream._parse_part("test-id", part)
assert chunk is not None
assert chunk.delta.content is None
assert chunk.delta.tool_calls is not None
assert len(chunk.delta.tool_calls) == 1
tool_call = chunk.delta.tool_calls[0]
assert tool_call.name == "get_weather"
assert '"city": "Paris"' in tool_call.arguments
def test_empty_text_part_returns_none(self, llm_stream: LLMStream):
part = types.Part(text="")
chunk = llm_stream._parse_part("test-id", part)
assert chunk is None
def test_thought_summary_part_returns_none(self, llm_stream: LLMStream):
# include_thoughts streams reasoning back as parts flagged thought=True; these
# must be dropped so TTS never speaks the model's internal reasoning.
part = types.Part(
text="**Calculating Refill Dates** I'm processing the timeline", thought=True
)
chunk = llm_stream._parse_part("test-id", part)
assert chunk is None
class TestCachedContentRequestSuppression:
"""Gemini's API rejects ``generateContent`` requests that pass
``cached_content`` together with ``system_instruction``, ``tools``,
or ``tool_config`` — those fields belong inside the CachedContent
resource. The plugin therefore strips them off the outgoing request
whenever a cache is attached. These tests run the LLMStream against
a stubbed ``generate_content_stream`` and assert on the
``GenerateContentConfig`` it received.
"""
@staticmethod
async def _single_response_async_iter():
"""Emit one minimal-but-valid GenerateContentResponse so the
retry layer in livekit.agents.LLM doesn't treat the stream as
empty and re-issue the request three more times."""
yield types.GenerateContentResponse(
candidates=[
types.Candidate(
content=types.Content(
role="model",
parts=[types.Part(text="ok")],
),
finish_reason=types.FinishReason.STOP,
)
],
)
@classmethod
def _patched_stream_capture(cls) -> tuple[AsyncMock, dict]:
captured: dict = {}
async def fake_stream(**kwargs):
captured["model"] = kwargs.get("model")
captured["contents"] = kwargs.get("contents")
captured["config"] = kwargs.get("config")
return cls._single_response_async_iter()
return AsyncMock(side_effect=fake_stream), captured
@pytest.mark.asyncio
async def test_request_omits_system_instruction_when_cached_content_set(self) -> None:
"""With a cache attached, the outgoing request must carry
``system_instruction=None`` — the system prompt lives in the
cache resource and re-sending it would make Gemini return 400."""
llm = LLM(
model="gemini-2.5-flash",
api_key="test",
cached_content="cachedContents/abc123",
)
chat_ctx = ChatContext.empty()
chat_ctx.add_message(role="system", content="system prompt that lives in cache")
chat_ctx.add_message(role="user", content="hi")
fake, captured = self._patched_stream_capture()
with patch.object(llm._client.aio.models, "generate_content_stream", fake):
stream = llm.chat(chat_ctx=chat_ctx)
try:
async for _ in stream:
pass
finally:
await stream.aclose()
config = captured["config"]
assert config.system_instruction is None
assert config.cached_content == "cachedContents/abc123"
@pytest.mark.asyncio
async def test_request_omits_tools_when_cached_content_set(self) -> None:
"""With a cache attached, the outgoing request must NOT include
``tools`` even if the LLMStream was constructed with function
tools — the tool schemas belong inside the cache resource."""
@function_tool
async def example_tool(query: str) -> str:
"""Look something up."""
return query
llm = LLM(
model="gemini-2.5-flash",
api_key="test",
cached_content="cachedContents/abc123",
)
fake, captured = self._patched_stream_capture()
with patch.object(llm._client.aio.models, "generate_content_stream", fake):
stream = llm.chat(chat_ctx=ChatContext.empty(), tools=[example_tool])
try:
async for _ in stream:
pass
finally:
await stream.aclose()
config = captured["config"]
assert config.tools is None
assert config.tool_config is None
assert config.cached_content == "cachedContents/abc123"
@pytest.mark.asyncio
async def test_request_includes_system_instruction_and_tools_when_no_cache(self) -> None:
"""Backward compatibility: without ``cached_content``, the
request still carries ``system_instruction`` and ``tools`` as
before. Suppression is gated strictly on the cache being set."""
@function_tool
async def example_tool(query: str) -> str:
"""Look something up."""
return query
llm = LLM(model="gemini-2.5-flash", api_key="test")
chat_ctx = ChatContext.empty()
chat_ctx.add_message(role="system", content="system prompt sent on every request")
chat_ctx.add_message(role="user", content="hi")
fake, captured = self._patched_stream_capture()
with patch.object(llm._client.aio.models, "generate_content_stream", fake):
stream = llm.chat(chat_ctx=chat_ctx, tools=[example_tool])
try:
async for _ in stream:
pass
finally:
await stream.aclose()
config = captured["config"]
assert config.system_instruction is not None
assert config.tools is not None and len(config.tools) >= 1
@pytest.mark.asyncio
async def test_request_preserves_headers_and_disables_sdk_retry_timeout(self) -> None:
caller_http_options = types.HttpOptions(headers={"X-Vertex-Test": "1"})
llm = LLM(
model="gemini-2.5-flash",
api_key="test",
http_options=caller_http_options,
)
fake, captured = self._patched_stream_capture()
with patch.object(llm._client.aio.models, "generate_content_stream", fake):
stream = llm.chat(
chat_ctx=ChatContext.empty(),
conn_options=APIConnectOptions(),
)
try:
async for _ in stream:
pass
finally:
await stream.aclose()
config = captured["config"]
assert config.http_options.timeout == 0
assert config.http_options.retry_options.attempts == 1
assert config.http_options.headers["X-Vertex-Test"] == "1"
assert "livekit-agents/" in config.http_options.headers["x-goog-api-client"]
assert caller_http_options.timeout is None
assert caller_http_options.headers == {"X-Vertex-Test": "1"}
class TestMixedToolsRequestConstruction:
"""Combining built-in (provider) tools with function tools is only supported on the
Gemini 3 Developer API (not Vertex). These tests drive ``chat()`` against a stubbed
``generate_content_stream`` and assert on the resulting request config."""
@staticmethod
async def _single_response_async_iter():
yield types.GenerateContentResponse(
candidates=[
types.Candidate(
content=types.Content(role="model", parts=[types.Part(text="ok")]),
finish_reason=types.FinishReason.STOP,
)
],
)
@classmethod
async def _capture_config(cls, llm_: LLM, **chat_kwargs):
captured: dict = {}
async def fake_stream(**kwargs):
captured["config"] = kwargs.get("config")
return cls._single_response_async_iter()
fake = AsyncMock(side_effect=fake_stream)
with patch.object(llm_._client.aio.models, "generate_content_stream", fake):
stream = llm_.chat(chat_ctx=ChatContext.empty(), **chat_kwargs)
try:
async for _ in stream:
pass
finally:
await stream.aclose()
return captured["config"]
@staticmethod
def _weather_tool():
@function_tool
async def get_weather(city: str) -> str:
"""Look up the weather."""
return city
return get_weather
@staticmethod
def _has_google_search(config) -> bool:
return bool(config.tools) and any(getattr(t, "google_search", None) for t in config.tools)
@staticmethod
def _has_function_declarations(config) -> bool:
return bool(config.tools) and any(t.function_declarations for t in config.tools)
@staticmethod
def _server_side_enabled(config) -> bool:
return bool(config.tool_config and config.tool_config.include_server_side_tool_invocations)
@pytest.mark.asyncio
async def test_dev_api_enables_server_side_invocations(self) -> None:
"""Gemini 3 Developer API: both tool types are sent and the circulation flag is set."""
llm_ = LLM(model="gemini-3-flash-preview", api_key="test")
config = await self._capture_config(llm_, tools=[self._weather_tool(), GoogleSearch()])
assert self._server_side_enabled(config)
assert self._has_function_declarations(config)
assert self._has_google_search(config)
@pytest.mark.asyncio
async def test_auto_mode_kept_for_mixed_tools(self) -> None:
"""``tool_choice='auto'`` is passed through as AUTO alongside the circulation flag.
Verified live against the Gemini 3 Developer API: AUTO + built-in tools is accepted,
so no VALIDATED upgrade is applied."""
llm_ = LLM(model="gemini-3-flash-preview", api_key="test")
config = await self._capture_config(
llm_, tools=[self._weather_tool(), GoogleSearch()], tool_choice="auto"
)
assert self._server_side_enabled(config)
assert (
config.tool_config.function_calling_config.mode == types.FunctionCallingConfigMode.AUTO
)
@pytest.mark.asyncio
async def test_non_gemini_3_drops_provider_tool(self) -> None:
"""Below Gemini 3 the provider tool is dropped and no flag is set."""
llm_ = LLM(model="gemini-2.5-flash", api_key="test")
config = await self._capture_config(llm_, tools=[self._weather_tool(), GoogleSearch()])
assert not self._server_side_enabled(config)
assert self._has_function_declarations(config)
assert not self._has_google_search(config)
@pytest.mark.asyncio
async def test_vertex_gemini_3_drops_provider_tool(self) -> None:
"""Vertex AI does not support mixing, even on Gemini 3: provider tool is dropped."""
from google.auth.credentials import AnonymousCredentials
llm_ = LLM(
model="gemini-3-flash-preview",
vertexai=True,
project="test-project",
location="us-central1",
credentials=AnonymousCredentials(),
)
assert llm_._client.vertexai is True
config = await self._capture_config(llm_, tools=[self._weather_tool(), GoogleSearch()])
assert not self._server_side_enabled(config)
assert self._has_function_declarations(config)
assert not self._has_google_search(config)
@pytest.mark.asyncio
async def test_provider_only_does_not_set_flag(self) -> None:
"""The circulation flag requires BOTH function and provider tools; a provider tool
alone is sent without it."""
llm_ = LLM(model="gemini-3-flash-preview", api_key="test")
config = await self._capture_config(llm_, tools=[GoogleSearch()])
assert not self._server_side_enabled(config)
assert self._has_google_search(config)
@pytest.mark.asyncio
async def test_cached_content_via_extra_kwargs_suppresses_tools(self) -> None:
"""cached_content passed through ``extra_kwargs`` (not just the constructor) must
still skip building tools/tool_config, so the request isn't rejected for combining
them with a cache."""
llm_ = LLM(model="gemini-3-flash-preview", api_key="test")
config = await self._capture_config(
llm_,
tools=[self._weather_tool(), GoogleSearch()],
extra_kwargs={"cached_content": "cachedContents/abc123"},
)
assert config.cached_content == "cachedContents/abc123"
assert config.tools is None
assert config.tool_config is None
@pytest.mark.asyncio
async def test_cached_content_strips_raw_tools_from_extra_kwargs(self) -> None:
"""Raw ``tools``/``tool_config`` placed directly in ``extra_kwargs`` must also be
stripped when a cache is active — otherwise Gemini rejects the request for
combining them with cached_content."""
llm_ = LLM(
model="gemini-3-flash-preview", api_key="test", cached_content="cachedContents/abc"
)
config = await self._capture_config(
llm_,
extra_kwargs={
"tools": [types.Tool(google_search=types.GoogleSearch())],
"tool_config": types.ToolConfig(
function_calling_config=types.FunctionCallingConfig(
mode=types.FunctionCallingConfigMode.AUTO
)
),
},
)
assert config.cached_content == "cachedContents/abc"
assert config.tools is None
assert config.tool_config is None
class TestThinkingConfigRequestConstruction:
"""Gemini 3 and Gemma 4 configure thinking with ``thinking_level``, every other model with
``thinking_budget``. These tests drive ``chat()`` against a stubbed
``generate_content_stream`` and assert on the ``ThinkingConfig`` it received."""
@staticmethod
async def _single_response_async_iter():
yield types.GenerateContentResponse(
candidates=[
types.Candidate(
content=types.Content(role="model", parts=[types.Part(text="ok")]),
finish_reason=types.FinishReason.STOP,
)
],
)
@classmethod
async def _capture_thinking_config(
cls, model: str, thinking_config: types.ThinkingConfigOrDict
) -> types.ThinkingConfig | None:
captured: dict = {}
async def fake_stream(**kwargs):
captured["config"] = kwargs.get("config")
return cls._single_response_async_iter()
llm_ = LLM(model=model, api_key="test", thinking_config=thinking_config)
fake = AsyncMock(side_effect=fake_stream)
with patch.object(llm_._client.aio.models, "generate_content_stream", fake):
stream = llm_.chat(chat_ctx=ChatContext.empty())
try:
async for _ in stream:
pass
finally:
await stream.aclose()
return captured["config"].thinking_config
@pytest.mark.asyncio
@pytest.mark.parametrize("model", ["gemma-4-31b-it", "models/gemma-4-31b-it"])
@pytest.mark.parametrize("level", [types.ThinkingLevel.MINIMAL, types.ThinkingLevel.HIGH])
async def test_gemma_4_level_reaches_the_request(
self, model: str, level: types.ThinkingLevel
) -> None:
"""Gemma 4 takes the level it documents, under a bare or a qualified model name."""
thinking_config = await self._capture_thinking_config(
model, types.ThinkingConfig(thinking_level=level)
)
assert thinking_config is not None
assert thinking_config.thinking_level == level
@pytest.mark.parametrize("level", [types.ThinkingLevel.LOW, types.ThinkingLevel.MEDIUM])
def test_gemma_4_rejects_undocumented_levels(self, level: types.ThinkingLevel) -> None:
"""Gemma 4 answers 'low' and 'medium' with 400 INVALID_ARGUMENT, so the session fails at
construction instead of on the first turn."""
with pytest.raises(ValueError, match="only supports thinking_level"):
LLM(
model="gemma-4-31b-it",
api_key="test",
thinking_config=types.ThinkingConfig(thinking_level=level),
)
@pytest.mark.asyncio
@pytest.mark.parametrize("model", ["gemini-3-pro-preview", "gemma-4-31b-it"])
async def test_include_thoughts_survives_the_level_branch(self, model: str) -> None:
thinking_config = await self._capture_thinking_config(
model, {"thinking_level": "high", "include_thoughts": True}
)
assert thinking_config is not None
assert thinking_config.thinking_level == types.ThinkingLevel.HIGH
assert thinking_config.include_thoughts is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model,expected_level",
[
("gemini-3-flash-preview", types.ThinkingLevel.MINIMAL),
("models/gemini-3-flash-preview", types.ThinkingLevel.MINIMAL),
("gemini-3-pro-preview", types.ThinkingLevel.LOW),
# no default is invented for a model whose accepted levels are unknown
("gemma-4-31b-it", None),
],
)
async def test_default_level_only_for_gemini_3(
self, model: str, expected_level: types.ThinkingLevel | None
) -> None:
thinking_config = await self._capture_thinking_config(
model, types.ThinkingConfig(include_thoughts=False)
)
assert thinking_config is not None
assert thinking_config.thinking_level == expected_level
assert thinking_config.include_thoughts is False
@pytest.mark.asyncio
@pytest.mark.parametrize("model", ["gemini-3-pro-preview", "gemma-4-31b-it"])
async def test_budget_alone_is_dropped_on_level_models(self, model: str) -> None:
"""A budget on a level model is ignored with a warning, on Gemini 3 and Gemma 4 alike."""
thinking_config = await self._capture_thinking_config(
model, types.ThinkingConfig(thinking_budget=0)
)
assert thinking_config is not None
assert thinking_config.thinking_budget is None
@pytest.mark.asyncio
async def test_budget_still_reaches_gemini_2_5(self) -> None:
thinking_config = await self._capture_thinking_config(
"gemini-2.5-flash", types.ThinkingConfig(thinking_budget=1024)
)
assert thinking_config is not None
assert thinking_config.thinking_budget == 1024
assert thinking_config.thinking_level is None
@pytest.mark.asyncio
@pytest.mark.parametrize(
("schema", "expected_type"),
[
({"type": "object", "properties": {"answer": {"type": "string"}}}, types.Type.OBJECT),
(
types.Schema(
type=types.Type.OBJECT, properties={"answer": types.Schema(type=types.Type.STRING)}
),
types.Type.OBJECT,
),
(list[str], types.Type.ARRAY),
],
)
async def test_declared_response_schema_forms_reach_the_provider(
schema: object, expected_type: types.Type
) -> None:
provider = LLM(api_key="test")
fake, captured = TestCachedContentRequestSuppression._patched_stream_capture()
try:
with patch.object(provider._client.aio.models, "generate_content_stream", fake):
async with provider.chat(
chat_ctx=ChatContext.empty(), response_format=schema
) as stream:
_ = [chunk async for chunk in stream]
config = captured["config"]
assert config.response_mime_type == "application/json"
resolved = types.Schema.model_validate(config.response_schema)
assert resolved.type == expected_type
finally:
await provider.aclose()
def test_function_call_missing_name_is_a_non_retryable_provider_error(
llm_stream: LLMStream,
) -> None:
part = types.Part(function_call=types.FunctionCall(args={}))
with pytest.raises(APIConnectionError) as failure:
llm_stream._parse_part("response", part)
assert not failure.value.retryable
@pytest.mark.asyncio
async def test_no_system_messages_omit_the_system_instruction() -> None:
provider = LLM(api_key="test")
fake, captured = TestCachedContentRequestSuppression._patched_stream_capture()
context = ChatContext.empty()
context.add_message(role="user", content="hello")
try:
with patch.object(provider._client.aio.models, "generate_content_stream", fake):
async with provider.chat(chat_ctx=context) as stream:
_ = [chunk async for chunk in stream]
config = captured["config"]
assert config.system_instruction is None
assert "systemInstruction" not in config.model_dump(by_alias=True, exclude_none=True)
finally:
await provider.aclose()
async def test_usage_counts_tool_prompts_in_the_input_and_thoughts_in_the_output() -> None:
# Gemini reports built-in tool prompts outside prompt_token_count and thinking outside
# candidates_token_count.
async def responses():
yield types.GenerateContentResponse(
candidates=[
types.Candidate(
content=types.Content(role="model", parts=[types.Part(text="ok")]),
finish_reason=types.FinishReason.STOP,
)
],
usage_metadata=types.GenerateContentResponseUsageMetadata(
prompt_token_count=100,
tool_use_prompt_token_count=40,
cached_content_token_count=60,
candidates_token_count=20,
thoughts_token_count=10,
),
)
provider = LLM(api_key="test")
context = ChatContext.empty()
context.add_message(role="user", content="hello")
try:
with patch.object(
provider._client.aio.models,
"generate_content_stream",
AsyncMock(return_value=responses()),
):
async with provider.chat(chat_ctx=context) as stream:
response = await stream.collect()
finally:
await provider.aclose()
assert response.usage is not None
assert response.usage.input_tokens == 140
assert response.usage.output_tokens == 30
assert response.usage.output_reasoning_tokens == 10
assert response.usage.cache_read_tokens == 60