Skip to content

[BUG] BraveSearchTool ignores the documented api_key argument #9762

Done

Description

@Kiko0301

Description

The docs for BraveSearchTool say the API key can be passed directly:

  • docs/edge/en/tools/search-research/bravesearchtool.mdx:45 - "store your key as BRAVE_API_KEY, or pass it directly via the api_key parameter"
  • docs/edge/en/tools/search-research/bravesearchtool.mdx:115 - | api_key | str \| None | None | Brave API key. Falls back to the BRAVE_API_KEY environment variable. |

The exported class (lib/crewai-tools/src/crewai_tools/tools/brave_search_tool/brave_search_tool.py, imported as crewai_tools.BraveSearchTool) has no api_key field, so pydantic silently drops the argument, and its __init__ only checks the environment:

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        if "BRAVE_API_KEY" not in os.environ:
            raise ValueError(
                "BRAVE_API_KEY environment variable is required for BraveSearchTool"
            )

and _run sends os.environ["BRAVE_API_KEY"] in the X-Subscription-Token header.

The newer Brave tools (BraveWebSearchTool, BraveNewsSearchTool, ...) inherit BraveSearchToolBase, whose constructor does accept api_key= and use it over the environment - so the same documented behavior works for them but not for BraveSearchTool itself.

Steps to Reproduce

import os

os.environ.pop("BRAVE_API_KEY", None)

from crewai_tools import BraveSearchTool

tool = BraveSearchTool(api_key="explicit-key")

Raises:

ValueError: BRAVE_API_KEY environment variable is required for BraveSearchTool

With BRAVE_API_KEY set to env-key and BraveSearchTool(api_key="explicit-key"), construction succeeds but the tool has no api_key attribute and the request still sends env-key.

Expected Behavior

BraveSearchTool(api_key=...) should work without the environment variable and should take precedence over it, with BRAVE_API_KEY kept as the fallback - as documented and as BraveSearchToolBase already implements for the other Brave tools.

AI assistance

I used an AI coding agent to investigate, reproduce, and prepare this report, and reviewed its results. Per CONTRIBUTING.md, please apply the llm-generated label; external contributors cannot add it with my current permissions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions