Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/google/adk/dependencies/vertexai.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@
# 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.
"""Lazy import shim for Vertex AI optional dependencies."""

from __future__ import annotations

import vertexai
from vertexai.preview import example_stores
from vertexai.preview import rag
try:
import vertexai
from vertexai.preview import example_stores
from vertexai.preview import rag
except ImportError as e:
raise ImportError(
"Vertex AI features require google-adk[gcp] or google-adk[all]. "
"Install one of those extras to use google.adk.dependencies.vertexai."
) from e

__all__ = ["example_stores", "rag", "vertexai"]
20 changes: 20 additions & 0 deletions tests/unittests/test_optional_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from __future__ import annotations

import importlib.util
import os
from pathlib import Path
import subprocess
Expand Down Expand Up @@ -167,6 +168,25 @@ def test_vertex_ai_session_service_fails_on_creation():
assert "google-cloud-aiplatform" in str(exc_info.value)


def test_vertexai_dependency_shim_raises_clear_importerror():
"""Verify that the Vertex AI dependency shim points users to the gcp extra."""
with mock.patch.dict("sys.modules", {"vertexai": None}):
module_path = _REPO_ROOT / "src/google/adk/dependencies/vertexai.py"
spec = importlib.util.spec_from_file_location(
"_test_google_adk_dependencies_vertexai", module_path
)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)

with pytest.raises(ImportError) as exc_info:
spec.loader.exec_module(module)

message = str(exc_info.value)
assert "google-adk[gcp]" in message
assert "google-adk[all]" in message


# =============================================================================
# Approach 2: High-Fidelity Integration Tests (Clean Venv, Skipped by Default)
# =============================================================================
Expand Down