Skip to content

Commit 7e637d3

Browse files
seanzhougooglecopybara-github
authored andcommitted
feat: support Langchain StructuredTool for Langchain tool
for fixing: #707 PiperOrigin-RevId: 764781197
1 parent 2a65c41 commit 7e637d3

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
"""
16+
This agent aims to test the Langchain tool with Langchain's StructuredTool
17+
"""
18+
from google.adk.agents import Agent
19+
from google.adk.tools.langchain_tool import LangchainTool
20+
from langchain_core.tools.structured import StructuredTool
21+
from pydantic import BaseModel
22+
23+
24+
def add(x, y) -> int:
25+
return x + y
26+
27+
28+
class AddSchema(BaseModel):
29+
x: int
30+
y: int
31+
32+
33+
test_langchain_tool = StructuredTool.from_function(
34+
add,
35+
name="add",
36+
description="Adds two numbers",
37+
args_schema=AddSchema,
38+
)
39+
40+
root_agent = Agent(
41+
model="gemini-2.0-flash-001",
42+
name="test_app",
43+
description="A helpful assistant for user questions.",
44+
instruction=(
45+
"You are a helpful assistant for user questions, you have access to a"
46+
" tool that adds two numbers."
47+
),
48+
tools=[LangchainTool(tool=test_langchain_tool)],
49+
)

src/google/adk/tools/langchain_tool.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Any
15+
from __future__ import annotations
16+
1617
from typing import Optional
1718
from typing import Union
1819

1920
from google.genai import types
2021
from langchain.agents import Tool
2122
from langchain_core.tools import BaseTool
23+
from langchain_core.tools.structured import StructuredTool
2224
from typing_extensions import override
2325

2426
from . import _automatic_function_calling_util
@@ -63,7 +65,10 @@ def __init__(
6365
raise ValueError("Langchain tool must have a 'run' or '_run' method")
6466

6567
# Determine which function to use
66-
func = tool._run if hasattr(tool, '_run') else tool.run
68+
if isinstance(tool, StructuredTool):
69+
func = tool.func
70+
else:
71+
func = tool._run if hasattr(tool, '_run') else tool.run
6772
super().__init__(func)
6873

6974
self._langchain_tool = tool

0 commit comments

Comments
 (0)