mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
chore: provide a way to disable model check for builtin tools
Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 872503435
This commit is contained in:
committed by
Copybara-Service
parent
f27a9cfb87
commit
eaf50ce37e
@@ -97,6 +97,22 @@ def test_process_llm_request_non_gemini_2_model(
|
||||
)
|
||||
|
||||
|
||||
def test_process_llm_request_non_gemini_2_model_with_disabled_check(
|
||||
built_in_executor: BuiltInCodeExecutor,
|
||||
monkeypatch,
|
||||
):
|
||||
"""Tests non-Gemini models pass when model-id check is disabled."""
|
||||
monkeypatch.setenv("ADK_DISABLE_GEMINI_MODEL_ID_CHECK", "true")
|
||||
llm_request = LlmRequest(model="internal-model-v1")
|
||||
|
||||
built_in_executor.process_llm_request(llm_request)
|
||||
|
||||
assert llm_request.config is not None
|
||||
assert llm_request.config.tools == [
|
||||
types.Tool(code_execution=types.ToolCodeExecution())
|
||||
]
|
||||
|
||||
|
||||
def test_process_llm_request_no_model_name(
|
||||
built_in_executor: BuiltInCodeExecutor,
|
||||
):
|
||||
|
||||
@@ -145,3 +145,43 @@ def test_vertex_rag_retrieval_for_gemini_2_x():
|
||||
)
|
||||
]
|
||||
assert 'rag_retrieval' not in mockModel.requests[0].tools_dict
|
||||
|
||||
|
||||
def test_vertex_rag_retrieval_for_non_gemini_with_disabled_check(monkeypatch):
|
||||
monkeypatch.setenv('ADK_DISABLE_GEMINI_MODEL_ID_CHECK', 'true')
|
||||
responses = [
|
||||
'response1',
|
||||
]
|
||||
mockModel = testing_utils.MockModel.create(responses=responses)
|
||||
mockModel.model = 'internal-model-v1'
|
||||
|
||||
agent = Agent(
|
||||
name='root_agent',
|
||||
model=mockModel,
|
||||
tools=[
|
||||
VertexAiRagRetrieval(
|
||||
name='rag_retrieval',
|
||||
description='rag_retrieval',
|
||||
rag_corpora=[
|
||||
'projects/123456789/locations/us-central1/ragCorpora/1234567890'
|
||||
],
|
||||
)
|
||||
],
|
||||
)
|
||||
runner = testing_utils.InMemoryRunner(agent)
|
||||
runner.run('test1')
|
||||
|
||||
assert len(mockModel.requests) == 1
|
||||
assert len(mockModel.requests[0].config.tools) == 1
|
||||
assert mockModel.requests[0].config.tools == [
|
||||
types.Tool(
|
||||
retrieval=types.Retrieval(
|
||||
vertex_rag_store=types.VertexRagStore(
|
||||
rag_corpora=[
|
||||
'projects/123456789/locations/us-central1/ragCorpora/1234567890'
|
||||
]
|
||||
)
|
||||
)
|
||||
)
|
||||
]
|
||||
assert 'rag_retrieval' not in mockModel.requests[0].tools_dict
|
||||
|
||||
@@ -76,6 +76,25 @@ async def test_process_llm_request_failure_with_non_gemini_models():
|
||||
assert 'is not supported for model' in str(exc_info.value)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_non_gemini_with_disabled_check(monkeypatch):
|
||||
monkeypatch.setenv('ADK_DISABLE_GEMINI_MODEL_ID_CHECK', 'true')
|
||||
tool = EnterpriseWebSearchTool()
|
||||
llm_request = LlmRequest(
|
||||
model='internal-model-v1', config=types.GenerateContentConfig()
|
||||
)
|
||||
tool_context = await _create_tool_context()
|
||||
|
||||
await tool.process_llm_request(
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
assert (
|
||||
llm_request.config.tools[0].enterprise_web_search
|
||||
== types.EnterpriseWebSearch()
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_failure_with_multiple_tools_gemini_1_models():
|
||||
tool = EnterpriseWebSearchTool()
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
# Copyright 2026 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.invocation_context import InvocationContext
|
||||
from google.adk.agents.sequential_agent import SequentialAgent
|
||||
from google.adk.models.llm_request import LlmRequest
|
||||
from google.adk.sessions.in_memory_session_service import InMemorySessionService
|
||||
from google.adk.tools.google_maps_grounding_tool import GoogleMapsGroundingTool
|
||||
from google.adk.tools.tool_context import ToolContext
|
||||
from google.genai import types
|
||||
import pytest
|
||||
|
||||
|
||||
async def _create_tool_context() -> ToolContext:
|
||||
session_service = InMemorySessionService()
|
||||
session = await session_service.create_session(
|
||||
app_name='test_app', user_id='test_user'
|
||||
)
|
||||
agent = SequentialAgent(name='test_agent')
|
||||
invocation_context = InvocationContext(
|
||||
invocation_id='invocation_id',
|
||||
agent=agent,
|
||||
session=session,
|
||||
session_service=session_service,
|
||||
)
|
||||
return ToolContext(invocation_context=invocation_context)
|
||||
|
||||
|
||||
class TestGoogleMapsGroundingTool:
|
||||
"""Tests for GoogleMapsGroundingTool."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_with_gemini_2_model(self):
|
||||
tool = GoogleMapsGroundingTool()
|
||||
tool_context = await _create_tool_context()
|
||||
llm_request = LlmRequest(
|
||||
model='gemini-2.5-pro', config=types.GenerateContentConfig()
|
||||
)
|
||||
|
||||
await tool.process_llm_request(
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
assert llm_request.config.tools is not None
|
||||
assert len(llm_request.config.tools) == 1
|
||||
assert llm_request.config.tools[0].google_maps is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_with_non_gemini_model_raises_error(self):
|
||||
tool = GoogleMapsGroundingTool()
|
||||
tool_context = await _create_tool_context()
|
||||
llm_request = LlmRequest(
|
||||
model='claude-3-sonnet', config=types.GenerateContentConfig()
|
||||
)
|
||||
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match='Google maps tool is not supported for model claude-3-sonnet',
|
||||
):
|
||||
await tool.process_llm_request(
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_with_non_gemini_and_disabled_check(
|
||||
self, monkeypatch
|
||||
):
|
||||
monkeypatch.setenv('ADK_DISABLE_GEMINI_MODEL_ID_CHECK', 'true')
|
||||
tool = GoogleMapsGroundingTool()
|
||||
tool_context = await _create_tool_context()
|
||||
llm_request = LlmRequest(
|
||||
model='internal-model-v1', config=types.GenerateContentConfig()
|
||||
)
|
||||
|
||||
await tool.process_llm_request(
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
assert llm_request.config.tools is not None
|
||||
assert len(llm_request.config.tools) == 1
|
||||
assert llm_request.config.tools[0].google_maps is not None
|
||||
@@ -268,6 +268,27 @@ class TestGoogleSearchTool:
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_with_non_gemini_model_and_disabled_check(
|
||||
self, monkeypatch
|
||||
):
|
||||
"""Test non-Gemini model can pass when model-id check is disabled."""
|
||||
monkeypatch.setenv('ADK_DISABLE_GEMINI_MODEL_ID_CHECK', 'true')
|
||||
tool = GoogleSearchTool()
|
||||
tool_context = await _create_tool_context()
|
||||
|
||||
llm_request = LlmRequest(
|
||||
model='internal-model-v1', config=types.GenerateContentConfig()
|
||||
)
|
||||
|
||||
await tool.process_llm_request(
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
assert llm_request.config.tools is not None
|
||||
assert len(llm_request.config.tools) == 1
|
||||
assert llm_request.config.tools[0].google_search is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_with_path_based_non_gemini_model_raises_error(
|
||||
self,
|
||||
|
||||
@@ -190,6 +190,27 @@ class TestUrlContextTool:
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_with_non_gemini_model_and_disabled_check(
|
||||
self, monkeypatch
|
||||
):
|
||||
"""Test non-Gemini model can pass when model-id check is disabled."""
|
||||
monkeypatch.setenv('ADK_DISABLE_GEMINI_MODEL_ID_CHECK', 'true')
|
||||
tool = UrlContextTool()
|
||||
tool_context = await _create_tool_context()
|
||||
|
||||
llm_request = LlmRequest(
|
||||
model='internal-model-v1', config=types.GenerateContentConfig()
|
||||
)
|
||||
|
||||
await tool.process_llm_request(
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
assert llm_request.config.tools is not None
|
||||
assert len(llm_request.config.tools) == 1
|
||||
assert llm_request.config.tools[0].url_context is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_with_path_based_non_gemini_model_raises_error(
|
||||
self,
|
||||
|
||||
@@ -376,6 +376,29 @@ class TestVertexAiSearchTool:
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_with_non_gemini_model_and_disabled_check(
|
||||
self, monkeypatch
|
||||
):
|
||||
"""Test non-Gemini model can pass when model-id check is disabled."""
|
||||
monkeypatch.setenv('ADK_DISABLE_GEMINI_MODEL_ID_CHECK', 'true')
|
||||
tool = VertexAiSearchTool(data_store_id='test_data_store')
|
||||
tool_context = await _create_tool_context()
|
||||
|
||||
llm_request = LlmRequest(
|
||||
model='internal-model-v1', config=types.GenerateContentConfig()
|
||||
)
|
||||
|
||||
await tool.process_llm_request(
|
||||
tool_context=tool_context, llm_request=llm_request
|
||||
)
|
||||
|
||||
assert llm_request.config.tools is not None
|
||||
assert len(llm_request.config.tools) == 1
|
||||
retrieval_tool = llm_request.config.tools[0]
|
||||
assert retrieval_tool.retrieval is not None
|
||||
assert retrieval_tool.retrieval.vertex_ai_search is not None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_process_llm_request_with_path_based_non_gemini_model_raises_error(
|
||||
self,
|
||||
|
||||
@@ -18,6 +18,7 @@ from google.adk.utils.model_name_utils import extract_model_name
|
||||
from google.adk.utils.model_name_utils import is_gemini_1_model
|
||||
from google.adk.utils.model_name_utils import is_gemini_2_or_above
|
||||
from google.adk.utils.model_name_utils import is_gemini_model
|
||||
from google.adk.utils.model_name_utils import is_gemini_model_id_check_disabled
|
||||
|
||||
|
||||
class TestExtractModelName:
|
||||
@@ -318,3 +319,15 @@ class TestModelNameUtilsIntegration:
|
||||
f'Inconsistent Gemini 2.0+ classification for {simple_model} vs'
|
||||
f' {path_model}'
|
||||
)
|
||||
|
||||
|
||||
class TestGeminiModelIdCheckFlag:
|
||||
"""Tests for Gemini model-id check override flag."""
|
||||
|
||||
def test_default_is_disabled(self, monkeypatch):
|
||||
monkeypatch.delenv('ADK_DISABLE_GEMINI_MODEL_ID_CHECK', raising=False)
|
||||
assert is_gemini_model_id_check_disabled() is False
|
||||
|
||||
def test_true_enables_check_bypass(self, monkeypatch):
|
||||
monkeypatch.setenv('ADK_DISABLE_GEMINI_MODEL_ID_CHECK', 'true')
|
||||
assert is_gemini_model_id_check_disabled() is True
|
||||
|
||||
Reference in New Issue
Block a user