feat:support Langchain tools that has run_manager in _run args and don't have args_schema populated

PiperOrigin-RevId: 765405793
This commit is contained in:
Xiang (Sean) Zhou 2025-05-30 18:05:02 -07:00 committed by Copybara-Service
parent e06e6753ad
commit 3616bb5fc4
4 changed files with 72 additions and 11 deletions

View File

@ -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
```

View File

@ -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

View File

@ -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",
)

View File

@ -70,7 +70,8 @@ class LangchainTool(FunctionTool):
else: else:
func = tool._run if hasattr(tool, '_run') else tool.run func = tool._run if hasattr(tool, '_run') else tool.run
super().__init__(func) super().__init__(func)
# run_manager is a special parameter for langchain tool
self._ignore_params.append('run_manager')
self._langchain_tool = tool self._langchain_tool = tool
# Set name: priority is 1) explicitly provided name, 2) tool's name, 3) default # 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 tool_wrapper.args_schema = self._langchain_tool.args_schema
return _automatic_function_calling_util.build_function_declaration_for_langchain( return _automatic_function_calling_util.build_function_declaration_for_langchain(
False, False,
self.name, self.name,
self.description, self.description,
tool_wrapper.func, tool_wrapper.func,
getattr(tool_wrapper, 'args', None), tool_wrapper.args,
) )
# Need to provide a way to override the function names and descriptions # Need to provide a way to override the function names and descriptions
# as the original function names are mostly ".run" and the descriptions # as the original function names are mostly ".run" and the descriptions
# may not meet users' needs # may not meet users' needs
return _automatic_function_calling_util.build_function_declaration( function_decl = super()._get_declaration()
func=self._langchain_tool.run, function_decl.name = self.name
) function_decl.description = self.description
return function_decl
except Exception as e: except Exception as e:
raise ValueError( raise ValueError(