From 3616bb5fc4da90e79eb89039fb5e302d6a0a14ec Mon Sep 17 00:00:00 2001 From: "Xiang (Sean) Zhou" Date: Fri, 30 May 2025 18:05:02 -0700 Subject: [PATCH] feat:support Langchain tools that has run_manager in _run args and don't have args_schema populated PiperOrigin-RevId: 765405793 --- .../langchain_youtube_search_agent/README.md | 8 +++++ .../__init__.py | 15 ++++++++ .../langchain_youtube_search_agent/agent.py | 36 +++++++++++++++++++ src/google/adk/tools/langchain_tool.py | 24 +++++++------ 4 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 contributing/samples/langchain_youtube_search_agent/README.md create mode 100644 contributing/samples/langchain_youtube_search_agent/__init__.py create mode 100644 contributing/samples/langchain_youtube_search_agent/agent.py diff --git a/contributing/samples/langchain_youtube_search_agent/README.md b/contributing/samples/langchain_youtube_search_agent/README.md new file mode 100644 index 0000000..e87ca59 --- /dev/null +++ b/contributing/samples/langchain_youtube_search_agent/README.md @@ -0,0 +1,8 @@ +# Langchain Youtube Search Agent + +This agent utilize the Lanchain YoutubeSearchTool to search youtubes. +You need to install below dependencies: + +```python +uv pip install youtube_search +``` diff --git a/contributing/samples/langchain_youtube_search_agent/__init__.py b/contributing/samples/langchain_youtube_search_agent/__init__.py new file mode 100644 index 0000000..c48963c --- /dev/null +++ b/contributing/samples/langchain_youtube_search_agent/__init__.py @@ -0,0 +1,15 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from . import agent diff --git a/contributing/samples/langchain_youtube_search_agent/agent.py b/contributing/samples/langchain_youtube_search_agent/agent.py new file mode 100644 index 0000000..70d7b1e --- /dev/null +++ b/contributing/samples/langchain_youtube_search_agent/agent.py @@ -0,0 +1,36 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from google.adk.agents import LlmAgent +from google.adk.tools.langchain_tool import LangchainTool +from langchain_community.tools import YouTubeSearchTool + +# Instantiate the tool +langchain_yt_tool = YouTubeSearchTool() + +# Wrap the tool in the LangchainTool class from ADK +adk_yt_tool = LangchainTool( + tool=langchain_yt_tool, +) + +root_agent = LlmAgent( + name="youtube_search_agent", + model="gemini-2.0-flash", # Replace with the actual model name + instruction=""" + Ask customer to provide singer name, and the number of videos to search. + """, + description="Help customer to search for a video on Youtube.", + tools=[adk_yt_tool], + output_key="youtube_search_output", +) diff --git a/src/google/adk/tools/langchain_tool.py b/src/google/adk/tools/langchain_tool.py index 38d9a8f..46cda6a 100644 --- a/src/google/adk/tools/langchain_tool.py +++ b/src/google/adk/tools/langchain_tool.py @@ -70,7 +70,8 @@ class LangchainTool(FunctionTool): else: func = tool._run if hasattr(tool, '_run') else tool.run super().__init__(func) - + # run_manager is a special parameter for langchain tool + self._ignore_params.append('run_manager') self._langchain_tool = tool # Set name: priority is 1) explicitly provided name, 2) tool's name, 3) default @@ -117,20 +118,21 @@ class LangchainTool(FunctionTool): ): tool_wrapper.args_schema = self._langchain_tool.args_schema - return _automatic_function_calling_util.build_function_declaration_for_langchain( - False, - self.name, - self.description, - tool_wrapper.func, - getattr(tool_wrapper, 'args', None), - ) + return _automatic_function_calling_util.build_function_declaration_for_langchain( + False, + self.name, + self.description, + tool_wrapper.func, + tool_wrapper.args, + ) # Need to provide a way to override the function names and descriptions # as the original function names are mostly ".run" and the descriptions # may not meet users' needs - return _automatic_function_calling_util.build_function_declaration( - func=self._langchain_tool.run, - ) + function_decl = super()._get_declaration() + function_decl.name = self.name + function_decl.description = self.description + return function_decl except Exception as e: raise ValueError(