chore: Avoid mutable default arguments in local_eval_service and runners

Changed default values for `session_service`, `artifact_service`, and `run_config` from instances of mutable classes to `None`. Instances are now created within the function body if the argument is not provided, preventing unexpected shared state across function calls.

PiperOrigin-RevId: 804624564
This commit is contained in:
Google Team Member
2025-09-08 16:11:24 -07:00
committed by Copybara-Service
parent d56dd08072
commit 64f11a6a67
3 changed files with 9 additions and 21 deletions
@@ -68,19 +68,14 @@ class LocalEvalService(BaseEvalService):
self,
root_agent: BaseAgent,
eval_sets_manager: EvalSetsManager,
metric_evaluator_registry: Optional[MetricEvaluatorRegistry] = None,
session_service: Optional[BaseSessionService] = None,
artifact_service: Optional[BaseArtifactService] = None,
metric_evaluator_registry: MetricEvaluatorRegistry = DEFAULT_METRIC_EVALUATOR_REGISTRY,
session_service: BaseSessionService = InMemorySessionService(),
artifact_service: BaseArtifactService = InMemoryArtifactService(),
eval_set_results_manager: Optional[EvalSetResultsManager] = None,
session_id_supplier: Callable[[], str] = _get_session_id,
):
self._root_agent = root_agent
self._eval_sets_manager = eval_sets_manager
metric_evaluator_registry = (
metric_evaluator_registry or DEFAULT_METRIC_EVALUATOR_REGISTRY
)
session_service = session_service or InMemorySessionService()
artifact_service = artifact_service or InMemoryArtifactService()
self._metric_evaluator_registry = metric_evaluator_registry
self._session_service = session_service
self._artifact_service = artifact_service
+5 -10
View File
@@ -188,7 +188,7 @@ class Runner:
user_id: str,
session_id: str,
new_message: types.Content,
run_config: Optional[RunConfig] = None,
run_config: RunConfig = RunConfig(),
) -> Generator[Event, None, None]:
"""Runs the agent.
@@ -205,7 +205,6 @@ class Runner:
Yields:
The events generated by the agent.
"""
run_config = run_config or RunConfig()
event_queue = queue.Queue()
async def _invoke_run_async():
@@ -249,7 +248,7 @@ class Runner:
session_id: str,
new_message: types.Content,
state_delta: Optional[dict[str, Any]] = None,
run_config: Optional[RunConfig] = None,
run_config: RunConfig = RunConfig(),
) -> AsyncGenerator[Event, None]:
"""Main entry method to run the agent in this runner.
@@ -262,7 +261,6 @@ class Runner:
Yields:
The events generated by the agent.
"""
run_config = run_config or RunConfig()
async def _run_with_trace(
new_message: types.Content,
@@ -428,7 +426,7 @@ class Runner:
user_id: Optional[str] = None,
session_id: Optional[str] = None,
live_request_queue: LiveRequestQueue,
run_config: Optional[RunConfig] = None,
run_config: RunConfig = RunConfig(),
session: Optional[Session] = None,
) -> AsyncGenerator[Event, None]:
"""Runs the agent in live mode (experimental feature).
@@ -454,7 +452,6 @@ class Runner:
.. NOTE::
Either `session` or both `user_id` and `session_id` must be provided.
"""
run_config = run_config or RunConfig()
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.'
@@ -604,7 +601,7 @@ class Runner:
*,
new_message: Optional[types.Content] = None,
live_request_queue: Optional[LiveRequestQueue] = None,
run_config: Optional[RunConfig] = None,
run_config: RunConfig = RunConfig(),
) -> InvocationContext:
"""Creates a new invocation context.
@@ -617,7 +614,6 @@ class Runner:
Returns:
The new invocation context.
"""
run_config = run_config or RunConfig()
invocation_id = new_invocation_context_id()
if run_config.support_cfc and isinstance(self.agent, LlmAgent):
@@ -649,10 +645,9 @@ class Runner:
session: Session,
*,
live_request_queue: Optional[LiveRequestQueue] = None,
run_config: Optional[RunConfig] = None,
run_config: RunConfig = RunConfig(),
) -> InvocationContext:
"""Creates a new invocation context for live multi-agent."""
run_config = run_config or RunConfig()
# For live multi-agent, we need model's text transcription as context for
# next agent.
+1 -3
View File
@@ -21,7 +21,6 @@ import sys
import tempfile
import time
from typing import Any
from typing import Optional
from unittest.mock import MagicMock
from unittest.mock import patch
@@ -121,9 +120,8 @@ async def dummy_run_async(
session_id,
new_message,
state_delta=None,
run_config: Optional[RunConfig] = None,
run_config: RunConfig = RunConfig(),
):
run_config = run_config or RunConfig()
yield _event_1()
await asyncio.sleep(0)