Skip to main content

langchain-oxylabs

LangChain integration for the Oxylabs Web API and the Oxylabs Web Scraper API. Gives LangChain agents live web search and page reading, and bulk-scrapes pages into LangChain Document objects for retrieval-augmented generation.

PyPI version Python versions Downloads License

YouTube

Why use this

A language model's knowledge is fixed at training time and it cannot browse the web on its own. This package closes that gap in two ways:

  • As an agent tool — the model decides when to search and for what, including which geographic location to search from, and gets back live results.
  • As a document loader — you bulk-scrape URLs or search queries ahead of time, then chunk, embed, and retrieve them in a RAG pipeline.

Requirements

  • Python 3.10 or newer
  • Works with both langchain-core 0.3.x and LangChain v1 (langchain-core 1.x)

Installation

pip install -U langchain-oxylabs

Which Oxylabs product

The package covers two Oxylabs products. They use different credentials, so use the classes for the product you have.

Product Credentials Classes
Web API One API key: OXYLABS_WEB_API_KEY OxylabsWebSearch, OxylabsWebScrape, OxylabsWebLoader, OxylabsWebAPIWrapper
Web Scraper API Username and password: OXYLABS_USERNAME, OXYLABS_PASSWORD OxylabsSearchRun, OxylabsSearchResults, OxylabsLoader, OxylabsSearchAPIWrapper

Web API

Credentials

Create a Web API instance in the Oxylabs dashboard and copy its API key. Keys from other Oxylabs products don't work with the Web API.

export OXYLABS_WEB_API_KEY="your-web-api-key"

The key can also be passed directly:

from langchain_oxylabs import OxylabsWebAPIWrapper, OxylabsWebSearch

tool = OxylabsWebSearch(
    wrapper=OxylabsWebAPIWrapper(oxylabs_web_api_key="your-web-api-key")
)

What to use when

Class Returns Use it for
OxylabsWebSearch JSON string: titles, URLs, snippets Letting an agent search the live web.
OxylabsWebScrape Page as Markdown (or HTML), or JSON fields Letting an agent read a URL, including JavaScript-heavy and bot-protected pages.
OxylabsWebLoader Document objects Loading a list of URLs for RAG indexing.
OxylabsWebAPIWrapper Raw API responses Calling the API from your own code, or configuring timeouts and retries.

Search and scrape tools

from langchain_oxylabs import OxylabsWebScrape, OxylabsWebSearch

search = OxylabsWebSearch()
print(search.invoke({"query": "best hiking boots", "max_results": 5, "location": "DE"}))
# {"results": [{"title": "...", "url": "https://...", "outline": ["..."]}, ...],
#  "related_questions": ["..."]}

scrape = OxylabsWebScrape()
print(scrape.invoke({"url": "https://sandbox.oxylabs.io/products/1"}))

location is a two-letter country code (DE, US, LT). Search results are ranked as someone in that country would see them, and pages are fetched from there.

Scrape options:

Argument Default Purpose
output markdown markdown or html
location unset Country to fetch the page from
device desktop desktop or mobile
run_js False Render the page's JavaScript. Slower; use it when a page comes back nearly empty.
json_prompt unset Fields to extract, in plain words. Returns them as JSON instead of the page.
scrape.invoke(
    {
        "url": "https://sandbox.oxylabs.io/products/1",
        "json_prompt": "the game title, price and platforms",
    }
)
# '{"game_title": "The Legend of Zelda: Ocarina of Time", "price": "91,99 €", ...}'

Pages longer than 50,000 characters are truncated so one page doesn't fill the model's context. Change it with OxylabsWebScrape(max_content_chars=...), or pass None to turn it off.

API errors (a bad key, a spent quota, an invalid argument) are returned to the agent as the tool's output rather than raised, so the agent can correct itself. Calling OxylabsWebAPIWrapper directly raises OxylabsWebAPIError instead.

Both tools support ainvoke for async use.

Using with an agent

from langchain.agents import create_agent
from langchain_oxylabs import OxylabsWebScrape, OxylabsWebSearch

agent = create_agent(
    model="anthropic:claude-sonnet-5",
    tools=[OxylabsWebSearch(), OxylabsWebScrape()],
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What do hiking boots cost in Germany?"}]}
)
print(result["messages"][-1].content)

The model searches, then reads the pages worth reading, and picks location from the question.

Document loader

from langchain_oxylabs import OxylabsWebLoader

loader = OxylabsWebLoader(
    urls=[
        "https://sandbox.oxylabs.io/products/1",
        "https://sandbox.oxylabs.io/products/2",
    ],
    location="DE",
)

for document in loader.lazy_load():
    print(document.metadata)  # source, final_url, status_code, request_id
    print(document.page_content[:250])

OxylabsWebLoader takes the same output, location, device and run_js options as the scrape tool. aload() and alazy_load() are available for async code.

Configuring the wrapper

Option Default Purpose
request_timeout 200 Seconds to wait for one HTTP response
max_retries 2 Extra attempts after a 429, 5xx or connection error
max_wait 600 Seconds to wait for a slow scrape
poll_interval 5 Seconds between checks on a slow scrape

A scrape that takes longer than the API's synchronous limit (often with run_js) is queued automatically and checked every poll_interval seconds until it finishes or max_wait runs out.

Web Scraper API

Credentials

Sign up for a free trial or purchase the product in the Oxylabs dashboard to create your API user credentials, then set:

export OXYLABS_USERNAME="your-oxylabs-username"
export OXYLABS_PASSWORD="your-oxylabs-password"

Credentials can also be passed directly:

OxylabsSearchAPIWrapper(
    oxylabs_username="your-oxylabs-username",
    oxylabs_password="your-oxylabs-password",
)

What to use when

Class Returns Use it for
OxylabsSearchRun Formatted plain text Giving an agent readable search results. The flattened text costs fewer tokens than raw JSON and models parse it reliably.
OxylabsSearchResults JSON string When your code, not the model, consumes the results.
OxylabsLoader Document objects Bulk scraping for RAG indexing.
OxylabsSearchAPIWrapper — Shared client and configuration for both search tools.

Search tools

import json
from langchain_oxylabs import (
    OxylabsSearchAPIWrapper,
    OxylabsSearchResults,
    OxylabsSearchRun,
)

oxylabs_wrapper = OxylabsSearchAPIWrapper()

# Formatted text, for a model to read
run_tool = OxylabsSearchRun(wrapper=oxylabs_wrapper)
print(run_tool.invoke({"query": "Visit restaurants in Vilnius."}))

# JSON, for your own code to consume
results_tool = OxylabsSearchResults(wrapper=oxylabs_wrapper)
for result in json.loads(results_tool.invoke({"query": "Visit restaurants in Paris."})):
    for key, value in result.items():
        print(f"{key}: {value}")

Both tools also support ainvoke for async use.

Note: the search tools query Google (google_search). Other Oxylabs sources such as amazon_search are available through OxylabsLoader.

Geo-targeted results

geo_location scopes results to a location — the main reason to use Oxylabs for search data. Supply it per call, or configure a default on the wrapper:

# Per call. An agent can set this itself from the user's question.
run_tool.invoke({"query": "best coffee", "geo_location": "Vilnius,Lithuania"})

# Or as the default for every search through this wrapper
oxylabs_wrapper = OxylabsSearchAPIWrapper(
    params={"geo_location": "Vilnius,Lithuania"}
)

A per-call value takes precedence over the wrapper default. If neither is set, Oxylabs applies its own default.

Configuring the wrapper

OxylabsSearchAPIWrapper(params={...}) accepts Oxylabs Web Scraper API parameters. Frequently useful ones:

Parameter Default Purpose
geo_location unset Location to search from, e.g. Vilnius,Lithuania
domain com Google domain, e.g. lt for google.lt
pages 1 Number of result pages to retrieve
limit 5 Results per page
user_agent_type desktop desktop or mobile
locale unset Interface language

The wrapper itself also accepts:

Option Default Purpose
result_categories all Restrict which result sections are returned (see below)
include_binary_image_data False Keep base64 image data instead of redacting it. Redaction keeps token counts down.
parsing_recursion_depth 5 How deep to walk nested results when formatting text

Valid result_categories values: knowledge_graph, combined_search_result, product_information, local_information, search_information. Restricting them trims irrelevant sections and reduces tokens:

# Only local results -- useful for "restaurants near X" style questions
wrapper = OxylabsSearchAPIWrapper(
    params={"result_categories": ["local_information"]}
)

Using with an agent

from langchain.agents import create_agent
from langchain_oxylabs import OxylabsSearchAPIWrapper, OxylabsSearchRun

tool = OxylabsSearchRun(wrapper=OxylabsSearchAPIWrapper())
agent = create_agent(model="anthropic:claude-sonnet-5", tools=[tool])

result = agent.invoke(
    {"messages": [{"role": "user", "content": "Find good coffee in Vilnius"}]}
)
print(result["messages"][-1].content)

The model chooses when to search and which geo_location to use, based on the question it is answering.

Document loader

OxylabsLoader scrapes into LangChain Document objects for RAG pipelines. Pass either urls or queries, plus any Oxylabs API parameters.

from langchain_oxylabs import OxylabsLoader

# Scrape specific URLs as markdown
loader = OxylabsLoader(
    urls=[
        "https://sandbox.oxylabs.io/products/1",
        "https://sandbox.oxylabs.io/products/2",
    ],
    params={"markdown": True},
)

for document in loader.lazy_load():
    print(document.metadata)          # url (or query) and created_at
    print(document.page_content[:250])
# Or scrape parsed search results for a list of queries
loader = OxylabsLoader(
    queries=["gaming headset", "gaming chair"],
    params={
        "source": "amazon_search",
        "parse": True,
        "geo_location": "DE",
        "currency": "EUR",
        "pages": 3,
    },
)

documents = loader.load()

Each document carries url (or query) and created_at in its metadata, so retrieved facts can be traced back to their source and freshness.

lazy_load() issues one request per URL or query and yields documents as they arrive; prefer it to load() for large batches.

Troubleshooting

Web API

Oxylabs Web API key is missing — OXYLABS_WEB_API_KEY is unset. Set it, or pass oxylabs_web_api_key to OxylabsWebAPIWrapper.

Authentication failed (401) — the key is wrong, or it belongs to another Oxylabs product. Copy the key of a Web API instance from the dashboard.

String should match pattern '^[A-Za-z]{2}$' — location must be a two-letter country code such as DE, not a place name.

A page comes back nearly empty — it renders client-side. Call the scrape again with run_js=True.

... with state "faulted" — the API could not fetch the page after its own retries. Faulted requests are not charged. If it keeps happening, quote the request_id from the message to Oxylabs support.

Web Scraper API

RuntimeError: Please set up environment variables — OXYLABS_USERNAME and OXYLABS_PASSWORD are unset, or contain a stray newline or space from being copied. Pass them explicitly to OxylabsSearchAPIWrapper to rule the environment out.

NotImplementedError: Source ... is not supported — the search tools only support google_search. Use OxylabsLoader for other Oxylabs sources.

Empty results / "No good search result found" — the API occasionally returns a response the wrapper cannot parse, and this is currently reported as an empty result rather than an error. Retrying usually succeeds.

Slow calls — scraping is inherently slower than a normal HTTP request; single calls can take tens of seconds. request_timeout (default 165s) is configurable.

Development

poetry install --with test,lint,typing

make test               # unit tests, no network access
make lint               # ruff + mypy
make integration_tests  # live API calls, requires credentials for both products

License

MIT — see LICENSE.

Metadata

Release files for langchain-oxylabs 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-oxylabs 0.4.0
File Size Uploaded Provenance Actions
langchain_oxylabs-0.4.0.tar.gz 28.1 kB With provenance Details for langchain_oxylabs-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-oxylabs 0.4.0
File Interpreter ABI Platform Actions
langchain_oxylabs-0.4.0-py3-none-any.whl Python 3 none any Details for langchain_oxylabs-0.4.0-py3-none-any.whl

Total release size: 55.2 kB

Release files / langchain_oxylabs-0.4.0.tar.gz

Download URL langchain_oxylabs-0.4.0.tar.gz
Size 28.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e0af6ee9a58cb160274ae1703634411cbd28a53d66335b31b123e02256cf0b0f
BLAKE2b-256 checksum
How to use checksums
9134c43bc4c6dfa9ec6a3fac75390f73367de36de5e94c64324185b39164e1dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

Release files / langchain_oxylabs-0.4.0-py3-none-any.whl

Download URL langchain_oxylabs-0.4.0-py3-none-any.whl
Size 27.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d5931d8a75b4ac7e4d0558aee141af6a3224703d3434d83aa2d0686d107a874b
BLAKE2b-256 checksum
How to use checksums
c01ac78390e2c3ef30aecb285d9f3684e231c600e2df2e002a70dc829c97477d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

Security for langchain-oxylabs 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for langchain_oxylabs-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page