From fe1de7b10326a38e0d5943d7002ac7889c161826 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Thu, 5 Jun 2025 13:43:55 -0700 Subject: [PATCH] feat: Add url_context_tool PiperOrigin-RevId: 767747328 --- src/google/adk/tools/__init__.py | 2 + src/google/adk/tools/url_context_tool.py | 61 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/google/adk/tools/url_context_tool.py diff --git a/src/google/adk/tools/__init__.py b/src/google/adk/tools/__init__.py index 4fb316c5..c562d7f4 100644 --- a/src/google/adk/tools/__init__.py +++ b/src/google/adk/tools/__init__.py @@ -27,6 +27,7 @@ from .long_running_tool import LongRunningFunctionTool from .preload_memory_tool import preload_memory_tool as preload_memory from .tool_context import ToolContext from .transfer_to_agent_tool import transfer_to_agent +from .url_context_tool import url_context from .vertex_ai_search_tool import VertexAiSearchTool __all__ = [ @@ -34,6 +35,7 @@ __all__ = [ 'AuthToolArguments', 'BaseTool', 'google_search', + 'url_context', 'VertexAiSearchTool', 'ExampleTool', 'exit_loop', diff --git a/src/google/adk/tools/url_context_tool.py b/src/google/adk/tools/url_context_tool.py new file mode 100644 index 00000000..5a52eb44 --- /dev/null +++ b/src/google/adk/tools/url_context_tool.py @@ -0,0 +1,61 @@ +# 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 __future__ import annotations + +from typing import TYPE_CHECKING + +from google.genai import types +from typing_extensions import override + +from .base_tool import BaseTool +from .tool_context import ToolContext + +if TYPE_CHECKING: + from ..models import LlmRequest + + +class UrlContextTool(BaseTool): + """A built-in tool that is automatically invoked by Gemini 2 models to retrieve content from the URLs and use that content to inform and shape its response. + + This tool operates internally within the model and does not require or perform + local code execution. + """ + + def __init__(self): + # Name and description are not used because this is a model built-in tool. + super().__init__(name='url_context', description='url_context') + + @override + async def process_llm_request( + self, + *, + tool_context: ToolContext, + llm_request: LlmRequest, + ) -> None: + llm_request.config = llm_request.config or types.GenerateContentConfig() + llm_request.config.tools = llm_request.config.tools or [] + if llm_request.model and 'gemini-1' in llm_request.model: + raise ValueError('Url context tool can not be used in Gemini 1.x.') + elif llm_request.model and 'gemini-2' in llm_request.model: + llm_request.config.tools.append( + types.Tool(url_context=types.UrlContext()) + ) + else: + raise ValueError( + f'Url context tool is not supported for model {llm_request.model}' + ) + + +url_context = UrlContextTool()