Skip to content

ADK 工具的限制

ADK 的某些工具存在限制,可能会影响你在智能体工作流中实现它们的方式。本页列出了这些工具限制以及可用的解决方法。

每个智能体一个工具的限制

通常情况下,你可以在一个智能体中使用多个工具,但在智能体中使用特定工具会排除该智能体中任何其他工具的使用。以下 ADK 工具只能单独使用,在单个智能体对象中不能与任何其他工具一起使用:

例如,以下在单个智能体中将这些工具之一与其他工具一起使用的方法不受支持:

root_agent = Agent(
    name="RootAgent",
    model="gemini-2.5-flash",
    description="Code Agent",
    tools=[custom_function],
    code_executor=BuiltInCodeExecutor() # <-- 与工具一起使用时不受支持
)
 LlmAgent searchAgent =
        LlmAgent.builder()
            .model(MODEL_ID)
            .name("SearchAgent")
            .instruction("You're a specialist in Google Search")
            .tools(new GoogleSearchTool(), new YourCustomTool()) // <-- 不受支持
            .build();

解决方法 #1: AgentTool.create() 方法

Supported in ADKPythonJava

以下代码示例演示了如何通过使用多个智能体来使用多个内置工具,或如何将内置工具与其他工具一起使用:

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-2.0-flash',
    name='SearchAgent',
    instruction="""
    你是 Google 搜索专家
    """,
    tools=[google_search],
)
coding_agent = Agent(
    model='gemini-2.0-flash',
    name='CodeAgent',
    instruction="""
    你是代码执行专家
    """,
    code_executor=BuiltInCodeExecutor(),
)
root_agent = Agent(
    name="RootAgent",
    model="gemini-2.0-flash",
    description="Root Agent",
    tools=[AgentTool(agent=search_agent), AgentTool(agent=coding_agent)],
)
import com.google.adk.agents.BaseAgent;
import com.google.adk.agents.LlmAgent;
import com.google.adk.tools.AgentTool;
import com.google.adk.tools.BuiltInCodeExecutionTool;
import com.google.adk.tools.GoogleSearchTool;
import com.google.common.collect.ImmutableList;

public class NestedAgentApp {

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

  public static void main(String[] args) {

    // 定义 SearchAgent
    LlmAgent searchAgent =
        LlmAgent.builder()
            .model(MODEL_ID)
            .name("SearchAgent")
            .instruction("你是 Google 搜索专家")
            .tools(new GoogleSearchTool()) // 实例化 GoogleSearchTool
            .build();


    // 定义 CodingAgent
    LlmAgent codingAgent =
        LlmAgent.builder()
            .model(MODEL_ID)
            .name("CodeAgent")
            .instruction("你是代码执行专家")
            .tools(new BuiltInCodeExecutionTool()) // 实例化 BuiltInCodeExecutionTool
            .build();

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

    // 注意:此示例仅演示智能体定义。
    // 要运行这些智能体,你需要将它们与 Runner 和 SessionService 集成,
    // 类似于之前的示例。
    System.out.println("智能体定义成功:");
    System.out.println("  根智能体: " + rootAgent.name());
    System.out.println("  搜索智能体(嵌套): " + searchAgent.name());
    System.out.println("  代码智能体(嵌套): " + codingAgent.name());
  }
}

解决方法 #2: bypass_multi_tools_limit

Supported in ADKPythonJava

ADK Python 有一个内置的解决方法,可以绕过 GoogleSearchToolVertexAiSearchTool 的此限制 (使用 bypass_multi_tools_limit=True 启用), 如 built_in_multi_tools 示例智能体所示。

警告

内置工具不能在子智能体中使用,但 ADK Python 中的 GoogleSearchToolVertexAiSearchTool 除外, 因为上述解决方法。

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

url_context_agent = Agent(
    model='gemini-2.5-flash',
    name='UrlContextAgent',
    instruction="""
    你是 URL 上下文专家
    """,
    tools=[url_context],
)
coding_agent = Agent(
    model='gemini-2.5-flash',
    name='CodeAgent',
    instruction="""
    你是代码执行专家
    """,
    code_executor=BuiltInCodeExecutor(),
)
root_agent = Agent(
    name="RootAgent",
    model="gemini-2.5-flash",
    description="Root Agent",
    sub_agents=[
        url_context_agent,
        coding_agent
    ],
)
LlmAgent searchAgent =
    LlmAgent.builder()
        .model("gemini-2.5-flash")
        .name("SearchAgent")
        .instruction("你是 Google 搜索专家")
        .tools(new GoogleSearchTool())
        .build();

LlmAgent codingAgent =
    LlmAgent.builder()
        .model("gemini-2.5-flash")
        .name("CodeAgent")
        .instruction("你是代码执行专家")
        .tools(new BuiltInCodeExecutionTool())
        .build();


LlmAgent rootAgent =
    LlmAgent.builder()
        .name("RootAgent")
        .model("gemini-2.5-flash")
        .description("Root Agent")
        .subAgents(searchAgent, codingAgent) // 不受支持,因为子智能体使用内置工具。
        .build();