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 Coverage Ruff GitHub - Version GitHub - Commits PyPI - Version PyPI - Status Docker - Version Docker - Size

The Brave Search Python Client provides Web, Image, News, and Video search capabilities.

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

Installation is as simple as:

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

Highlights

Usage Examples

Streamlit App

Watch it

Try it out! - Show the code

Minimal Python Script:

import asyncio
import json
import os

from dotenv import load_dotenv

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:
    raise Exception("BRAVE_SEARCH_API_KEY not found in environment")


async def search():
    """Run various searches using the Brave Search Python Client (see https://brave-search-python-client.readthedocs.io/en/latest/reference_index.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
    print("# Web search")
    print("## JSON response")
    print(json.dumps(response.model_dump(), indent=2))

    # Iterate over web hits and render links in markdown
    print("## Iterate and render")
    for result in response.web.results if response.web else []:
        print(f"[{result.title}]({result.url})")

    # Advanced search with parameters
    response = await bs.web(
        WebSearchRequest(
            q="python programming",
            country=CountryCode.DE,
            search_lang=LanguageCode.DE,
        )
    )
    print("# Advanced search results")
    for result in response.web.results if response.web else []:
        print(f"[{result.title}]({result.url})")

    # Search and render images
    print("# Images")
    response = await bs.images(ImagesSearchRequest(q="cute cats"))
    for image in response.results if response.results else []:
        print(f"![{image.source}]({image.url})")

    # Search and render videos
    print("# Videos")
    response = await bs.videos(VideosSearchRequest(q="singularity is close"))
    for video in response.results if response.results else []:
        print(f"![{video.title}]({video.url})")

    # Search and render news
    print("# News")
    response = await bs.news(NewsSearchRequest(q="AI"))
    for item in response.results if response.results else []:
        print(f"![{item.title}]({item.url})")


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

Show script code - Read the reference documentation

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

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

Extra: MCP Server

TK

Contributing

Please read our Contributing Guidelines for how to setup your development environment, and guidance for making pull requests.

Resources

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.2.15.tar.gz (37.3 MB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for brave_search_python_client-0.2.15.tar.gz
Algorithm Hash digest
SHA256 eb7e490e9c8564d38f844c9bd273f9da14f04d4bc443cd5a4b3dfd2c9ddb5f4d
MD5 2f0ff570ff6c6a02d264c6ef81b47b70
BLAKE2b-256 c0b998842dc1b4b88b5abe79a7dbc0c34283e1992792ef9c2e7bf4a95faf78c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for brave_search_python_client-0.2.15-py3-none-any.whl
Algorithm Hash digest
SHA256 5e33b8e2c5e24af3391b7aafdce24c4ed2a8d0bc433e6b266b510d0422e5db68
MD5 5d2e3fa2cd20ced52a62a57cef6efad8
BLAKE2b-256 5c5731a54272a6dd3a6a90df37ca553563a52a53923a88821f25d3a4702b11f0

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