Skip to content
Open
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
20 changes: 11 additions & 9 deletions src/google/adk/evaluation/evaluation_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ async def _get_or_create_eval_session(
# Keyword-argument names accepted by `Runner`, used when building the eval
# Runner kwargs so the strings are not duplicated at each call site.
_APP_NAME_KEY = "app_name"
_AGENT_KEY = "agent"
_PLUGINS_KEY = "plugins"
_APP_KEY = "app"


Expand All @@ -194,15 +192,19 @@ def _build_eval_runner_kwargs(
application-wide configuration participate in the eval run. The copy leaves
the caller's App instance untouched, and `root_agent` is overridden so the
Runner targets the agent the caller asked to evaluate, which may be a
sub-agent. When `app` is None, the Runner is built from the bare
`root_agent` with only the internal eval plugins.
sub-agent. When `app` is None, `root_agent` is wrapped in a new App that
holds only the internal eval plugins, because passing `plugins=` to Runner
is deprecated. Like Runner's own wrapping of a bare agent, this uses
`model_construct` so that eval app names which are not valid App names
keep working.
"""
if app is None:
return {
_APP_NAME_KEY: app_name,
_AGENT_KEY: root_agent,
_PLUGINS_KEY: internal_eval_plugins,
}
runner_app = App.model_construct(
name=app_name,
root_agent=root_agent,
plugins=list(internal_eval_plugins),
)
return {_APP_KEY: runner_app, _APP_NAME_KEY: app_name}

runner_app = app.model_copy(
update={
Expand Down
47 changes: 42 additions & 5 deletions tests/unittests/evaluation/test_evaluation_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import asyncio
import builtins
from typing import AsyncGenerator
import warnings

from google.adk.agents.base_agent import BaseAgent
from google.adk.agents.llm_agent import LlmAgent
Expand Down Expand Up @@ -48,6 +49,7 @@
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.adk.plugins.base_plugin import BasePlugin
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.adk.sessions.session import Session
from google.genai import types
Expand Down Expand Up @@ -2023,10 +2025,13 @@ async def test_user_app_is_not_mutated(
assert id(app.plugins) == original_plugins_id

@pytest.mark.asyncio
async def test_runner_falls_back_to_bare_agent_when_no_app(
async def test_runner_wraps_bare_agent_in_app_when_no_app(
self, runner_cls, mock_session_service, stop_immediately_simulator
):
"""When `app` is None, Runner is built with the legacy `agent=`/`plugins=` shape."""
"""When `app` is None, the bare agent is wrapped in an App with the eval plugins.

Passing `plugins=` to Runner is deprecated, so the eval Runner must not use it.
"""
root_agent = BaseAgent(name="root_agent")

await EvaluationGenerator._generate_inferences_from_root_agent(
Expand All @@ -2036,9 +2041,12 @@ async def test_runner_falls_back_to_bare_agent_when_no_app(

runner_cls.assert_called_once()
kwargs = runner_cls.call_args.kwargs
assert "app" not in kwargs
assert kwargs["agent"] is root_agent
plugin_names = [p.name for p in kwargs["plugins"]]
assert "agent" not in kwargs
assert "plugins" not in kwargs
runner_app = kwargs["app"]
assert isinstance(runner_app, App)
assert runner_app.root_agent is root_agent
plugin_names = [p.name for p in runner_app.plugins]
assert plugin_names == [
"request_intercepter_plugin",
"ensure_retry_options",
Expand All @@ -2065,6 +2073,35 @@ async def test_root_agent_override_propagates_to_merged_app(
assert app.root_agent is full_root


class TestBuildEvalRunnerKwargs:
"""Tests that the eval Runner kwargs build a real Runner without warnings."""

@pytest.mark.parametrize(
"app_name",
# Eval app names come from the eval session (e.g. an eval case's
# `session_input.app_name`) and are not always valid App names. The
# legacy Runner path accepted them, so must this.
["my_app", "my-agent.evalset"],
)
def test_no_app_builds_runner_without_deprecation_warning(self, app_name):
root_agent = BaseAgent(name="root_agent")
eval_plugin = _SpyPlugin(name="eval_plugin")

kwargs = evaluation_generator_module._build_eval_runner_kwargs(
root_agent=root_agent,
app_name=app_name,
app=None,
internal_eval_plugins=[eval_plugin],
)
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
runner = Runner(**kwargs, session_service=InMemorySessionService())

assert runner.app_name == app_name
assert runner.agent is root_agent
assert [p.name for p in runner.plugin_manager.plugins] == ["eval_plugin"]


# -----------------------------------------------------------------------------
# `generate_responses_from_session` -- replays a recorded session file instead of
# invoking an agent, annotating each eval row with what the session actually did.
Expand Down