Skip to content

ADK 工具的限制

ADK 的某些内置工具存在特定的联用限制,这可能会影响你在智能体工作流中集成它们的方式。本页详细说明了这些局限性及其推荐的解决方法。

单智能体单工具限制

ADK Python:内置的 Search 解决方法需手动启用

ADK Python v1.16.0 及更高版本可以绕过 Google 搜索和 Agent Search 工具的此限制,但默认不会绕过。你必须自行用 bypass_multi_tools_limit=True 构造该工具;从 google.adk.tools 导出的共享 google_search 实例并未开启此标志。参见下方解决方法 #2。

  • 使用 Gemini API 的代码执行(注意:在 TypeScript 中,这需要 Gemini 2.0+,且不受此限制)
  • 使用 Gemini API 的 Google 搜索(注意:限制仅适用于 TypeScript 中的 Gemini 1.x 模型)
  • Agent Search(注意:目前在 TypeScript 中不可用)

以下这种尝试在单个智能体中整合多个互斥工具的做法是不受支持的:

root_agent = Agent(
    name="RootAgent",
    model="gemini-flash-latest",
    description="Code Agent",
    tools=[custom_function],
    code_executor=BuiltInCodeExecutor() # <-- 报错:不能与常规工具同时配置
)
import {Agent, BuiltInCodeExecutor} from '@google/adk';

const rootAgent = new Agent({
  name: 'RootAgent',
  model: 'gemini-flash-latest',
  description: 'Code Agent',
  tools: [myCustomTool], // Assume myCustomTool is defined
  codeExecutor: new BuiltInCodeExecutor(), // <-- NOT supported when used with tools
});
LlmAgent searchAgent = LlmAgent.builder()
    .model(MODEL_ID)
    .name("SearchAgent")
    .tools(new GoogleSearchTool(), new YourCustomTool()) // <-- 不受支持
    .build();
val searchAgent = LlmAgent(
    name = "SearchAgent",
    model = Gemini(name = "gemini-flash-latest"),
    instruction = Instruction("You're a specialist in Google Search"),
    tools = listOf(GoogleSearchTool(), YourCustomTool()) // <-- 不受支持
)

解决方法 #1:将智能体包装进 AgentTool

Supported in ADKPythonTypeScript (v0.6.1+)JavaKotlin v0.1.0

解决此类限制的最有效方法是采用“智能体即工具”模式。你可以创建专门的子智能体来封装这些受限工具,然后将子智能体作为工具交给父智能体管理。

from google.adk.tools.agent_tool import AgentTool
from google.adk.agents import Agent
from google.adk.tools import google_search
from google.adk.code_executors import BuiltInCodeExecutor

# 专门负责搜索的智能体
search_agent = Agent(
    model='gemini-flash-latest',
    name='SearchAgent',
    tools=[google_search],
)

# 专门负责代码执行的智能体
coding_agent = Agent(
    model='gemini-flash-latest',
    name='CodeAgent',
    code_executor=BuiltInCodeExecutor(),
)

# 根智能体通过 AgentTool 统一调度
root_agent = Agent(
    name="RootAgent",
    model="gemini-flash-latest",
    description="Root Agent",
    tools=[AgentTool(agent=search_agent), AgentTool(agent=coding_agent)],
)
import {Agent, AgentTool, BuiltInCodeExecutor, GOOGLE_SEARCH} from '@google/adk';

const searchAgent = new Agent({
  model: 'gemini-flash-latest',
  name: 'SearchAgent',
  instruction: "You're a specialist in Google Search",
  tools: [GOOGLE_SEARCH],
});

const codingAgent = new Agent({
  model: 'gemini-flash-latest', // Built-in code execution requires Gemini 2.0+ in ADK JS
  name: 'CodeAgent',
  instruction: "You're a specialist in Code Execution",
  codeExecutor: new BuiltInCodeExecutor(),
});

const rootAgent = new Agent({
  name: 'RootAgent',
  model: 'gemini-flash-latest',
  description: 'Root Agent',
  tools: [new AgentTool({agent: searchAgent}), new AgentTool({agent: codingAgent})],
});
// 定义核心逻辑
LlmAgent searchAgent = LlmAgent.builder()
    .name("SearchAgent")
    .tools(new GoogleSearchTool())
    .build();

LlmAgent codingAgent = LlmAgent.builder()
    .name("CodeAgent")
    .tools(new BuiltInCodeExecutionTool())
    .build();

  private static final String MODEL_ID = "gemini-flash-latest";

  public static void main(String[] args) {

    // Define the SearchAgent
    LlmAgent searchAgent =
        LlmAgent.builder()
            .model(MODEL_ID)
            .name("SearchAgent")
            .instruction("You're a specialist in Google Search")
            .tools(new GoogleSearchTool()) // Instantiate GoogleSearchTool
            .build();


    // Define the CodingAgent
    LlmAgent codingAgent =
        LlmAgent.builder()
            .model(MODEL_ID)
            .name("CodeAgent")
            .instruction("You're a specialist in Code Execution")
            .tools(new BuiltInCodeExecutionTool()) // 实例化 BuiltInCodeExecutionTool
            .build();

    // 定义 RootAgent,它使用 AgentTool.create() 包装 SearchAgent 和 CodingAgent
    BaseAgent rootAgent =
        LlmAgent.builder()
            .name("RootAgent")
            .model(MODEL_ID)
            .description("Root Agent")
            .tools(
                AgentTool.create(searchAgent), // 使用 create 方法
                AgentTool.create(codingAgent)   // 使用 create 方法
             )
            .build();

    // 注意:本示例仅演示智能体的定义。
    // 要运行这些智能体,你需要将它们与 Runner 和 SessionService 集成,
    // 类似于前面的示例。
    System.out.println("Agents defined successfully:");
    System.out.println("  Root Agent: " + rootAgent.name());
    System.out.println("  Search Agent (nested): " + searchAgent.name());
    System.out.println("  Code Agent (nested): " + codingAgent.name());
  }
}
// Define the SearchAgent
val searchAgent =
    LlmAgent(
        name = "SearchAgent",
        model = Gemini(name = modelId),
        instruction = Instruction("You're a specialist in Google Search"),
        tools = listOf(GoogleSearchTool()),
    )

// Define another agent (e.g., for specialized tasks)
val taskAgent =
    LlmAgent(
        name = "TaskAgent",
        model = Gemini(name = modelId),
        instruction = Instruction("You're a specialist in performing specific tasks."),
    )

// Define the RootAgent, which uses AgentTool to wrap SearchAgent and TaskAgent
val rootAgent =
    LlmAgent(
        name = "RootAgent",
        model = Gemini(name = modelId),
        description = "Root Agent",
        tools =
            listOf(
                AgentTool(agent = searchAgent),
                AgentTool(agent = taskAgent),
            ),
    )

在 ADK Python 中,AgentTool 不会把被包装智能体的基础信息获取 (Grounding) 元数据传回给调用方智能体。在 AgentTool 上设置 propagate_grounding_metadata=True,可将该元数据以 temp:_adk_grounding_metadata 键存储在会话状态中。ADK 只有在父智能体拥有名为 google_search_agent 的工具时,才会将其附加到父智能体自身的响应上——这个工具正是下方解决方法 #2 为你创建的。

解决方法 #2:bypass_multi_tools_limit

Supported in ADKPythonJavaKotlin v0.1.0

ADK Python 提供了一个内置的解决方法,可以绕过 GoogleSearchTool 和 VertexAiSearchTool 的这一限制(使用 bypass_multi_tools_limit=True 启用),如 built_in_multi_tools 示例智能体所示。

请在你自己构建的工具实例上设置该标志,例如从 google.adk.tools.google_search_tool 导入的 GoogleSearchTool(bypass_multi_tools_limit=True)。google.adk.tools 导出的共享 google_search 实例默认不开启该标志。当智能体的 tools 列表中有多个条目,或可以将控制权转移给另一个智能体时,该绕过才会生效。

关于子智能体 (Sub-agent) 的局限性

内置工具不能在子智能体中使用。在 ADK Python 中,GoogleSearchTool 和 VertexAiSearchTool 仅当子智能体以 bypass_multi_tools_limit=True 构造该工具时才是例外。

例如,以下在子智能体中使用内置工具的方式是不受支持的:

url_context_agent = Agent(
    model='gemini-flash-latest',
    name='UrlContextAgent',
    instruction="""
    You're a specialist in URL Context
    """,
    tools=[url_context],
)
coding_agent = Agent(
    model='gemini-flash-latest',
    name='CodeAgent',
    instruction="""
    You're a specialist in Code Execution
    """,
    code_executor=BuiltInCodeExecutor(),
)
root_agent = Agent(
    name="RootAgent",
    model="gemini-flash-latest",
    description="Root Agent",
    sub_agents=[
        url_context_agent,
        coding_agent
    ],
)
import {Agent, BuiltInCodeExecutor} from '@google/adk';

const urlContextAgent = new Agent({
  model: 'gemini-flash-latest',
  name: 'UrlContextAgent',
  instruction: "You're a specialist in URL Context",
  tools: [myCustomTool], // Assume myCustomTool is defined
});

const codingAgent = new Agent({
  model: 'gemini-flash-latest',
  name: 'CodeAgent',
  instruction: "You're a specialist in Code Execution",
  codeExecutor: new BuiltInCodeExecutor(),
});

const rootAgent = new Agent({
  name: 'RootAgent',
  model: 'gemini-flash-latest',
  description: 'Root Agent',
  subAgents: [urlContextAgent, codingAgent], // NOT supported when sub-agents use built-in tools
});
LlmAgent searchAgent =
    LlmAgent.builder()
        .model("gemini-flash-latest")
        .name("SearchAgent")
        .instruction("You're a specialist in Google Search")
        .tools(new GoogleSearchTool())
        .build();

LlmAgent codingAgent =
    LlmAgent.builder()
        .model("gemini-flash-latest")
        .name("CodeAgent")
        .instruction("You're a specialist in Code Execution")
        .tools(new BuiltInCodeExecutionTool())
        .build();


LlmAgent rootAgent =
    LlmAgent.builder()
        .name("RootAgent")
        .model("gemini-flash-latest")
        .description("Root Agent")
        .subAgents(searchAgent, codingAgent) // Not supported, as the sub agents use built in tools.
        .build();
val searchAgent = LlmAgent(
    model = Gemini(name = "gemini-flash-latest"),
    name = "SearchAgent",
    instruction = Instruction("You're a specialist in Google Search"),
    tools = listOf(GoogleSearchTool())
)

val codingAgent = LlmAgent(
    model = Gemini(name = "gemini-flash-latest"),
    name = "CodeAgent",
    instruction = Instruction("You're a specialist in Code Execution")
    // Kotlin 目前在核心模块中没有 BuiltInCodeExecutionTool
)


val rootAgent = LlmAgent(
    name = "RootAgent",
    model = Gemini(name = "gemini-flash-latest"),
    description = "Root Agent",
    subAgents = listOf(searchAgent, codingAgent) // Not supported when sub-agents use built-in tools
)