diff --git a/agentplatform/_genai/_runtimes_utils.py b/agentplatform/_genai/_runtimes_utils.py index 9aa20621ab..ed46877ed6 100644 --- a/agentplatform/_genai/_runtimes_utils.py +++ b/agentplatform/_genai/_runtimes_utils.py @@ -47,8 +47,6 @@ Union, ) -import httpx - import proto from google.api_core import exceptions @@ -112,6 +110,12 @@ _SpecifierSet: type[Any] = Any # type: ignore[no-redef] +try: + import httpx # pylint: disable=g-import-not-at-top +except ImportError: + # Only A2A operations use httpx, and a2a-sdk requires it. + httpx = None # type: ignore[assignment] + try: from a2a.types import AgentCard from a2a.client import ClientConfig, ClientFactory diff --git a/tests/unit/agentplatform/genai/test_agent_engines.py b/tests/unit/agentplatform/genai/test_agent_engines.py index e093a3faaa..c35fec395f 100644 --- a/tests/unit/agentplatform/genai/test_agent_engines.py +++ b/tests/unit/agentplatform/genai/test_agent_engines.py @@ -2124,6 +2124,15 @@ def test_yield_parsed_json_from_httpbody_non_json_content_type(self): body = httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello") assert list(_runtimes_utils._yield_parsed_json_from_httpbody(body)) == [body] + def test_module_loads_without_httpx(self): + """Only A2A operations use httpx, so the module loads without it.""" + spec = importlib.util.find_spec(_runtimes_utils.__name__) + module = importlib.util.module_from_spec(spec) + # A None entry in sys.modules makes `import httpx` raise ImportError. + with mock.patch.dict(sys.modules, {"httpx": None}): + spec.loader.exec_module(module) + assert module.httpx is None + def test_create_base64_encoded_tarball(self): with tempfile.TemporaryDirectory() as tmpdir: test_file_path = os.path.join(tmpdir, "test_file.txt") diff --git a/tests/unit/vertex_a2a/test_agent_engines_a2a.py b/tests/unit/vertex_a2a/test_agent_engines_a2a.py index aa6b97679c..9eb0f5aefa 100644 --- a/tests/unit/vertex_a2a/test_agent_engines_a2a.py +++ b/tests/unit/vertex_a2a/test_agent_engines_a2a.py @@ -14,6 +14,7 @@ # import asyncio +import importlib.util import os import sys import tempfile @@ -549,6 +550,29 @@ async def _empty_stream(): assert mock_async_client.call_args.kwargs["timeout"] == 120.0 +def _load_module_without_httpx(module): + """Executes a fresh copy of the module as if httpx were not installed.""" + spec = importlib.util.find_spec(module.__name__) + fresh_module = importlib.util.module_from_spec(spec) + # A None entry in sys.modules makes `import httpx` raise ImportError. + with mock.patch.dict(sys.modules, {"httpx": None}): + spec.loader.exec_module(fresh_module) + return fresh_module + + +class TestOptionalHttpxImport: + """Verifies that the Agent Engine modules load when httpx is not installed.""" + + def test_agent_engines_loads_without_httpx(self): + assert _load_module_without_httpx(_agent_engines).httpx is None + + def test_agent_engines_utils_loads_without_httpx(self): + # pylint: disable=g-import-not-at-top + from vertexai._genai import _agent_engines_utils + + assert _load_module_without_httpx(_agent_engines_utils).httpx is None + + class TestCreateAgentCard: """Tests for create_agent_card security_schemes, security, and kwargs support.""" diff --git a/vertexai/_genai/_agent_engines_utils.py b/vertexai/_genai/_agent_engines_utils.py index f72f382de6..da2b57ad8b 100644 --- a/vertexai/_genai/_agent_engines_utils.py +++ b/vertexai/_genai/_agent_engines_utils.py @@ -46,8 +46,6 @@ Union, ) -import httpx - import proto from google.api_core import exceptions @@ -110,6 +108,12 @@ _SpecifierSet: type[Any] = Any # type: ignore[no-redef] +try: + import httpx # pylint: disable=g-import-not-at-top +except ImportError: + # Only A2A operations use httpx, and a2a-sdk requires it. + httpx = None # type: ignore[assignment] + try: from a2a.utils.constants import TransportProtocol as _A2aVersionTest # noqa: F401 diff --git a/vertexai/agent_engines/_agent_engines.py b/vertexai/agent_engines/_agent_engines.py index b3bd0c94ca..fbba646e62 100644 --- a/vertexai/agent_engines/_agent_engines.py +++ b/vertexai/agent_engines/_agent_engines.py @@ -47,7 +47,6 @@ from google.cloud.aiplatform_v1.types import reasoning_engine_service from vertexai.agent_engines import _utils from vertexai._genai import _agent_engines_utils -import httpx import proto from google.protobuf import field_mask_pb2 @@ -116,6 +115,12 @@ except (ImportError, AttributeError): ADKAgent = None +try: + import httpx # pylint: disable=g-import-not-at-top +except ImportError: + # Only A2A operations use httpx, and a2a-sdk requires it. + httpx = None # type: ignore[assignment] + try: from a2a.types import ( AgentCard,