feat: add Tavily search provider to legacy web_search skill#3
feat: add Tavily search provider to legacy web_search skill#3mani2001 wants to merge 1 commit intotysonchamp:mainfrom
Conversation
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 introduces Tavily as a configurable search provider for the legacy web_search skill, providing an alternative to the existing DuckDuckGo search. It preserves the original DuckDuckGo functionality while adding a new option controlled by an environment variable. The change includes refactoring the search logic into provider-specific helpers and adding a new dependency. 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 introduces Tavily as a new search provider, which can be configured via an environment variable. The implementation is a good step forward, but there are a few areas for improvement regarding error handling to make the code more robust. Specifically, potential ImportErrors are not handled within the try...except blocks, which could lead to application crashes. I've provided suggestions to move the imports inside the try blocks and to improve exception handling. I also noted an opportunity to make dictionary access safer in the DuckDuckGo search implementation and to make the provider selection logic more explicit.
| from tavily import TavilyClient | ||
| try: | ||
| client = TavilyClient() | ||
| response = client.search(query=query, max_results=5, search_depth="basic") | ||
| results = response.get("results", []) | ||
| if not results: | ||
| return "No results found." | ||
|
|
||
| summary = "" | ||
| for r in results: | ||
| summary += f"- [{r['title']}]({r['url']}): {r.get('content', '')}\n" | ||
| return summary | ||
| except Exception as e: | ||
| return f"Error performing search: {e}" |
There was a problem hiding this comment.
The try...except block does not cover the import statement. If the tavily-python package is not installed, an ImportError will be raised and crash the application instead of being handled gracefully. The import should be moved inside the try block. Additionally, it's good practice to log exceptions before returning an error message to aid in debugging.
| from tavily import TavilyClient | |
| try: | |
| client = TavilyClient() | |
| response = client.search(query=query, max_results=5, search_depth="basic") | |
| results = response.get("results", []) | |
| if not results: | |
| return "No results found." | |
| summary = "" | |
| for r in results: | |
| summary += f"- [{r['title']}]({r['url']}): {r.get('content', '')}\n" | |
| return summary | |
| except Exception as e: | |
| return f"Error performing search: {e}" | |
| try: | |
| from tavily import TavilyClient | |
| client = TavilyClient() | |
| response = client.search(query=query, max_results=5, search_depth="basic") | |
| results = response.get("results", []) | |
| if not results: | |
| return "No results found." | |
| summary = "" | |
| for r in results: | |
| summary += f"- [{r['title']}]({r['url']}): {r.get('content', '')}\n" | |
| return summary | |
| except ImportError: | |
| logging.error("Tavily SDK not installed. Please run 'pip install tavily-python'.") | |
| return "Error: Tavily search provider is not available." | |
| except Exception as e: | |
| logging.error(f"Error performing Tavily search: {e}") | |
| return f"Error performing search: {e}" |
| from ddgs import DDGS | ||
| try: | ||
| results = DDGS().text(query, max_results=5) | ||
| if not results: | ||
| return "No results found." | ||
|
|
||
| summary = "" | ||
| for r in results: | ||
| summary += f"- [{r['title']}]({r['href']}): {r['body']}\n" | ||
| return summary | ||
| except Exception as e: | ||
| return f"Error performing search: {e}" |
There was a problem hiding this comment.
There are a couple of improvements that can be made here:
- The
try...exceptblock does not cover theimportstatement. If theddgspackage is not installed, anImportErrorwill be raised and crash the application. The import should be moved inside thetryblock. - Using
r['body']can raise aKeyErrorif thebodykey is missing in a result. It's safer to user.get('body', '')for robustness, which is consistent with the Tavily implementation.
The suggestion below addresses both points.
| from ddgs import DDGS | |
| try: | |
| results = DDGS().text(query, max_results=5) | |
| if not results: | |
| return "No results found." | |
| summary = "" | |
| for r in results: | |
| summary += f"- [{r['title']}]({r['href']}): {r['body']}\n" | |
| return summary | |
| except Exception as e: | |
| return f"Error performing search: {e}" | |
| try: | |
| from ddgs import DDGS | |
| results = DDGS().text(query, max_results=5) | |
| if not results: | |
| return "No results found." | |
| summary = "" | |
| for r in results: | |
| summary += f"- [{r['title']}]({r['href']}): {r.get('body', '')}\n" | |
| return summary | |
| except ImportError: | |
| logging.error("DDGS SDK not installed. Please run 'pip install ddgs'.") | |
| return "Error: DuckDuckGo search provider is not available." | |
| except Exception as e: | |
| logging.error(f"Error performing DDGS search: {e}") | |
| return f"Error performing search: {e}" |
| if provider == "tavily": | ||
| return _perform_tavily_search(query) | ||
| return _perform_ddgs_search(query) |
There was a problem hiding this comment.
The current implementation silently defaults to DuckDuckGo for any SEARCH_PROVIDER value other than "tavily". This could be confusing if a user makes a typo (e.g., "tavilyy"). It would be more user-friendly to explicitly handle the "duckduckgo" case and log a warning for unknown providers before defaulting.
if provider == "tavily":
return _perform_tavily_search(query)
elif provider == "duckduckgo":
return _perform_ddgs_search(query)
else:
logging.warning(f"Unknown SEARCH_PROVIDER '{provider}', defaulting to duckduckgo.")
return _perform_ddgs_search(query)
Summary
_legacy/skills/web_search.pySEARCH_PROVIDERenvironment variable controls which provider is used (tavilyorduckduckgo, default:duckduckgo)SEARCH_PROVIDERis unset or set toduckduckgoFiles changed
_legacy/skills/web_search.py— Refactoredperform_web_search()into provider-specific helpers (_perform_tavily_search,_perform_ddgs_search) with env-var-based dispatchrequirements.txt— Addedtavily-pythondependencyDependency changes
tavily-pythontorequirements.txtEnvironment variable changes
SEARCH_PROVIDER— Set to"tavily"to use Tavily, defaults to"duckduckgo"TAVILY_API_KEY— Required whenSEARCH_PROVIDER=tavily(read automatically by the Tavily SDK)Notes for reviewers
🤖 Generated with Claude Code
Automated Review
perform_web_search()is split into a private_perform_ddgs_search()(unchanged logic) and a new_perform_tavily_search(), with a dispatcher checking theSEARCH_PROVIDERenv var.tavily-pythonis added torequirements.txt. The Tavily SDK is used correctly —TavilyClient()auto-readsTAVILY_API_KEYfrom the environment, theclient.search()call is valid, and response fields (title,url,content) match the Tavily API response schema. DuckDuckGo behavior is fully preserved as the default. No regressions, no dead code, no unintended changes. Two minor points noted but neither is blocking.