Course navigation
AgentsLesson 9 of 9

API Integration Tools

Search Tool used TavilySearch, a class someone else wrote. Your own APIs usually go in an @tool with requests.get.

Open-Meteo GET

Live weather is not in your HTML notes. Open-Meteo is free, no API key. Geocode the city, read current from the forecast JSON, return a one-line string to the tool message.

Search Tool

TavilySearch
their HTTP
TAVILY_API_KEY

get_weather

@tool you write
requests.get
you parse JSON
The Kolkata weather line in What are Agents? — here it is real Python calling Open-Meteo.

get_weather @tool

One arg: city. Two GETs: geocode URL, then forecast URL with lat/lon.

Put the HTTP in fetch_weather. Bad city name or timeout → return an error string, same idea as the calculator whitelist.

import requests
from langchain.tools import tool

GEOCODE_URL = "https://geocoding-api.open-meteo.com/v1/search"
FORECAST_URL = "https://api.open-meteo.com/v1/forecast"

def fetch_weather(city: str) -> str:
    geo = requests.get(GEOCODE_URL, params={"name": city, "count": 1}, timeout=10)
    geo.raise_for_status()
    results = geo.json().get("results") or []
    if not results:
        return f"No coordinates for '{city}'."
    lat, lon = results[0]["latitude"], results[0]["longitude"]
    forecast = requests.get(
        FORECAST_URL,
        params={"latitude": lat, "longitude": lon, "current": "temperature_2m,relative_humidity_2m"},
        timeout=10,
    )
    forecast.raise_for_status()
    c = forecast.json()["current"]
    return f"{results[0]['name']}: {c['temperature_2m']}°C, humidity {c['relative_humidity_2m']}%"

@tool("get_weather")
def get_weather(city: str) -> str:
    """Get current temperature and humidity for a city."""
    return fetch_weather(city)

When get_weather runs:

user: Weather in Kolkata?
get_weather("Kolkata")
GET geocode → GET forecast
tool: Kolkata: 28.7°C, humidity 89%
ai: About 29°C and humid in Kolkata.
Geocode, then forecast. Your function returns one line; that lands in the tool message.

create_agent

tools=[get_weather]. Nothing else changes from the last few lessons.

from langchain.agents import create_agent

agent = create_agent(model="openai:gpt-4o-mini", tools=[get_weather])

result = agent.invoke({
    "messages": [{"role": "user", "content": "Weather in Kolkata? Use get_weather."}],
})
print(result["messages"][-1].content)

Run the demo

Venv from Project Setup. Install requests, download, then:

pip install requests

api_integration_tools_demo.py

Kolkata, then Berlin

OPENAI_API_KEY same as Tool Calling. Open-Meteo is free; the script needs network access.
api_integration_tools_demo.py
"""api_integration_tools_demo.py"""
# geocode city, then forecast temp + humidity
@tool("get_weather")
python api_integration_tools_demo.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> python api_integration_tools_demo.py
Question: What's the weather in Kolkata today? Use get_weather.
get_weather returned: Kolkata: 28.7°C, humidity 89%
Answer: In Kolkata it is about 29°C with high humidity.
Question: What's the weather in Berlin? Use get_weather.
get_weather returned: Berlin: 31.6°C, humidity 25%
Answer: Berlin is about 32°C with low humidity.
Temp and humidity change with the hour. Check get_weather returned for the raw line.

POST, auth headers, retries: same @tool shell, different requests call. Tools docs.

What's Next

Last lesson in the Agents module.