Skip to main content

langchain-contextfetch

This package contains the LangChain integration with ContextFetch, a web search API that returns pages as Markdown.

  • ContextFetchRetriever: a retriever that returns search results as Documents.
  • ContextFetchSearch: a search tool for agents and tool-calling models.

Installation

pip install -U langchain-contextfetch

Requires Python 3.10 or later.

Credentials

Set your API key in the CONTEXTFETCH_API_KEY environment variable, or pass api_key= to either class:

export CONTEXTFETCH_API_KEY="your-api-key"

Without a key, requests use ContextFetch's keyless tier, which has a small daily allowance per IP address. See the API reference for keys and credits.

Retriever

from langchain_contextfetch import ContextFetchRetriever

retriever = ContextFetchRetriever(k=3)
docs = retriever.invoke("pgvector HNSW index parameters")

for doc in docs:
    print(doc.metadata["url"], doc.metadata["title"])

Each Document has:

  • page_content: the page as Markdown (content="text", the default), or only the chunks of the page relevant to the query (content="pruned_context").
  • metadata: id, url, title, score, published_date and author. Fields the API does not return are None.

Options:

Parameter Default Description
k 5 Number of results (1 to 25). Can also be passed per call: retriever.invoke(query, k=10).
content "text" "text" or "pruned_context".
include_domains None Only return results from these domains.
exclude_domains None Never return results from these domains.
start_published_date None Only return pages published on or after this date (YYYY-MM-DD).
api_key CONTEXTFETCH_API_KEY API key.

Search tool

from langchain_contextfetch import ContextFetchSearch

tool = ContextFetchSearch(num_results=3)
tool.invoke({"query": "pgvector HNSW index parameters"})

The model can set query, include_domains, exclude_domains and start_published_date. num_results (default 5, maximum 25), content and api_key are set when the tool is created. The tool's default content is "pruned_context", so each result contributes only its query-relevant chunks to the model's context; use content="text" for whole pages.

The tool uses response_format="content_and_artifact". Called with plain args as above, it returns the content string: each result's title, URL, published date and content. Called with a ToolCall, it returns a ToolMessage with that content and the raw API response as its artifact.

msg = tool.invoke(
    {
        "args": {"query": "pgvector HNSW index parameters"},
        "id": "1",
        "name": tool.name,
        "type": "tool_call",
    }
)
msg.artifact["results"][0]["url"]

API errors (for example HTTP 402 when credits run out) are raised as ToolException. To pass the error message to the model instead, create the tool with handle_tool_error=True.

Use in an agent

from langchain.agents import create_agent

agent = create_agent(model="claude-sonnet-4-6", tools=[tool])
agent.invoke({"messages": [{"role": "user", "content": "What changed in Python 3.14?"}]})

Async

Both classes support ainvoke. The underlying contextfetch SDK is synchronous, so async calls run in a thread executor (LangChain's default for tools and retrievers).

Development

make test               # unit tests, network disabled
make integration_test   # live API tests; skipped unless CONTEXTFETCH_API_KEY is set
make lint

The unit tests include LangChain's standard ToolsUnitTests. The integration tests run the standard ToolsIntegrationTests and RetrieversIntegrationTests suites, which make 10 searches in total.

Metadata

Release files for langchain-contextfetch 0.1.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-contextfetch 0.1.0
File Size Uploaded Provenance
langchain_contextfetch-0.1.0.tar.gz 8.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-contextfetch 0.1.0
File Interpreter ABI Platform
langchain_contextfetch-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.3 kB

Release files / langchain_contextfetch-0.1.0.tar.gz

Download URL langchain_contextfetch-0.1.0.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
114cbe8f4eba55038c296274e8d9a29160fbe328213625925af5e79397cb4bcc
BLAKE2b-256 checksum
How to use checksums
ef036514cba00e88686ec582699c8c00c1ef5dbd50a11a5ce5fc8d2a1bea1971
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / langchain_contextfetch-0.1.0-py3-none-any.whl

Download URL langchain_contextfetch-0.1.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e9c324e7685f5d829b83468f3f6191d80a211a55973f97ca155ec1c683198b5
BLAKE2b-256 checksum
How to use checksums
4f14a2ded302390535df3475b704e4a82651dac197c4e2d318df557e2ecdb651
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for langchain-contextfetch 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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