8000 feat:support Langchain tools that has run_manager in _run args and do… · nag763/adk-python@3616bb5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3616bb5

Browse files
seanzhougooglecopybara-github
authored andcommitted
feat:support Langchain tools that has run_manager in _run args and don't have args_schema populated
PiperOrigin-RevId: 765405793
1 parent e06e675 commit 3616bb5

File tree

4 files changed

+72
-11
lines changed

4 files changed

+72
-11
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Langchain Youtube Search Agent
2+
3+
This agent utilize the Lanchain YoutubeSearchTool to search youtubes.
4+
You need to install below dependencies:
5+
6+
```python
7+
uv pip install youtube_search
8+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import agent
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.adk.agents import LlmAgent
16+
from google.adk.tools.langchain_tool import LangchainTool
17+
from langchain_community.tools import YouTubeSearchTool
18+
19+
# Instantiate the tool
20+
langchain_yt_tool = YouTubeSearchTool()
21+
22+
# Wrap the tool in the LangchainTool class from ADK
23+
adk_yt_tool = LangchainTool(
24+
tool=langchain_yt_tool,
25+
)
26+
27+
root_agent = LlmAgent(
28+
name="youtube_search_agent",
29+
model="gemini-2.0-flash", # Replace with the actual model name
30+
instruction="""
31+
Ask customer to provide singer name, and the number of videos to search.
32+
""",
33+
description="Help customer to search for a video on Youtube.",
34+
tools=[adk_yt_tool],
35+
output_key="youtube_search_output",
36+
)

src/google/adk/tools/langchain_tool.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def __init__(
7070
else:
7171
func = tool._run if hasattr(tool, '_run') else tool.run
7272
super().__init__(func)
73-
73+
# run_manager is a special parameter for langchain tool
74+
self._ignore_params.append('run_manager')
7475
self._langchain_tool = tool
7576

7677
# Set name: priority is 1) explicitly provided name, 2) tool's name, 3) default
@@ -117,20 +118,21 @@ def _get_declaration(self) -> types.FunctionDeclaration:
117118
):
118119
tool_wrapper.args_schema = self._langchain_tool.args_schema
119120

120-
return _automatic_function_calling_util.build_function_declaration_for_langchain(
121-
False,
122-
self.name,
123-
self.description,
124-
tool_wrapper.func,
125-
getattr(tool_wrapper, 'args', None),
126-
)
121+
return _automatic_function_calling_util.build_function_declaration_for_langchain(
122+
False,
123+
self.name,
124+
self.description,
125+
tool_wrapper.func,
126+
tool_wrapper.args,
127+
)
127128

128129
# Need to provide a way to override the function names and descriptions
129130
# as the original function names are mostly ".run" and the descriptions
130131
# may not meet users' needs
131-
return _automatic_function_calling_util.build_function_declaration(
132-
func=self._langchain_tool.run,
133-
)
132+
function_decl = super()._get_declaration()
133+
function_decl.name = self.name
134+
function_decl.description = self.description
135+
return function_decl
134136

135137
except Exception as e:
136138
raise ValueError(

0 commit comments

Comments
 (0)
0