Building a Smart Weather Agent with LangChain V1 and Google Gemini
Learn how to create an AI agent that can check the weather for any city in the world –
Introduction
In this tutorial, you’ll learn how to create an AI agent that can check the weather for any city in the world – and here’s the cool part: if you don’t specify a city, it automatically detects where you are based on the tools we will add to the agent and gives you your local weather. No manual input needed!
What is an AI Agent? Think of a regular chatbot as someone who only talks. An AI agent is like giving that person hands – they can actually perform actions! They can call functions, use tools, and interact with the real world through APIs. In our case, the agent can fetch your location and get weather data.
What You’ll Build
By the end of this tutorial, you’ll have a weather assistant that:
Accepts natural language queries about weather
Automatically detects your location when you don’t specify a city
Fetches real-time weather data from OpenWeather API
Presents weather information in an easy-to-read format
Knows which temperature unit (Celsius or Fahrenheit) to use based on the location
Let’s get started!
Prerequisites
Before we begin, make sure you have:
Python installed (version 3.8 or higher)
Basic knowledge of Python (variables, functions, importing modules)
A Google API key for Gemini (free to get at Google AI Studio)
An OpenWeather API key (free to get at OpenWeather)
Step 1: Installing Dependencies
Create a file called requirements.txt and add these lines:
langchain==1.0.3
langchain-google-genai==3.0.1
python-dotenv==1.2.1
requests==2.31.0What are these packages?
langchain: The main library that makes agents worklangchain-google-genai: Connects LangChain to Google’s Gemini AIpython-dotenv: Helps us keep our API keys securerequests: Makes HTTP requests to weather APIs
Install everything with:
pip install -r requirements.txt
This might take a minute. Time for a quick stretch! 🏃
Step 2: Setting Up Your API Keys
Create a file called .env in your project folder and add:
GOOGLE_API_KEY=your_google_api_key_here
OPENWEATHER_API_KEY=your_openweather_api_key_here
Replace the placeholder values with your actual API keys.
Why do we use a .env file? This keeps your secret API keys safe. Never share this file or put it on GitHub!
Note:
Get your Google API key here for free: https://aistudio.google.com/app/api-keys
Get your OpenWeather API key here for free: https://openweathermap.org/api (sign up for the free tier)
Step 3: Understanding the Code
Now let’s create our weather_agent.py file. I’ll explain each section step by step so you understand exactly what’s happening.
Part 1: Importing Libraries
import requests
from langchain_google_genai import ChatGoogleGenerativeAI
from dotenv import load_dotenv
from langchain.agents import create_agent
import os
What’s happening here?
requests: Lets us make HTTP requests to weather APIsChatGoogleGenerativeAI: Connects to Google’s Gemini AIload_dotenv: Loads our API keys from the .env filecreate_agent: The magic function that creates our AI agentos: Helps us read environment variables
Part 2: Loading Environment Variables
load_dotenv()What’s happening here?
This single line reads your .env file and loads all your API keys so we can use them in the program. Simple but essential!
Part 3: Creating the Weather Tool
def get_weather(city: str):
"""Get weather for a given city.
Return the temperature_fahrenheit value in Fahrenheit label for locations such as US, Liberia, Burma"""
api_key = os.getenv("OPENWEATHER_API_KEY")
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q":city,
"appid":api_key,
'units': 'metric'
}
response = requests.get(base_url, params=params)
data = response.json()
temperature_celsius = data['main']['temp']
temperature_fahrenheit = temperature_celsius * 9/5 + 32
return data, {'temperature_fahrenheit': temperature_fahrenheit}
What’s happening here?
This is one of the “tools” our agent can use. Think of it like giving your AI a weather app on their phone!
The docstring (text in triple quotes) is crucial – it tells the AI what this tool does and when to use it
We get the API key from our environment variables
We make a request to OpenWeather API with the city name
We get the response in JSON format
We calculate both Celsius and Fahrenheit temperatures (OpenWeather gives us Celsius, but some locations prefer Fahrenheit)
We return both the complete weather data and the Fahrenheit temperature
Important: The docstring isn’t just a comment – the AI agent actually reads it to understand when and how to use this tool!


