Lab 11: Building a "Global Market Analyst" Challenge
Goalโ
In this lab, you will build an agent that can retrieve live currency exchange rates from a public REST API. You will learn how to use the OpenAPIToolset to automatically generate the necessary tools from an OpenAPI specification.
Step 1: Create the Agent Projectโ
Environment setup:
- Local machine: If you're continuing the course in sequence,
cdinto theadk-trainingproject you created in Module 2 and skip the block below -- your environment and dependencies already carry forward. If you're starting fresh at this module, create that project first:uv init adk-training --python 3.10 && cd adk-training
uv add "google-adk>=2.1.0" python-dotenv- DevContainer / Codespaces (Alternative): If you opened this repository in a DevContainer or GitHub Codespaces, Python,
uv, and the core ADK dependencies are already pre-installed in your container environment.
-
Create the agent project:
uv run adk create market_analyst
cd market_analystNote:
adk createprompts you interactively to choose a model and backend. If this course's target model isn't in the numbered list, choose the "Other models (fill later)" option and set the model manually afterward (inagent.py, and in.envif you're using an API key) rather than picking a model you don't intend to use. -
Set up your API key in the
.envfile for the Gemini model. (The Frankfurter Currency API we are using is completely free and requires no authentication).
Step 2: Define the OpenAPI Specificationโ
Exercise: Open the generated agent.py. adk create scaffolds it with generic boilerplate, not this lab's code -- replace the entire contents of the file with the skeleton below. Your task is to complete the spec for the /latest endpoint's get operation. You can deduce the necessary parameters from the Frankfurter API documentation (or the theory section).
# In agent.py
import json
from google.adk import Agent
from google.adk.tools.openapi_tool import OpenAPIToolset
# ============================================================================
# OPENAPI SPECIFICATION
# ============================================================================
# The `operationId` is critical. The ADK uses it to generate the tool's name
# (e.g., `operationId: "get_latest_rates"` becomes the `get_latest_rates` tool).
FRANKFURTER_SPEC = {
"openapi": "3.0.0",
"info": {
"title": "Frankfurter Currency API",
"description": "Free API for current and historical foreign exchange rates",
"version": "1.0.0"
},
"servers": [{"url": "https://api.frankfurter.dev/v1"}],
"paths": {
"/latest": {
"get": {
# TODO: Complete this section for the "/latest" endpoint.
# - The operationId should be "get_latest_rates".
# - The summary should be "Get latest exchange rates".
# - It needs a "parameters" list with query parameters: "amount", "from", "to".
}
}
}
}
# ============================================================================
# AGENT NODE DEFINITION
# ============================================================================
# TODO: 1. Create the OpenAPIToolset instance.
# TODO: 2. Define the root Agent node and register the toolset.
root_agent = Agent(...)
Step 3: Run and Test Your Agentโ
- Start the agent in terminal mode:
uv run adk run . - Interact with the agent:
- Test its capabilities:
- "Convert 100 USD to EUR."
- "How many Japanese Yen (JPY) can I get for 50 British Pounds (GBP)?"
- "Convert 500 AUD to USD and EUR." (Notice if it calls the API twice or handles it smartly!)
- Observe the logs to see the agent constructing and executing the HTTP requests perfectly based on your spec.
- Test its capabilities:
Having Trouble?โ
If you get stuck, you can find the complete, working code in the lab-solution.md file.
Lab Summaryโ
You have successfully integrated a live REST API into your agent without writing a single manual tool function. You have learned:
- How to translate API documentation into an OpenAPI specification.
- How to use
OpenAPIToolsetto automatically generate tools from a spec. - How to instruct your agent to use the new, auto-generated tools.
Self-Reflection Questionsโ
- What are the main advantages of using
OpenAPIToolsetcompared to writing a custom Python function (likerequests.get(...)) for each API endpoint? - The
operationIdin the OpenAPI spec is very important. What do you think would happen if two different paths in the spec had the sameoperationId? - Many modern web services publish their own OpenAPI specifications (often as a URL like
api.example.com/openapi.json). How does this widespread adoption of the OpenAPI standard make it easier to build powerful, integrated AI agents?
๐ Reference Solution Vaultโ
Module 11 Reference Solution Vault
Active Comprehension Gate โข Zero Guessable URLs โข Guided Verification