解決 ADK 中 Gemini Agent 的 Function Calling 不支援問題

問題描述

當 Agent 掛有 google_search tool 時:

  • 不能直接將此 Agent 掛到 sub_agents
  • 會出現錯誤:
1
AGENT_ERROR: Tool use with function calling is unsupported by the model

參考: GitHub Issue #53

解決方案:將 Agent 包裝成 Agent Tool

關鍵概念

當 Agent 包含 google_search tool 時,必須用 agenttool.New() 將其轉為 Tool,才能被其他 Agent 調用。

Go 實作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import "google.golang.org/adk/tool/agenttool"

// 1. 建立帶有 google_search 的 agent
webSearchAgent, err := llmagent.New(llmagent.Config{
Name: "web_search_agent",
Model: llmModel,
Description: "A specialized agent with web search capabilities.",
Instruction: "Use GoogleSearch tool to find information. Return concise results.",
Tools: []tool.Tool{
geminitool.GoogleSearch{}, // 關鍵:這個 agent 有 google_search
},
})

// 2. 將 agent 轉為 Agent Tool(解決方案)
agentTool := agenttool.New(webSearchAgent, nil) // 必須這樣包裝

// 3. 掛到主 Agent 的 Tools(非 SubAgents)
rootAgent, err := llmagent.New(llmagent.Config{
Name: "root_agent",
Model: llmModel,
Tools: []tool.Tool{agentTool},
})

Python 版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from google.adk.tools.agent_tool import AgentTool
from google.adk.tools import google_search
from google.adk.agents import Agent

web_search_agent = Agent(
name="web_search_agent",
model="gemini-2.0-flash",
tools=[google_search] # 有 google_search 的 agent
)

root_agent = Agent(
name="root_agent",
tools=[AgentTool(agent=web_search_agent)] # 必須轉為 AgentTool
)

關鍵重點

  • Agent 有 google_search → 必須用 agenttool.New(agent, nil) 轉為 Tool
  • 然後掛到 Tools(而非 SubAgents
  • 副作用:包裝後的 Agent Tool 無上下文記憶(詳見:Agent Tool 無上下文記憶,需改用 Sub-agent)