Agent Foundry
All Problems

#8. Build a Weather Tool

EasyTool Calling

The Problem

Your weather assistant agent has a skeleton tool that does nothing — it just returns None. The agent needs a fully implemented weather lookup tool that the model can call to answer weather questions. The tool must have a proper docstring (so the model knows what it does), type hints, return structured data, and handle invalid city names gracefully. You're given a simulated weather database as a Python dictionary; your job is to implement the tool function so the agent can actually look up weather data.

Examples

Example 1

User input: What's the weather in Tokyo?

Current (bad) output: The tool returns None, so the agent either hallucinates weather data or says it can't help.

Expected (good) output: The tool returns {"city": "tokyo", "temperature": "68°F", "condition": "Clear", "humidity": "55%"} and the agent responds: The weather in Tokyo is currently 68°F and Clear with 55% humidity.

Example 2

User input: How's the weather in Berlin?

Current (bad) output: The tool returns None or crashes.

Expected (good) output: The tool returns a helpful message like "Weather data not available for 'Berlin'. Available cities: New York, London, Tokyo, Paris, Sydney." and the agent relays this to the user.

Example 3

User input: Is it raining in Paris?

Current (bad) output: Agent guesses or crashes.

Expected (good) output: The tool returns {"city": "paris", "temperature": "64°F", "condition": "Rainy", "humidity": "82%"} and the agent responds: Yes, it is currently rainy in Paris at 64°F with 82% humidity.

Your Task

Implement the get_weather tool function so that it:

  • Has a clear docstring explaining what it does and its city parameter.
  • Uses proper type hints (e.g., city: str -> str).
  • Looks up the city in the provided WEATHER_DATA dictionary (case-insensitive).
  • Returns a structured result with city, temperature, condition, and humidity.
  • Returns a helpful error message listing available cities when the city is not found.

Do not change the agent setup, prompt, or model configuration.

Evaluation

Submissions are checked for the following:

  • Tool has proper schema: The function has a descriptive docstring, type hints, and is properly decorated as a tool.
  • Handles invalid cities: Unrecognized city names produce a helpful error message, not a crash or None.
  • Returns structured weather data: Valid lookups return a structured object with all required weather fields.

Constraints

  • The tool must have a proper docstring and type hints
  • The tool must handle invalid city names gracefully
  • The tool must return structured weather data, not free text
Starter Code
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import tool

llm = ChatOpenAI(model="gpt-4o-mini")

# TODO: Implement the weather lookup tool
# Requirements:
# 1. Add a proper docstring describing the tool and its parameters
# 2. Add type hints for the function signature
# 3. Use the WEATHER_DATA dict below as the data source
# 4. Return a structured dict with: city, temperature, condition, humidity
# 5. Handle invalid cities by returning a helpful error message

WEATHER_DATA = {
    "new york": {"temperature": "72°F", "condition": "Sunny", "humidity": "45%"},
    "london": {"temperature": "58°F", "condition": "Cloudy", "humidity": "78%"},
    "tokyo": {"temperature": "68°F", "condition": "Clear", "humidity": "55%"},
    "paris": {"temperature": "64°F", "condition": "Rainy", "humidity": "82%"},
    "sydney": {"temperature": "75°F", "condition": "Partly Cloudy", "humidity": "60%"},
}

@tool
def get_weather(city):
    # TODO: Implement this tool
    pass

tools = [get_weather]

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a weather assistant. Use the weather tool to answer questions about the weather."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)

# Test
result = executor.invoke({"input": "What's the weather in Tokyo?"})
print(result["output"])
Open in Google Colab
Evaluation Criteria0/3