feat: add Tavily as parallel search provider in web_search tool#2
feat: add Tavily as parallel search provider in web_search tool#2mani2001 wants to merge 2 commits intotysonchamp:mainfrom
Conversation
… search provider in tools/web_search.py
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the web search tool by integrating Tavily as an additional, configurable search provider alongside the existing DuckDuckGo service. This provides users with greater flexibility in choosing their preferred search backend via environment variables or configuration, thereby expanding web data retrieval options without altering the default behavior or requiring changes to existing implementations. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds Tavily as a configurable search provider, which is a great enhancement. The changes across the configuration files and requirements.txt are correct. However, in tools/web_search.py, the new dispatcher logic for selecting the search provider is overly complex, difficult to read, and contains a bug related to handling the TAVILY_API_KEY when the configuration file is not present. I've provided a single, comprehensive suggestion to refactor the perform_web_search function to improve its clarity, fix the bug, and make it more robust by adding a fallback mechanism.
| def perform_web_search(query): | ||
| """Dispatches search to the configured provider (duckduckgo or tavily).""" | ||
| conf = app_config.load_config() | ||
| provider = (conf.get("search", {}).get("provider", "duckduckgo") if conf else os.environ.get("SEARCH_PROVIDER", "duckduckgo")).lower() | ||
| if provider not in ("duckduckgo", "tavily"): | ||
| logging.warning(f'Unknown SEARCH_PROVIDER "{provider}", falling back to duckduckgo') | ||
| provider = "duckduckgo" | ||
| if provider == "tavily": | ||
| tavily_key = conf.get("search", {}).get("tavily_api_key", "") if conf else "" | ||
| if tavily_key: | ||
| os.environ.setdefault("TAVILY_API_KEY", tavily_key) | ||
| return perform_tavily_search(query) | ||
| return perform_duckduckgo_search(query) |
There was a problem hiding this comment.
The current implementation for selecting the search provider and handling the API key in perform_web_search is complex and contains a bug.
- The logic is packed into dense one-liners, which harms readability.
- There's a bug where if the config file is missing, the code doesn't fall back to using the
TAVILY_API_KEYenvironment variable. - If Tavily is the selected provider but the API key is missing, the search will fail instead of gracefully falling back to another provider.
I suggest refactoring the entire function to be more explicit and robust. This change improves readability, fixes the bug, and adds a safe fallback to DuckDuckGo if the Tavily key is missing.
def perform_web_search(query):
"""Dispatches search to the configured provider (duckduckgo or tavily)."""
conf = app_config.load_config()
if conf:
search_conf = conf.get("search", {})
provider = search_conf.get("provider", "duckduckgo")
tavily_key = search_conf.get("tavily_api_key")
else:
provider = os.getenv("SEARCH_PROVIDER", "duckduckgo")
tavily_key = os.getenv("TAVILY_API_KEY")
provider = provider.lower()
if provider not in ("duckduckgo", "tavily"):
logging.warning(f'Unknown SEARCH_PROVIDER "{provider}", falling back to duckduckgo')
provider = "duckduckgo"
if provider == "tavily":
if tavily_key:
os.environ.setdefault("TAVILY_API_KEY", tavily_key)
return perform_tavily_search(query)
logging.warning("SEARCH_PROVIDER is 'tavily' but TAVILY_API_KEY is not set. Falling back to duckduckgo.")
return perform_duckduckgo_search(query)
Summary
tools/web_search.pySEARCH_PROVIDERenv var (duckduckgoortavily, defaults toduckduckgo)Files changed
tools/web_search.py— Extracted DuckDuckGo logic intoperform_duckduckgo_search(), addedperform_tavily_search()usingTavilyClient, added provider dispatcher inperform_web_search()requirements.txt— Addedtavily-pythondependencyenv.example— AddedSEARCH_PROVIDERandTAVILY_API_KEYenv varsconfig/config.yaml.example— Addedsearch.providerandsearch.tavily_api_keyconfig fieldsDependency changes
tavily-pythontorequirements.txtEnvironment variable changes
SEARCH_PROVIDER(optional, values:duckduckgo|tavily, default:duckduckgo)TAVILY_API_KEY(required whenSEARCH_PROVIDER=tavily)Notes for reviewers
SEARCH_PROVIDER=tavilyand provide a validTAVILY_API_KEYtitle,url,content) is mapped to match the existing DuckDuckGo output shape🤖 Generated with Claude Code
Automated Review
requirements.txtaddstavily-python,config/__init__.pywires upSEARCH_PROVIDER/TAVILY_API_KEYenv-var overrides into the config object,config/config.yaml.exampledocuments the newsearch:block,env.exampleadds both env vars with a trailing newline, andtools/web_search.pycorrectly renames the old function toperform_duckduckgo_search, addsperform_tavily_search, and introduces a dispatcherperform_web_searchwith provider routing, fallback warning, and graceful error handling. Existing DuckDuckGo behavior is fully preserved. Three minor issues were found but none are blocking.