A function is a tool
Put a Python function in the tools list and the model can choose to call it. Lesson 3 showed what ADK reads off it. This lesson gives one to an agent.
From here on the lessons use a small helper called ask, which is the runner, the session and the loop from lesson 5 wrapped in one function. It is not part of ADK; it just keeps these pages about the thing being taught.
The tool
def lookup_order(order_id: str) -> str:
"""Look up the status of an order by its id."""
return f"Order {order_id} shipped on 3 March."The same function from lesson 2, unchanged. Nothing about it knows it is a tool.
The agent that has it
agent = LlmAgent(
name="support",
model=PretendModel(replies=[call("lookup_order", order_id="A17"),
say("It shipped on 3 March.")]),
instruction="Help with orders.",
tools=[lookup_order],
)One line does the work: tools=[lookup_order]. ADK wraps the function, builds the description from lesson 3, and hands that to the model.
print(await ask(agent, "Where is order A17?"))The model asked for the tool, ADK ran it, and the model answered from the result. You have now seen the whole loop three times, which is deliberate: everything else in this course changes some part of it.
Required and optional arguments
A parameter with a type hint and no default is required. Give it a default and the model may leave it out.
def get_weather(city: str, unit: str = "Celsius"):
"""Get the weather for a city."""Here the model must send a city and may send a unit. That single difference is worth checking whenever a model keeps calling a tool without an argument you expected.
get_data with the docstring "gets data" will be called at the wrong times or not at all.- Give the tool a second parameter and script the model to send it.
- Rename the tool to something vague and see whether the fallback rule still finds it.
You understood something today that you didn't yesterday.