community[minor]: Improve Brave Search Tool, allow api key in env var (#30364)

- **Description:** 

- Make Brave Search Tool consistent with other tools and allow reading
its api key from `BRAVE_SEARCH_API_KEY` instead of having to pass the
api key manually (no breaking changes)
- Improve Brave Search Tool by storing api key in `SecretStr` instead of
plain `str`.
    - Add unit test for `BraveSearchWrapper`
    - Reflect the changes in the documentation
  - **Issue:** N/A
  - **Dependencies:** N/A
  - **Twitter handle:** ivan_brko
This commit is contained in:
Ivan Brko
2025-03-31 20:48:52 +02:00
committed by GitHub
parent 0c623045b5
commit ecff055096
5 changed files with 101 additions and 9 deletions

View File

@@ -0,0 +1,32 @@
from typing import Any
import pytest
from pydantic import SecretStr
from langchain_community.utilities.brave_search import BraveSearchWrapper
def test_api_key_explicit() -> None:
"""Test that the API key is correctly set when provided explicitly."""
explicit_key = "explicit-api-key"
wrapper = BraveSearchWrapper(api_key=SecretStr(explicit_key), search_kwargs={})
assert wrapper.api_key.get_secret_value() == explicit_key
def test_api_key_from_env(monkeypatch: Any) -> None:
"""Test that the API key is correctly obtained from the environment variable."""
env_key = "env-api-key"
monkeypatch.setenv("BRAVE_SEARCH_API_KEY", env_key)
# Do not pass the api_key explicitly
wrapper = BraveSearchWrapper() # type: ignore[call-arg]
assert wrapper.api_key.get_secret_value() == env_key
def test_api_key_missing(monkeypatch: Any) -> None:
"""Test that instantiation fails when no API key is provided
either explicitly or via environment."""
# Ensure that the environment variable is not set
monkeypatch.delenv("BRAVE_SEARCH_API_KEY", raising=False)
with pytest.raises(ValueError):
# This should raise an error because no api_key is available.
BraveSearchWrapper() # type: ignore[call-arg]