Skip to content

Commit 5062205

Browse files
authored
feat(genai): add support to built-in url_context tool (#1371)
1 parent 558cacb commit 5062205

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

libs/genai/langchain_google_genai/_function_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@
5555

5656
_GoogleSearchLike = gapic.Tool.GoogleSearch | dict[str, Any]
5757
_CodeExecutionLike = gapic.CodeExecution | dict[str, Any]
58+
_UrlContextLike = gapic.UrlContext | dict[str, Any]
5859

5960

6061
class _ToolDict(TypedDict):
6162
function_declarations: Sequence[_FunctionDeclarationLike]
6263
google_search_retrieval: _GoogleSearchRetrievalLike | None
6364
google_search: NotRequired[_GoogleSearchLike]
6465
code_execution: NotRequired[_CodeExecutionLike]
66+
url_context: NotRequired[_UrlContextLike]
6567

6668

6769
# Info: This means one tool=Sequence of FunctionDeclaration
@@ -153,6 +155,7 @@ def convert_to_genai_function_declarations(
153155
"google_search_retrieval",
154156
"google_search",
155157
"code_execution",
158+
"url_context",
156159
]
157160
):
158161
fd = _format_to_gapic_function_declaration(tool) # type: ignore[arg-type]
@@ -186,6 +189,8 @@ def convert_to_genai_function_declarations(
186189
)
187190
if "code_execution" in tool:
188191
gapic_tool.code_execution = gapic.CodeExecution(tool["code_execution"])
192+
if "url_context" in tool:
193+
gapic_tool.url_context = gapic.UrlContext(tool["url_context"])
189194
else:
190195
fd = _format_to_gapic_function_declaration(tool)
191196
gapic_tool.function_declarations.append(fd)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from langchain_core.messages import AIMessage
2+
3+
from langchain_google_genai import ChatGoogleGenerativeAI
4+
5+
_MODEL = "gemini-2.5-flash"
6+
7+
8+
def test_url_context_tool() -> None:
9+
model = ChatGoogleGenerativeAI(model=_MODEL)
10+
model_with_search = model.bind_tools([{"url_context": {}}])
11+
12+
input = "What is this page's contents about? https://docs.langchain.com"
13+
response = model_with_search.invoke(input)
14+
assert isinstance(response, AIMessage)
15+
16+
assert (
17+
response.response_metadata["grounding_metadata"]["grounding_chunks"][0]["web"][
18+
"uri"
19+
]
20+
is not None
21+
)

0 commit comments

Comments
 (0)