From d0bfc7f82001567390d7f942fc2e49dcb9e7b370 Mon Sep 17 00:00:00 2001 From: pulvedu Date: Thu, 13 Mar 2025 18:19:31 -0400 Subject: [PATCH] community[fix] : Pass API_KEY as argument (#30272) PR Title: community: Fix Pass API_KEY as argument PR Message: Description: This PR fixes validation error "Value error, Did not find tavily_api_key, please add an environment variable `TAVILY_API_KEY` which contains it, or pass `tavily_api_key` as a named parameter." Dependencies: No new dependencies introduced. --------- Co-authored-by: pulvedu Co-authored-by: Chester Curme --- .../langchain_community/tools/tavily_search/tool.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/tools/tavily_search/tool.py b/libs/community/langchain_community/tools/tavily_search/tool.py index 765f99daecf..32cd21d4197 100644 --- a/libs/community/langchain_community/tools/tavily_search/tool.py +++ b/libs/community/langchain_community/tools/tavily_search/tool.py @@ -1,6 +1,6 @@ """Tool for the Tavily search API.""" -from typing import Dict, List, Literal, Optional, Tuple, Type, Union +from typing import Any, Dict, List, Literal, Optional, Tuple, Type, Union from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, @@ -149,6 +149,15 @@ class TavilySearchResults(BaseTool): # type: ignore[override, override] api_wrapper: TavilySearchAPIWrapper = Field(default_factory=TavilySearchAPIWrapper) # type: ignore[arg-type] response_format: Literal["content_and_artifact"] = "content_and_artifact" + def __init__(self, **kwargs: Any) -> None: + # Create api_wrapper with tavily_api_key if provided + if "tavily_api_key" in kwargs: + kwargs["api_wrapper"] = TavilySearchAPIWrapper( + tavily_api_key=kwargs["tavily_api_key"] + ) + + super().__init__(**kwargs) + def _run( self, query: str,