adk-docs
adk-docs copied to clipboard
What is the correct way to import and implement `AgentTool`?
In the docs here and in the Google blog post here the import is from google.adk.agents import agent_tool and the implementation is this:
research_assistant = LlmAgent(
name="ResearchAssistant",
model="gemini-2.0-flash",
description="Finds and summarizes information on a topic.",
tools=[agent_tool.AgentTool(agent=web_searcher), agent_tool.AgentTool(agent=summarizer)]
)
However this results in the error ImportError: cannot import name 'agent_tool' from 'google.adk.agents'.
Elsewhere in the docs here the following pattern is used
tools=[AgentTool(agent=agent_b)]
No example of the required import is provided.
This is a minimum working example:
# ,/research_agent/agent.py
from google.adk.agents import Agent
from google.adk.tools import google_search, AgentTool
GEMINI_MODEL = "gemini-2.5-flash"
web_search = Agent(
name='web_search_agent',
model=GEMINI_MODEL,
instruction=(
'You are a helpful agent that can answer questions using web search.'
),
tools=[google_search],
)
root_agent = Agent(
name='research_assistant',
model=GEMINI_MODEL,
instruction=(
'You are a helpful research assistant. You can use web search to answer'
' questions.'
),
tools=[
AgentTool(web_search),
],
)
# ./research_agent/__init__.py
from . import agent
# ./main.py
import os, uvicorn
from google.adk.cli.fast_api import get_fast_api_app
from dotenv import load_dotenv
load_dotenv()
AGENT_DIR = os.path.dirname(os.path.abspath(__file__))
ALLOWED_ORIGINS = ["http://localhost", "http://localhost:8080","http://localhost:3000"]
SERVE_WEB_INTERFACE = True
app = get_fast_api_app(
agents_dir=AGENT_DIR,
allow_origins=ALLOWED_ORIGINS,
web=SERVE_WEB_INTERFACE,
)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
@dylan-apex
Thanks.
It seems that AgentTool can be imported from both google.adk.tools and google.adk.tools.agent_tool - is there any difference between the two?
Also, on a side note, in your example you are using Agent and not LlmAgent - why is that?
@jeremypeters LlmAgent and Agent are aliases of one another - I've made a PR recently in the docs to simplify the use and only use Agent in examples to avoid confusing people.
Importing it as from google.adk.tools import AgentTool is technically more correct due to the way that imports/exports work. Theres an __init__.py file in the tools/ directory that exports the AgentTool explicitly, whereas the other way is a direct import from the file where it is defined.
Ok, that's great, thanks very much @dylan-apex
@jwiejulie I don't think there's any changes that need to be made in the docs. In the full Python code example below, there's a proper import example.