Skip to main content

LangChain WebSearch Tool

A powerful LangChain toolkit for Querit APIs: web search and web page content fetching.

🔍 Features

  • Querit Search API Integration: Powered by Querit Search API
  • Querit Contents API Integration: Fetch full page content by URL, up to 10 URLs per call
  • API Key Management: Secure API key handling with environment variables
  • LangChain Integration: Seamlessly integrates with LangChain agents and chains
  • Structured Results: Returns formatted search results with metadata
  • Async Support: Asynchronous version available
  • Flexible Configuration: Customizable search parameters

🚀 Quick Start

Requirements

Python 3.9 or newer, with langchain>=0.3 and pydantic>=2.0. Older floors were declared through 0.0.2 but did not work: pinning pydantic<2 makes pip resolve langchain-core 0.2.x, whose BaseTool is built on the pydantic v1 compatibility layer, and the tools then fail to construct.

Installation

pip install langchain-querit

Basic Usage

from langchain_websearch import WebSearchTool

# Read the API key from the QUERIT_API_KEY environment variable:
#   export QUERIT_API_KEY="your-querit-api-key"

# Initialize the tool
search_tool = WebSearchTool()

# Perform a search
results = search_tool.invoke("latest Python programming news")
print(results)

Advanced Configuration

from langchain_websearch import WebSearchTool

# Configure with specific parameters
search_tool = WebSearchTool(
    num_results=5  # Number of results to return
)

# Use with custom query
results = search_tool.invoke("machine learning tutorials")
print(results)

📄 Web Contents Tool

WebContentsTool fetches the full text of web pages by URL, using the Querit Contents API. It is a separate tool from WebSearchTool: search finds URLs, contents fetches what is behind them.

Basic Usage

from langchain_websearch import WebContentsTool

# Read the API key from the QUERIT_API_KEY environment variable:
#   export QUERIT_API_KEY="your-querit-api-key"

contents_tool = WebContentsTool()

# Input is a list of 1 to 10 URLs
results = contents_tool.invoke({"urls": ["https://example.com"]})
print(results)

Advanced Configuration

from langchain_websearch import WebContentsTool

contents_tool = WebContentsTool(
    format="markdown",   # "text" | "markdown" | "html"
    crawl_timeout=10,    # per-page crawl timeout in seconds, 1-60
    extras_meta=True,    # also return title / site name / publish time
)

results = contents_tool.invoke({
    "urls": [
        "https://example.com",
        "https://docs.python.org/3/whatsnew/3.13.html",
    ]
})
print(results)

Output marks each URL with its own status, so a page that fails to crawl does not fail the whole call:

1. https://example.com  [success]
   Title: Example Domain
   Site: example.com
   Content (113 chars):
This domain is for use in documentation examples without needing permission. Avoid use in operations.

Learn more

2. https://this-domain-does-not-exist-xyz123.com  [FAILED]
   No content retrieved.

Using Both Tools in an Agent

from langchain_websearch import WebSearchTool, WebContentsTool

tools = [WebSearchTool(num_results=5), WebContentsTool(extras_meta=True)]
# pass `tools` to your LangChain agent constructor

Structured Access

For programmatic use, the backend returns ContentResult objects instead of a formatted string:

from langchain_websearch import QueritContentsBackend

backend = QueritContentsBackend()
for result in backend.fetch(["https://example.com"], extras_meta=True):
    print(result.status, result.url, len(result.content))
    if result.meta:
        print(result.meta.title, result.meta.site_name)

Error Behavior

Request-level failures raise instead of being returned as text, so callers and tests can tell success from failure:

  • Missing API key, empty urls, or more than 10 URLs → ValueError (no HTTP request sent)
  • Invalid formatpydantic.ValidationError
  • HTTP 401 / 400 / 429 / 5xx → requests.HTTPError
  • A single URL that cannot be crawled → not an exception; that entry gets status="failed"

🧪 Testing

To run tests with your API key:

export QUERIT_API_KEY="your-querit-api-key" && python3 -m pytest tests/

For verbose test output:

export QUERIT_API_KEY="your-querit-api-key" && python3 tests/test_basic.py

⚙️ Configuration

Environment Variables

  • QUERIT_API_KEY: Your Querit Search API key (required)

WebSearchTool Parameters

  • num_results: Number of results to return (default: 10, range: 1-50)
  • region: Search region/language (default: "en-US", currently not used)
  • safe_search: Enable safe search filtering (default: True, currently not used)

WebContentsTool Parameters

  • format: Output format, one of "text", "markdown", "html" (default: "markdown")
  • crawl_timeout: Per-page crawl timeout in seconds (default: 10, range: 1-60)
  • extras_meta: Return title, site name, site icon and publish time (default: False)

📚 Documentation

For full API reference and examples, see the examples directory.

Example Usage

Check examples/basic_usage.py for complete usage examples including LangChain agent integration.

For the Contents tool, examples/contents_usage.py is a runnable smoke test that reads QUERIT_API_KEY from the environment and exits non-zero on failure:

export QUERIT_API_KEY="your-querit-api-key"
python3 examples/contents_usage.py

🔧 Development

Development Setup

# Clone the repository
git clone https://github.com/querit-ai/langchain-querit.git
cd langchain-querit

# Install in development mode
pip install -e ".[dev]"

Running Tests

# Run all tests
export QUERIT_API_KEY="your-querit-api-key" && pytest tests/

# Run with coverage
export QUERIT_API_KEY="your-querit-api-key" && pytest --cov=src tests/

# Run specific test file
export QUERIT_API_KEY="your-querit-api-key" && python3 tests/test_basic.py

See CONTRIBUTING.md for detailed development guidelines.

🤝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for guidelines on how to contribute.

📜 License

MIT License - See LICENSE for details.

🔗 Links

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_querit-0.0.3.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_querit-0.0.3-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file langchain_querit-0.0.3.tar.gz.

File metadata

  • Download URL: langchain_querit-0.0.3.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for langchain_querit-0.0.3.tar.gz
Algorithm Hash digest
SHA256 d7bcc12c73769564ea25c15bec5f4152e74ff5432deb9b7dc32abfa39a403e3e
MD5 f25bc63a36036972e0bcea65547f5cf7
BLAKE2b-256 3b7c1d80135d238362ad2e015115d7f83e26f4bd7fce18270f26811bc1df2057

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_querit-0.0.3.tar.gz:

Publisher: publish.yml on querit-ai/langchain-querit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_querit-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_querit-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a2a563a491170dc2da7e281494f4f2af35f58d79bbe12ad0fec6adc885fa63c3
MD5 09478c9fc9241255c07257ccd9175ec4
BLAKE2b-256 3c9755b616e89ed5b6dcb616ca07e6ca15a32748c46a9403896acdde42263727

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_querit-0.0.3-py3-none-any.whl:

Publisher: publish.yml on querit-ai/langchain-querit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 files

0.0.2

2 files

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