Using the LangChain Requests Utility for HTTP Operations

Posted: Feb 21, 2025.

The LangChain Requests utility provides a wrapper around the Python requests library, offering both synchronous and asynchronous HTTP operations with built-in authentication handling.

What is the Requests Utility?

The Requests utility is a class that wraps HTTP operations and provides two key benefits:

  1. Simple authentication handling by maintaining headers and auth credentials
  2. Both synchronous and asynchronous HTTP methods using the same interface

This makes it particularly useful when building applications that need to interact with APIs, especially when working with LangChain agents and tools.

Reference

MethodDescription
get(url, **kwargs)Performs a synchronous GET request
post(url, data, **kwargs)Performs a synchronous POST request with data
put(url, data, **kwargs)Performs a synchronous PUT request with data
delete(url, **kwargs)Performs a synchronous DELETE request
patch(url, data, **kwargs)Performs a synchronous PATCH request with data
aget(url, **kwargs)Performs an asynchronous GET request
apost(url, data, **kwargs)Performs an asynchronous POST request with data
aput(url, data, **kwargs)Performs an asynchronous PUT request with data
adelete(url, **kwargs)Performs an asynchronous DELETE request
apatch(url, data, **kwargs)Performs an asynchronous PATCH request with data

How to Use the Requests Utility

Basic Usage

Here's how to create a basic Requests instance:

from langchain_community.utilities import Requests

requests = Requests()
response = requests.get("https://api.example.com/data")

Using Authentication

You can provide authentication headers when initializing:

headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}
requests = Requests(headers=headers)

Making POST Requests with Data

data = {
    "name": "John Doe",
    "email": "john@example.com"
}
response = requests.post("https://api.example.com/users", data=data)

Async Operations

The Requests utility supports async operations for all HTTP methods:

import asyncio

async def fetch_data():
    requests = Requests()
    async for response in requests.aget("https://api.example.com/data"):
        data = await response.json()
        return data

# Run the async function
data = asyncio.run(fetch_data())

Integration with LangChain Tools

The Requests utility is commonly used when creating custom tools for LangChain agents:

from langchain_community.agent_toolkits import NLAToolkit
from langchain_openai import OpenAI

# Create a Requests instance with API key
requests = Requests(headers={"x-api-key": "YOUR_API_KEY"})

# Use it with a toolkit
llm = OpenAI(temperature=0)
toolkit = NLAToolkit.from_llm_and_url(
    llm,
    "https://api.example.com/openapi.yaml",
    requests=requests,
    max_text_length=1800
)

Handling Different Response Types

When working with responses, you can handle different content types:

# JSON response
response = requests.get("https://api.example.com/json")
json_data = response.json()

# Text response
response = requests.get("https://api.example.com/text")
text_data = response.text

# Binary response
response = requests.get("https://api.example.com/binary")
binary_data = response.content

The Requests utility provides a clean and consistent interface for making HTTP requests in your LangChain applications, with built-in support for both synchronous and asynchronous operations. This makes it particularly valuable when building agents that need to interact with external APIs.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs