Skip to content

Commit 64f11a6

Browse files
google-genai-botcopybara-github
authored andcommitted
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
1 parent d56dd08 commit 64f11a6

File tree

3 files changed

+9
-21
lines changed

3 files changed

+9
-21
lines changed

src/google/adk/evaluation/local_eval_service.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,14 @@ def __init__(
6868
self,
6969
root_agent: BaseAgent,
7070
eval_sets_manager: EvalSetsManager,
71-
metric_evaluator_registry: Optional[MetricEvaluatorRegistry] = None,
72-
session_service: Optional[BaseSessionService] = None,
73-
artifact_service: Optional[BaseArtifactService] = None,
71+
metric_evaluator_registry: MetricEvaluatorRegistry = DEFAULT_METRIC_EVALUATOR_REGISTRY,
72+
session_service: BaseSessionService = InMemorySessionService(),
73+
artifact_service: BaseArtifactService = InMemoryArtifactService(),
7474
eval_set_results_manager: Optional[EvalSetResultsManager] = None,
7575
session_id_supplier: Callable[[], str] = _get_session_id,
7676
):
7777
self._root_agent = root_agent
7878
self._eval_sets_manager = eval_sets_manager
79-
metric_evaluator_registry = (
80-
metric_evaluator_registry or DEFAULT_METRIC_EVALUATOR_REGISTRY
81-
)
82-
session_service = session_service or InMemorySessionService()
83-
artifact_service = artifact_service or InMemoryArtifactService()
8479
self._metric_evaluator_registry = metric_evaluator_registry
8580
self._session_service = session_service
8681
self._artifact_service = artifact_service

src/google/adk/runners.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def run(
188188
user_id: str,
189189
session_id: str,
190190
new_message: types.Content,
191-
run_config: Optional[RunConfig] = None,
191+
run_config: RunConfig = RunConfig(),
192192
) -> Generator[Event, None, None]:
193193
"""Runs the agent.
194194
@@ -205,7 +205,6 @@ def run(
205205
Yields:
206206
The events generated by the agent.
207207
"""
208-
run_config = run_config or RunConfig()
209208
event_queue = queue.Queue()
210209

211210
async def _invoke_run_async():
@@ -249,7 +248,7 @@ async def run_async(
249248
session_id: str,
250249
new_message: types.Content,
251250
state_delta: Optional[dict[str, Any]] = None,
252-
run_config: Optional[RunConfig] = None,
251+
run_config: RunConfig = RunConfig(),
253252
) -> AsyncGenerator[Event, None]:
254253
"""Main entry method to run the agent in this runner.
255254
@@ -262,7 +261,6 @@ async def run_async(
262261
Yields:
263262
The events generated by the agent.
264263
"""
265-
run_config = run_config or RunConfig()
266264

267265
async def _run_with_trace(
268266
new_message: types.Content,
@@ -428,7 +426,7 @@ async def run_live(
428426
user_id: Optional[str] = None,
429427
session_id: Optional[str] = None,
430428
live_request_queue: LiveRequestQueue,
431-
run_config: Optional[RunConfig] = None,
429+
run_config: RunConfig = RunConfig(),
432430
session: Optional[Session] = None,
433431
) -> AsyncGenerator[Event, None]:
434432
"""Runs the agent in live mode (experimental feature).
@@ -454,7 +452,6 @@ async def run_live(
454452
.. NOTE::
455453
Either `session` or both `user_id` and `session_id` must be provided.
456454
"""
457-
run_config = run_config or RunConfig()
458455
if session is None and (user_id is None or session_id is None):
459456
raise ValueError(
460457
'Either session or user_id and session_id must be provided.'
@@ -604,7 +601,7 @@ def _new_invocation_context(
604601
*,
605602
new_message: Optional[types.Content] = None,
606603
live_request_queue: Optional[LiveRequestQueue] = None,
607-
run_config: Optional[RunConfig] = None,
604+
run_config: RunConfig = RunConfig(),
608605
) -> InvocationContext:
609606
"""Creates a new invocation context.
610607
@@ -617,7 +614,6 @@ def _new_invocation_context(
617614
Returns:
618615
The new invocation context.
619616
"""
620-
run_config = run_config or RunConfig()
621617
invocation_id = new_invocation_context_id()
622618

623619
if run_config.support_cfc and isinstance(self.agent, LlmAgent):
@@ -649,10 +645,9 @@ def _new_invocation_context_for_live(
649645
session: Session,
650646
*,
651647
live_request_queue: Optional[LiveRequestQueue] = None,
652-
run_config: Optional[RunConfig] = None,
648+
run_config: RunConfig = RunConfig(),
653649
) -> InvocationContext:
654650
"""Creates a new invocation context for live multi-agent."""
655-
run_config = run_config or RunConfig()
656651

657652
# For live multi-agent, we need model's text transcription as context for
658653
# next agent.

tests/unittests/cli/test_fast_api.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import tempfile
2222
import time
2323
from typing import Any
24-
from typing import Optional
2524
from unittest.mock import MagicMock
2625
from unittest.mock import patch
2726

@@ -121,9 +120,8 @@ async def dummy_run_async(
121120
session_id,
122121
new_message,
123122
state_delta=None,
124-
run_config: Optional[RunConfig] = None,
123+
run_config: RunConfig = RunConfig(),
125124
):
126-
run_config = run_config or RunConfig()
127125
yield _event_1()
128126
await asyncio.sleep(0)
129127

0 commit comments

Comments
 (0)