OpenWeather

Fetch current weather, forecasts, air-quality data, and geocoding from the OpenWeather API inside an Agno agent.

Use OpenWeatherTools to fetch current weather, forecasts, air-quality data, and geocoding results.

Prerequisites

Install dependencies with uv pip install -U agno openai requests.

  1. Create an OpenWeather API key.
  2. Export OPENWEATHER_API_KEY and OPENAI_API_KEY.
"""
OpenWeather Usage:
- Get current weather for a location
- Get weather forecast for a location
- Get air pollution data for a location
- Geocode a location name to coordinates
"""

from agno.agent import Agent
from agno.tools.openweather import OpenWeatherTools

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------


# Example 1: Enable all OpenWeather functions
agent_all = Agent(
    tools=[
        OpenWeatherTools(
            all=True,  # Enable all OpenWeather functions
            units="imperial",  # Options: 'standard', 'metric', 'imperial'
        )
    ],
    markdown=True,
)

# Example 2: Enable specific OpenWeather functions only
agent_specific = Agent(
    tools=[
        OpenWeatherTools(
            enable_current_weather=True,
            enable_forecast=True,
            enable_air_pollution=True,
            enable_geocoding=True,
            units="metric",
        )
    ],
    markdown=True,
)

# Example 3: Default behavior with all functions enabled
agent = Agent(
    tools=[
        OpenWeatherTools(
            enable_current_weather=True,
            enable_forecast=True,
            enable_air_pollution=True,
            enable_geocoding=True,
            units="imperial",  # Options: 'standard', 'metric', 'imperial'
        )
    ],
    markdown=True,
)

# Example usage with all functions enabled

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    print("=== Example 1: Using all OpenWeather functions ===")
    agent_all.print_response(
        "Give me a comprehensive weather report for Tokyo including current weather, forecast, and air quality",
        markdown=True,
    )

    # Example usage with specific functions only
    print(
        "\n=== Example 2: Using specific OpenWeather functions (current weather + geocoding) ==="
    )
    agent_specific.print_response(
        "What's the current weather in Tokyo?",
        markdown=True,
    )

    # Example usage with default configuration
    print("\n=== Example 3: Default OpenWeather agent usage ===")
    agent.print_response(
        "What's the current weather in Tokyo?",
        markdown=True,
    )

    # Additional examples (commented out to avoid API calls)
    # agent.print_response(
    #     "Give me a 3-day weather forecast for New York City",
    #     markdown=True,
    # )

    # agent.print_response(
    #     "What's the air quality in Beijing right now?",
    #     markdown=True,
    # )

    # agent.print_response(
    #     "Compare the current weather between London, Paris, and Rome",
    #     markdown=True,
    # )

Forecasts use up to 40 three-hour entries from the five-day endpoint. Supply a specific city and country when a name is ambiguous: each weather call uses the first geocoding match. All three shown configurations enable all four functions, even though the second example’s printed label says only current weather and geocoding.

Run the Example

# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno
git checkout d703c34f3abf3c41275d3fb2da6e0518a8881f24

# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate

export OPENAI_API_KEY="your_openai_api_key_here"
export OPENWEATHER_API_KEY="your_openweather_api_key_here"

python cookbook/91_tools/openweather_tools.py

For details, see OpenWeather cookbook.