diff --git a/src/google/adk/dependencies/vertexai.py b/src/google/adk/dependencies/vertexai.py index 6f882704d70..38186231c8f 100644 --- a/src/google/adk/dependencies/vertexai.py +++ b/src/google/adk/dependencies/vertexai.py @@ -16,4 +16,6 @@ import vertexai as vertexai from vertexai.preview import example_stores as example_stores -from vertexai.preview import rag as rag + +# vertexai.preview.rag is not imported here: loading it emits a UserWarning +# on google-cloud-aiplatform 1.163+, and evaluation only needs vertexai itself. diff --git a/src/google/adk/tools/retrieval/vertex_ai_rag_retrieval.py b/src/google/adk/tools/retrieval/vertex_ai_rag_retrieval.py index 25bbe52daf7..83bef2f77a2 100644 --- a/src/google/adk/tools/retrieval/vertex_ai_rag_retrieval.py +++ b/src/google/adk/tools/retrieval/vertex_ai_rag_retrieval.py @@ -32,7 +32,7 @@ from .base_retrieval_tool import BaseRetrievalTool if TYPE_CHECKING: - from ...dependencies.vertexai import rag + from vertexai.preview import rag logger = logging.getLogger('google_adk.' + __name__) @@ -100,7 +100,9 @@ async def run_async( args: dict[str, Any], tool_context: ToolContext, ) -> Any: - from ...dependencies.vertexai import rag + # Load inside the call: vertexai.preview.rag is deprecated and warns on + # import, so evaluation and other Vertex helpers must not pull it in. + from vertexai.preview import rag query = args.get('query') if not isinstance(query, str): diff --git a/tests/unittests/dependencies/test_vertexai.py b/tests/unittests/dependencies/test_vertexai.py new file mode 100644 index 00000000000..161bc57b4da --- /dev/null +++ b/tests/unittests/dependencies/test_vertexai.py @@ -0,0 +1,60 @@ +# 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 __future__ import annotations + +import importlib +import subprocess +import sys +import warnings + + +def test_importing_eval_facade_does_not_warn_about_preview_rag(): + """Importing the Vertex AI eval facade does not emit the preview.rag deprecation.""" + script = """ +import warnings + +warnings.simplefilter('always', UserWarning) +with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter('always', UserWarning) + import google.adk.evaluation.vertex_ai_eval_facade + +rag_warnings = [w for w in caught if 'preview.rag' in str(w.message)] +raise SystemExit(1 if rag_warnings else 0) +""" + result = subprocess.run( + [sys.executable, '-c', script], + capture_output=True, + text=True, + check=False, + ) + assert result.returncode == 0, result.stderr or result.stdout + + +def test_vertexai_dependency_does_not_load_preview_rag(): + """Importing google.adk.dependencies.vertexai does not load vertexai.preview.rag.""" + sys.modules.pop('google.adk.dependencies.vertexai', None) + sys.modules.pop('vertexai.preview.rag', None) + + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter('always', UserWarning) + module = importlib.import_module('google.adk.dependencies.vertexai') + importlib.reload(module) + + rag_warnings = [ + warning for warning in caught if 'preview.rag' in str(warning.message) + ] + assert rag_warnings == [] + assert 'vertexai.preview.rag' not in sys.modules + assert 'rag' not in module.__dict__ diff --git a/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py b/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py index f366a28c6ee..3857fb090ae 100644 --- a/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py +++ b/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py @@ -57,9 +57,7 @@ async def test_retrieval_query_gets_the_original_rag_resources(mocker): description='rag_retrieval', rag_resources=[resource], ) - retrieval_query = mocker.patch( - 'google.adk.dependencies.vertexai.rag.retrieval_query' - ) + retrieval_query = mocker.patch('vertexai.preview.rag.retrieval_query') retrieval_query.return_value.contexts.contexts = [] await retrieval.run_async(args={'query': 'q'}, tool_context=mocker.Mock())