Skip to content

Commit fde6a2b

Browse files
he-yufengDeanChensj
authored andcommitted
fix(dependencies): clarify missing Vertex AI extra
Merge #5904 Change-Id: I7046548303aeb66bab1956b70803fc13b6be7d38
1 parent 8847f23 commit fde6a2b

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/google/adk/dependencies/vertexai.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
"""Lazy import shim for Vertex AI optional dependencies."""
1415

1516
from __future__ import annotations
1617

17-
import vertexai
18-
from vertexai.preview import example_stores
19-
from vertexai.preview import rag
18+
try:
19+
import vertexai
20+
from vertexai.preview import example_stores
21+
from vertexai.preview import rag
22+
except ImportError as e:
23+
raise ImportError(
24+
"Vertex AI features require google-adk[gcp] or google-adk[all]. "
25+
"Install one of those extras to use google.adk.dependencies.vertexai."
26+
) from e
27+
28+
__all__ = ["example_stores", "rag", "vertexai"]

tests/unittests/test_optional_dependencies.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from __future__ import annotations
2222

23+
import importlib.util
2324
import os
2425
from pathlib import Path
2526
import subprocess
@@ -167,6 +168,25 @@ def test_vertex_ai_session_service_fails_on_creation():
167168
assert "google-cloud-aiplatform" in str(exc_info.value)
168169

169170

171+
def test_vertexai_dependency_shim_raises_clear_importerror():
172+
"""Verify that the Vertex AI dependency shim points users to the gcp extra."""
173+
with mock.patch.dict("sys.modules", {"vertexai": None}):
174+
module_path = _REPO_ROOT / "src/google/adk/dependencies/vertexai.py"
175+
spec = importlib.util.spec_from_file_location(
176+
"_test_google_adk_dependencies_vertexai", module_path
177+
)
178+
assert spec is not None
179+
assert spec.loader is not None
180+
module = importlib.util.module_from_spec(spec)
181+
182+
with pytest.raises(ImportError) as exc_info:
183+
spec.loader.exec_module(module)
184+
185+
message = str(exc_info.value)
186+
assert "google-adk[gcp]" in message
187+
assert "google-adk[all]" in message
188+
189+
170190
# =============================================================================
171191
# Approach 2: High-Fidelity Integration Tests (Clean Venv, Skipped by Default)
172192
# =============================================================================

0 commit comments

Comments
 (0)