chore: Lazy load Vertex AI dependencies in ADK modules

This is about 35% decrease. This change refactors several ADK modules to import `vertexai` and its submodules only when they are first used, rather than at the top of the file. This improves module load times by avoiding unnecessary imports of large dependencies. Imports are also placed within `if TYPE_CHECKING:` blocks where appropriate.

Co-authored-by: Liang Wu <wuliang@google.com>
PiperOrigin-RevId: 829017293
This commit is contained in:
Liang Wu
2025-11-06 10:40:54 -08:00
committed by Copybara-Service
parent 5f057498a2
commit 44d45fe9cd
12 changed files with 38 additions and 33 deletions
@@ -58,17 +58,15 @@ class TestAgentEngineSandboxCodeExecutor:
),
)
@patch(
"google.adk.code_executors.agent_engine_sandbox_code_executor.vertexai"
)
@patch("vertexai.Client")
def test_execute_code_success(
self,
mock_vertexai,
mock_vertexai_client,
mock_invocation_context,
):
# Setup Mocks
mock_api_client = MagicMock()
mock_vertexai.Client.return_value = mock_api_client
mock_vertexai_client.return_value = mock_api_client
mock_response = MagicMock()
mock_json_output = MagicMock()
mock_json_output.mime_type = "application/json"
@@ -87,9 +87,7 @@ def mock_vertex_ai_memory_bank_service(
@pytest.fixture
def mock_vertexai_client():
with mock.patch(
'google.adk.memory.vertex_ai_memory_bank_service.vertexai.Client'
) as mock_client_constructor:
with mock.patch('vertexai.Client') as mock_client_constructor:
mock_client = mock.MagicMock()
mock_client.agent_engines.memories.generate = mock.MagicMock()
mock_client.agent_engines.memories.retrieve = mock.MagicMock()