Skip to content
Open
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
8 changes: 6 additions & 2 deletions agentplatform/_genai/_runtimes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
Union,
)

import httpx

import proto

from google.api_core import exceptions
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/agentplatform/genai/test_agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/vertex_a2a/test_agent_engines_a2a.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#

import asyncio
import importlib.util
import os
import sys
import tempfile
Expand Down Expand Up @@ -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."""

Expand Down
8 changes: 6 additions & 2 deletions vertexai/_genai/_agent_engines_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
Union,
)

import httpx

import proto

from google.api_core import exceptions
Expand Down Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion vertexai/agent_engines/_agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Loading