Skip to main content

🦁 Brave Search Python Client supporting Web, Image, News and Video search.

Project description

🦁 Brave Search Python Client

License PyPI - Python Version CI Read the Docs Quality Gate Security Maintainability Technical Debt Code Smells CodeQL Dependabot Renovate enabled Coverage Ruff MyPy GitHub - Version GitHub - Commits PyPI - Version PyPI - Status Docker - Version Docker - Size Copier Open in Dev Containers Open in GitHub Codespaces

[!TIP] 📚 Online documentation - 📖 PDF Manual

[!NOTE] 🧠 This project was scaffolded using the template oe-python-template with copier.


Brave Search Python Client supporting Web, Image, News and Video search.

Use Cases:

  1. Integrate into your Python code to help users find what they're looking for.
  2. Add to your AI applications to give LLMs access to current web information.
  3. Use the built-in CLI in shell scripts to get search results in JSON format.

Overview

Adding Brave Search Python Client to your project as a dependency is easy.

uv add brave-search-python-client             # add dependency to your project

If you don't have uv installed follow these instructions. If you still prefer pip over the modern and fast package manager uv, you can install the library like this:

pip install brave-search-python-client        # add dependency to your project

Obtain your Brave Search API key by signing up here - the free tier includes 2,000 requests per month. For guidance on how to integrate the Brave Search Python client into your code base check out the examples below and explore the reference documentation. If you just want to try out the client without having to write code you can use the integrated CLI:

export BRAVE_SEARCH_API_KEY=YOUR_API_KEY         # replace YOUR_API_KEY
uvx brave-search-python-client web "hello world" # search for hello world

All advanced search options of Brave Search are supported by the client and in the CLI:

# Find all German content about AI added in the last 24 hours
uvx brave-search-python-client web --country=DE --search-lang=de --units=metric --freshness=pd ai

The CLI provides extensive help:

uvx brave-search-python-client --help            # all CLI commands
uvx brave-search-python-client web --help        # all options for web search
uvx brave-search-python-client images --help     # all options image search
uvx brave-search-python-client videos --help     # all options video search
uvx brave-search-python-client news --help       # all options news search

CLI

Operational Excellence

This project is designed with operational excellence in mind, using modern Python tooling and practices. It includes:

  1. Various examples demonstrating usage: a. Simple Python script b. Streamlit web application deployed on Streamlit Community Cloud c. Jupyter and Marimo notebook
  2. Complete reference documentation on Read the Docs
  3. Transparent test coverage including unit and E2E tests (reported on Codecov)
  4. Matrix tested with multiple python versions to ensure compatibility (powered by Nox)
  5. Compliant with modern linting and formatting standards (powered by Ruff)
  6. Up-to-date dependencies (monitored by Renovate and Dependabot)
  7. A-grade code quality in security, maintainability, and reliability with low technical debt and codesmell (verified by SonarQube)
  8. Additional code security checks using CodeQL
  9. Security Policy
  10. License compliant with the Open Source Initiative (OSI)
  11. 1-liner for installation and execution of command line interface (CLI) via uv(x) or Docker
  12. Setup for developing inside a devcontainer included (supports VSCode and GitHub Codespaces)

Usage Examples

Streamlit App

Watch it

Try it out! - Show the code

Minimal Python Script:

"""
Example script demonstrating the usage of the Brave Search Python Client.

For web, image, video and news search.
"""

import asyncio
import os

from dotenv import load_dotenv
from rich.console import Console

from brave_search_python_client import (
    BraveSearch,
    CountryCode,
    ImagesSearchRequest,
    LanguageCode,
    NewsSearchRequest,
    VideosSearchRequest,
    WebSearchRequest,
)

# Load .env file and get Brave Search API key from environment
load_dotenv()
api_key = os.getenv("BRAVE_SEARCH_API_KEY")
if not api_key:
    msg = "BRAVE_SEARCH_API_KEY not found in environment"
    raise ValueError(msg)


async def search() -> None:
    """Run various searches using the Brave Search Python Client (see https://brave-search-python-client.readthedocs.io/en/latest/lib_reference.html)."""
    # Initialize the Brave Search Python client, using the API key from the environment
    bs = BraveSearch()

    # Perform a web search
    response = await bs.web(WebSearchRequest(q="jupyter"))

    # Print results as JSON

    # Iterate over web hits and render links in markdown
    for _result in response.web.results if response.web else []:
        pass

    # Advanced search with parameters
    response = await bs.web(
        WebSearchRequest(
            q="python programming",
            country=CountryCode.DE,
            search_lang=LanguageCode.DE,
        ),
    )
    for _result in response.web.results if response.web else []:
        pass

    # Search and render images
    response = await bs.images(ImagesSearchRequest(q="cute cats"))
    for _image in response.results or []:
        pass

    # Search and render videos
    response = await bs.videos(VideosSearchRequest(q="singularity is close"))
    for _video in response.results or []:
        pass

    # Search and render news
    response = await bs.news(NewsSearchRequest(q="AI"))
    for _item in response.results or []:
        pass


# Run the async search function
# Alternatively use await search() from an async function
asyncio.run(search())

Show script code -

Read the library reference documentation for an explanation of available classes and methods.

Jupyter Notebook

Jupyter Notebook

Show notebook code

Command Line Interface (CLI)

Run with uvx

Add Brave Search API key to the environment

export BRAVE_SEARCH_API_KEY=YOUR_API_KEY

Show available commands:

uvx brave-search-python-client --help

Search the web for "hello world":

uvx brave-search-python-client web "hello world"

Show options for web search

uvx brave-search-python-client web --help

Search images:

uvx brave-search-python-client images "hello world"

Show options for image search

uvx brave-search-python-client images --help

Search videos:

uvx brave-search-python-client videos "hello world"

Show options for videos search

uvx brave-search-python-client videos --help

Search news:

uvx brave-search-python-client news "hello world"

Show options for news search

uvx brave-search-python-client news --help

Read the CLI reference documentation for an explanation of all commands and options.

Run with Docker

Note: Replace YOUR_BRAVE_SEARCH_API_KEY with your API key in the following examples.

Show available commands:

docker run helmuthva/brave-search-python-client --help

Search the web:

docker run --env BRAVE_SEARCH_API_KEY=YOUR_BRAVE_SEARCH_API_KEY helmuthva/brave-search-python-client web "hello world"

Show options for web search

docker run helmuthva/brave-search-python-client web --help

Search images:

docker run --env BRAVE_SEARCH_API_KEY=YOUR_BRAVE_SEARCH_API_KEY helmuthva/brave-search-python-client images "hello world"

Show options for image search

docker run helmuthva/brave-search-python-client images --help

Search videos:

docker run --env BRAVE_SEARCH_API_KEY=YOUR_BRAVE_SEARCH_API_KEY helmuthva/brave-search-python-client videos "hello world"

Show options for video search

docker run helmuthva/brave-search-python-client videos --help

Search news:

docker run --env BRAVE_SEARCH_API_KEY=YOUR_BRAVE_SEARCH_API_KEY helmuthva/brave-search-python-client news "hello world"

Show options for news search

docker run helmuthva/brave-search-python-client news --help

Or use docker compose

File .env is passed through

docker compose up
docker compose run brave-search-python-client --help

Further Reading

  • Inspect our security policy with detailed documentation of checks, tools and principles.
  • Check out the CLI reference with detailed documentation of all CLI commands and options.
  • Check out the library reference with detailed documentation of public classes and functions.
  • Check out the API reference with detailed documentation of all API operations and parameters.
  • Our release notes provide a complete log of recent improvements and changes.
  • In case you want to help us improve 🦁 Brave Search Python Client: The contribution guidelines explain how to setup your development environment and create pull requests.
  • We gratefully acknowledge the open source projects that this project builds upon. Thank you to all these wonderful contributors!

Star History

Star History Chart

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

brave_search_python_client-0.4.27.tar.gz (124.4 kB view details)

Uploaded Source

Built Distribution

brave_search_python_client-0.4.27-py3-none-any.whl (128.8 kB view details)

Uploaded Python 3

File details

Details for the file brave_search_python_client-0.4.27.tar.gz.

File metadata

File hashes

Hashes for brave_search_python_client-0.4.27.tar.gz
Algorithm Hash digest
SHA256 3b8803dd8d4bac8110de2c5ba3014610e6bb36da7ea19ad9ce62b5f1deb38d5a
MD5 765006607778fe5db398da0a77cd3ee1
BLAKE2b-256 1d56bbd47494ebf93742a4ccb40d758a93d0cc1bc0a9513f9106ac9b6f0fa7a2

See more details on using hashes here.

File details

Details for the file brave_search_python_client-0.4.27-py3-none-any.whl.

File metadata

File hashes

Hashes for brave_search_python_client-0.4.27-py3-none-any.whl
Algorithm Hash digest
SHA256 5b7b7a93f46a825517f81b42dc6c0c6ddcabee4a0fe44ce92b9ceae6a37699ec
MD5 3fcd6cbc57a555e7bef0841e394cec2f
BLAKE2b-256 98d3ae16180d52456d8fedcb288e4e4927dd806ce0a43d8ec9b9388ea6c6b072

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page