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
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:
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.python api_integration_tools_demo.py
get_weather returned for the raw line.POST, auth headers, retries: same @tool shell, different requests call. Tools docs.