From b2ac7d14e08ae17419358662ec2dda8b5f50e554 Mon Sep 17 00:00:00 2001 From: Mike Dabydeen Date: Tue, 7 Jul 2026 00:23:14 -0400 Subject: [PATCH] refactor(agents): Improve transfer type safety Replace Any annotations with BaseAgent and Sequence to make transfer helper contracts explicit without changing runtime behavior. Author: Mike Dabydeen --- .../adk/flows/llm_flows/agent_transfer.py | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/google/adk/flows/llm_flows/agent_transfer.py b/src/google/adk/flows/llm_flows/agent_transfer.py index 050f2ae8a3c..6c8626a6eae 100644 --- a/src/google/adk/flows/llm_flows/agent_transfer.py +++ b/src/google/adk/flows/llm_flows/agent_transfer.py @@ -17,8 +17,8 @@ from __future__ import annotations import typing -from typing import Any from typing import AsyncGenerator +from typing import Sequence from typing_extensions import override @@ -30,8 +30,8 @@ from ._base_llm_processor import BaseLlmRequestProcessor if typing.TYPE_CHECKING: - from ...agents import BaseAgent - from ...agents import LlmAgent + from ...agents.base_agent import BaseAgent + from ...agents.llm_agent import LlmAgent class _AgentTransferLlmRequestProcessor(BaseLlmRequestProcessor): @@ -72,8 +72,7 @@ async def run_async( request_processor = _AgentTransferLlmRequestProcessor() -def _build_target_agents_info(target_agent: Any) -> str: - # TODO: Refactor the annotation of the parameters +def _build_target_agents_info(target_agent: BaseAgent) -> str: return f""" Agent name: {target_agent.name} Agent description: {target_agent.description} @@ -85,17 +84,16 @@ def _build_target_agents_info(target_agent: Any) -> str: def _build_transfer_instruction_body( tool_name: str, - target_agents: list[Any], + target_agents: Sequence[BaseAgent], ) -> str: """Build the core transfer instruction text. - TODO: Refactor the annotation of the parameters This is the agent-tree-agnostic portion of transfer instructions. It - works with any objects having ``.name`` and ``.description`` attributes + works with any BaseAgent implementation. Args: tool_name: The name of the transfer tool (e.g. 'transfer_to_agent'). - target_agents: Objects with ``.name`` and ``.description``. + target_agents: Agents available as transfer targets. Returns: Instruction text for the LLM about agent transfers. @@ -128,8 +126,8 @@ def _build_transfer_instruction_body( def _build_transfer_instructions( tool_name: str, - agent: 'LlmAgent', - target_agents: list['BaseAgent'], + agent: LlmAgent, + target_agents: Sequence[BaseAgent], ) -> str: """Build instructions for agent transfer (agent-tree variant).