Skip to content

Weather Tools

enhancedtoolkits.weather.EnhancedWeatherTools

EnhancedWeatherTools(timeout: int = 30, base_url: Optional[str] = None, add_instructions: bool = True, **kwargs)

Bases: StrictToolkit

Enhanced Weather Tools v1.0

A comprehensive toolkit for weather data including: - Current weather conditions - Weather forecasts - Temperature data - Weather descriptions - Support for multiple languages

Parameters:

Name Type Description Default
timeout int

Request timeout in seconds

30
base_url Optional[str]

Custom base URL for the weather API (default: wttr.in)

None
add_instructions bool

Whether to add usage instructions

True
Source code in src/enhancedtoolkits/weather.py
def __init__(
    self,
    timeout: int = 30,
    base_url: Optional[str] = None,
    add_instructions: bool = True,
    **kwargs,
):
    """
    Initialize Enhanced Weather Tools.

    Args:
        timeout: Request timeout in seconds
        base_url: Custom base URL for the weather API (default: wttr.in)
        add_instructions: Whether to add usage instructions
    """
    if not PYWTTR_AVAILABLE:
        raise ImportError(
            "pywttr package is required. Install with: pip install -U pywttr pywttr-models"
        )

    # Configuration
    self.timeout = max(5, min(120, timeout))  # Limit between 5-120 seconds
    self.base_url = base_url
    self.add_instructions = add_instructions
    self.instructions = EnhancedWeatherTools.get_llm_usage_instructions()

    super().__init__(name="enhanced_weather_tools", **kwargs)

    # Register weather methods
    self.register(self.fetch_current_weather_conditions)
    self.register(self.fetch_weather_forecast)
    self.register(self.fetch_temperature_data)
    self.register(self.fetch_weather_text_description)

    log_info(
        f"Enhanced Weather Tools initialized - Timeout: {self.timeout}, Base URL: {self.base_url or 'default'}"
    )

Functions

fetch_current_weather_conditions

fetch_current_weather_conditions(location: str, language: str = 'en') -> str

Get current weather conditions for a location.

Parameters:

Name Type Description Default
location str

Location name or coordinates

required
language str

Language code (default: en)

'en'

Returns:

Type Description
str

JSON string containing current weather data

Raises:

Type Description
WeatherError

If weather data retrieval fails

Source code in src/enhancedtoolkits/weather.py
def fetch_current_weather_conditions(self, location: str, language: str = "en") -> str:
    """
    Get current weather conditions for a location.

    Args:
        location: Location name or coordinates
        language: Language code (default: en)

    Returns:
        JSON string containing current weather data

    Raises:
        WeatherError: If weather data retrieval fails
    """
    try:
        # Validate inputs
        self._validate_location(location)
        lang = self._validate_language(language)

        log_debug(f"Getting current weather for {location} in {language}")

        # Create Wttr instance with optional custom base URL
        wttr_kwargs = {}
        if self.base_url:
            import pydantic

            wttr_kwargs["base_url"] = pydantic.AnyHttpUrl(self.base_url)

        # Get weather data
        with pywttr.Wttr(**wttr_kwargs) as wttr:
            weather_data = wttr.weather(location, language=lang)

        # Extract current weather
        if (
            not weather_data
            or not hasattr(weather_data, "weather")
            or not weather_data.weather
        ):
            raise WeatherError("No weather data available for this location")

        current = weather_data.weather[0]

        # Format response
        result = {
            "operation": "current_weather",
            "location": location,
            "timestamp": datetime.now().isoformat(),
            "current_condition": {
                "temp_c": getattr(current, "avgtemp_c", None),
                "temp_f": getattr(current, "avgtemp_f", None),
                "feels_like_c": getattr(current, "feelslike_c", None),
                "feels_like_f": getattr(current, "feelslike_f", None),
                "humidity": getattr(current, "humidity", None),
                "weather_desc": self._get_weather_desc(current),
                "wind_speed_kmph": getattr(current, "maxwind_kph", None),
                "wind_speed_mph": getattr(current, "maxwind_mph", None),
                "precipitation_mm": getattr(current, "totalprecip_mm", None),
                "precipitation_in": getattr(current, "totalprecip_in", None),
                "uv_index": getattr(current, "uv", None),
            },
            "metadata": {
                "language": language,
                "source": "wttr.in",
            },
        }

        log_info(f"Retrieved current weather for {location}")
        return json.dumps(result, indent=2, ensure_ascii=False)

    except WeatherError:
        raise
    except Exception as e:
        log_error(f"Error getting current weather: {e}")
        raise WeatherError(f"Failed to get current weather: {e}")

fetch_weather_forecast

fetch_weather_forecast(location: str, days: int = 3, language: str = 'en') -> str

Get weather forecast for a location.

Parameters:

Name Type Description Default
location str

Location name or coordinates

required
days int

Number of days for forecast (1-7)

3
language str

Language code (default: en)

'en'

Returns:

Type Description
str

JSON string containing weather forecast

Raises:

Type Description
WeatherError

If weather data retrieval fails

Source code in src/enhancedtoolkits/weather.py
def fetch_weather_forecast(
    self, location: str, days: int = 3, language: str = "en"
) -> str:
    """
    Get weather forecast for a location.

    Args:
        location: Location name or coordinates
        days: Number of days for forecast (1-7)
        language: Language code (default: en)

    Returns:
        JSON string containing weather forecast

    Raises:
        WeatherError: If weather data retrieval fails
    """
    try:
        # Validate inputs
        self._validate_location(location)
        lang = self._validate_language(language)
        days = max(1, min(7, days))  # Limit between 1-7 days

        log_debug(f"Getting {days}-day forecast for {location} in {language}")

        # Create Wttr instance with optional custom base URL
        wttr_kwargs = {}
        if self.base_url:
            import pydantic

            wttr_kwargs["base_url"] = pydantic.AnyHttpUrl(self.base_url)

        # Get weather data
        with pywttr.Wttr(**wttr_kwargs) as wttr:
            weather_data = wttr.weather(location, language=lang)

        # Extract forecast
        if (
            not weather_data
            or not hasattr(weather_data, "weather")
            or not weather_data.weather
        ):
            raise WeatherError("No weather data available for this location")

        forecast_days = weather_data.weather[:days]

        # Format response
        forecast = []
        for day in forecast_days:
            forecast.append(
                {
                    "date": getattr(day, "date", None),
                    "max_temp_c": getattr(day, "maxtemp_c", None),
                    "max_temp_f": getattr(day, "maxtemp_f", None),
                    "min_temp_c": getattr(day, "mintemp_c", None),
                    "min_temp_f": getattr(day, "mintemp_f", None),
                    "avg_temp_c": getattr(day, "avgtemp_c", None),
                    "avg_temp_f": getattr(day, "avgtemp_f", None),
                    "weather_desc": self._get_weather_desc(day),
                    "max_wind_kph": getattr(day, "maxwind_kph", None),
                    "max_wind_mph": getattr(day, "maxwind_mph", None),
                    "total_precip_mm": getattr(day, "totalprecip_mm", None),
                    "total_precip_in": getattr(day, "totalprecip_in", None),
                    "chance_of_rain": getattr(day, "daily_chance_of_rain", None),
                    "uv_index": getattr(day, "uv", None),
                }
            )

        result = {
            "operation": "weather_forecast",
            "location": location,
            "timestamp": datetime.now().isoformat(),
            "forecast_days": days,
            "forecast": forecast,
            "metadata": {
                "language": language,
                "source": "wttr.in",
            },
        }

        log_info(f"Retrieved {days}-day forecast for {location}")
        return json.dumps(result, indent=2, ensure_ascii=False)

    except WeatherError:
        raise
    except Exception as e:
        log_error(f"Error getting weather forecast: {e}")
        raise WeatherError(f"Failed to get weather forecast: {e}")

fetch_temperature_data

fetch_temperature_data(location: str, language: str = 'en') -> str

Get temperature data for a location.

Parameters:

Name Type Description Default
location str

Location name or coordinates

required
language str

Language code (default: en)

'en'

Returns:

Type Description
str

JSON string containing temperature data

Raises:

Type Description
WeatherError

If weather data retrieval fails

Source code in src/enhancedtoolkits/weather.py
def fetch_temperature_data(self, location: str, language: str = "en") -> str:
    """
    Get temperature data for a location.

    Args:
        location: Location name or coordinates
        language: Language code (default: en)

    Returns:
        JSON string containing temperature data

    Raises:
        WeatherError: If weather data retrieval fails
    """
    try:
        # Validate inputs
        self._validate_location(location)
        lang = self._validate_language(language)

        log_debug(f"Getting temperature for {location} in {language}")

        # Create Wttr instance with optional custom base URL
        wttr_kwargs = {}
        if self.base_url:
            import pydantic

            wttr_kwargs["base_url"] = pydantic.AnyHttpUrl(self.base_url)

        # Get weather data
        with pywttr.Wttr(**wttr_kwargs) as wttr:
            weather_data = wttr.weather(location, language=lang)

        # Extract temperature data
        if (
            not weather_data
            or not hasattr(weather_data, "weather")
            or not weather_data.weather
        ):
            raise WeatherError("No weather data available for this location")

        current = weather_data.weather[0]

        # Format response
        result = {
            "operation": "temperature",
            "location": location,
            "timestamp": datetime.now().isoformat(),
            "temperature": {
                "current_c": getattr(current, "avgtemp_c", None),
                "current_f": getattr(current, "avgtemp_f", None),
                "feels_like_c": getattr(current, "feelslike_c", None),
                "feels_like_f": getattr(current, "feelslike_f", None),
                "max_c": getattr(current, "maxtemp_c", None),
                "max_f": getattr(current, "maxtemp_f", None),
                "min_c": getattr(current, "mintemp_c", None),
                "min_f": getattr(current, "mintemp_f", None),
            },
            "metadata": {
                "language": language,
                "source": "wttr.in",
            },
        }

        log_info(f"Retrieved temperature data for {location}")
        return json.dumps(result, indent=2, ensure_ascii=False)

    except WeatherError:
        raise
    except Exception as e:
        log_error(f"Error getting temperature data: {e}")
        raise WeatherError(f"Failed to get temperature data: {e}")

fetch_weather_text_description

fetch_weather_text_description(location: str, language: str = 'en') -> str

Get weather description for a location.

Parameters:

Name Type Description Default
location str

Location name or coordinates

required
language str

Language code (default: en)

'en'

Returns:

Type Description
str

JSON string containing weather description

Raises:

Type Description
WeatherError

If weather data retrieval fails

Source code in src/enhancedtoolkits/weather.py
def fetch_weather_text_description(self, location: str, language: str = "en") -> str:
    """
    Get weather description for a location.

    Args:
        location: Location name or coordinates
        language: Language code (default: en)

    Returns:
        JSON string containing weather description

    Raises:
        WeatherError: If weather data retrieval fails
    """
    try:
        # Validate inputs
        self._validate_location(location)
        lang = self._validate_language(language)

        log_debug(f"Getting weather description for {location} in {language}")

        # Create Wttr instance with optional custom base URL
        wttr_kwargs = {}
        if self.base_url:
            import pydantic

            wttr_kwargs["base_url"] = pydantic.AnyHttpUrl(self.base_url)

        # Get weather data
        with pywttr.Wttr(**wttr_kwargs) as wttr:
            weather_data = wttr.weather(location, language=lang)

        # Extract weather description
        if (
            not weather_data
            or not hasattr(weather_data, "weather")
            or not weather_data.weather
        ):
            raise WeatherError("No weather data available for this location")

        current = weather_data.weather[0]
        weather_desc = self._get_weather_desc(current)

        # Format response
        result = {
            "operation": "weather_description",
            "location": location,
            "timestamp": datetime.now().isoformat(),
            "description": weather_desc,
            "metadata": {
                "language": language,
                "source": "wttr.in",
            },
        }

        log_info(f"Retrieved weather description for {location}")
        return json.dumps(result, indent=2, ensure_ascii=False)

    except WeatherError:
        raise
    except Exception as e:
        log_error(f"Error getting weather description: {e}")
        raise WeatherError(f"Failed to get weather description: {e}")

get_llm_usage_instructions staticmethod

get_llm_usage_instructions() -> str

Returns detailed instructions for LLMs on how to use each tool in EnhancedWeatherTools. Each instruction includes the method name, description, parameters, types, and example values.

Source code in src/enhancedtoolkits/weather.py
    @staticmethod
    def get_llm_usage_instructions() -> str:
        """
        Returns detailed instructions for LLMs on how to use each tool in EnhancedWeatherTools.
        Each instruction includes the method name, description, parameters, types, and example values.
        """
        instructions = """
<weather_tools_instructions>
*** Weather Tools Instructions ***

By leveraging the following set of tools, you can retrieve current weather conditions, forecasts, temperature data, and weather descriptions for locations worldwide. These tools provide accurate, real-time weather information in multiple languages. Here are the detailed instructions for using each tool:

- Use fetch_current_weather_conditions to retrieve current weather conditions for a location.
   Parameters:
      - location (str): Location name or coordinates, e.g., "New York", "London", "48.8566,2.3522"
      - language (str, optional): Language code (default: "en"), e.g., "fr", "es", "de"

- Use fetch_weather_forecast to retrieve a multi-day weather forecast for a location.
   Parameters:
      - location (str): Location name or coordinates, e.g., "Tokyo", "Sydney", "35.6762,139.6503"
      - days (int, optional): Number of days for forecast (default: 3, range: 1-7)
      - language (str, optional): Language code (default: "en"), e.g., "it", "ru", "zh_cn"

- Use fetch_temperature_data to retrieve temperature data for a location.
   Parameters:
      - location (str): Location name or coordinates, e.g., "Berlin", "Cairo", "52.5200,13.4050"
      - language (str, optional): Language code (default: "en"), e.g., "de", "ar", "fr"

- Use fetch_weather_text_description to retrieve a textual description of the weather for a location.
   Parameters:
      - location (str): Location name or coordinates, e.g., "Rio de Janeiro", "Mumbai", "-22.9068,-43.1729"
      - language (str, optional): Language code (default: "en"), e.g., "pt_br", "hi", "es"

Notes:
- The language parameter is always optional and defaults to English ("en").
- Supported language codes include: en, af, am, ar, be, bn, ca, da, de, el, es, et, fa, fr, gl, hi, hu, ia, id, it, lt, mg, nb, nl, oc, pl, pt_br, ro, ru, ta, th, tr, uk, vi, zh_cn, zh_tw.
- Location can be specified as a city name, address, or latitude,longitude coordinates.
- Temperature values are provided in both Celsius and Fahrenheit where available.
- Weather data is sourced from wttr.in, which aggregates data from various weather services.
</weather_tools_instructions>
"""
        return instructions