Repository navigation
Conversation
The docs say BraveSearchTool takes its key as api_key or from BRAVE_API_KEY, but the class had no api_key field: pydantic dropped the argument and __init__ only checked the environment, so BraveSearchTool(api_key=...) raised without the variable and sent the environment's key when it was set. Add an api_key field excluded from serialization, fall back to BRAVE_API_KEY, and send the resolved key in the request header.
Strix Security Review✅ No open security findings. PR summary: The PR adds the previously missing `api_key` parameter to the legacy `BraveSearchTool`: the value is a new excluded model field, `__init__` falls back to `BRAVE_API_KEY` and errors when neither source is available, and `_run` sends the resolved key in `X-Subscription-Token`. It also adds five tests covering the explicit key, the environment fallback, precedence, the missing-key error, and the header actually sent. Review summaryReviewed the legacy Ruled out (5)
Review 1 on this PR · Reviewed by Strix |
Fixes #9762
Summary
The docs for
BraveSearchToolsay the API key can be passed directly via theapi_keyparameter or left toBRAVE_API_KEY, but the exported class had no such field: pydantic silently dropped the argument,__init__only checked the environment, and_runsentos.environ["BRAVE_API_KEY"].BraveSearchTool(api_key="explicit-key")therefore raisedValueError: BRAVE_API_KEY environment variable is requiredwithout the variable, and silently used the environment's key when it was set. The newer Brave tools already support both sources throughBraveSearchToolBase; this brings the legacyBraveSearchToolin line with its docs and with them.Changes
lib/crewai-tools/src/crewai_tools/tools/brave_search_tool/brave_search_tool.pyapi_key: str | Nonefield (exclude=True, so the secret is not serialized), defaulting toNone.__init__falls back toBRAVE_API_KEYwhen no key was passed and raises when neither is available._runsends the resolved key inX-Subscription-Token(environment fallback kept).lib/crewai-tools/tests/tools/brave_search_tool_test.pyTestLegacyBraveSearchToolApiKey: explicit key without the variable, environment fallback, explicit key winning, missing-key error, and the explicit key reaching the request header.Verification
pytest lib/crewai-tools/tests/tools/brave_search_tool_test.py -k TestLegacyBraveSearchToolApiKey->5 passed.brave_search_tool.py(new tests kept): 4 of the 5 fail (noapi_keyfield /ValueErrorwithout the variable); only the missing-key error test passes, as before.66 passed, 1 failed; the failingtest_rate_limit_disabled_when_zeroalso fails on the unmodified base (it exercisesBraveSearchToolBase, not the legacy class).AI assistance
I used an AI coding agent to investigate, implement, and test this change, and reviewed its results. Per CONTRIBUTING.md, please apply the
llm-generatedlabel; external contributors cannot add it with my current permissions.