Skip to content

Commit 1e93b06

Browse files
yinghsienwucopybara-github
authored andcommitted
fix: Import pandas lazily in the Gen AI SDK _gcs_utils module.
fixes #5928 fixes #7129 FUTURE_COPYBARA_INTEGRATE_REVIEW=#7116 from googleapis:release-please--branches--main 3370e7f PiperOrigin-RevId: 974719338
1 parent 5631755 commit 1e93b06

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

‎agentplatform/_genai/_gcs_utils.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
import io
1818
import json
1919
import logging
20-
from typing import Any, Union
20+
from typing import Any, TYPE_CHECKING, Union
2121
import uuid
2222

2323
from google.cloud import storage # type: ignore[attr-defined]
2424
from google.genai._api_client import BaseApiClient
2525
from packaging.version import Version
26-
import pandas as pd
26+
27+
if TYPE_CHECKING:
28+
import pandas as pd
2729

2830

2931
logger = logging.getLogger(__name__)
@@ -211,6 +213,14 @@ def read_gcs_file_to_dataframe(
211213
self, gcs_filepath: str, file_type: str
212214
) -> "pd.DataFrame":
213215
"""Reads a file from Google Cloud Storage into a Pandas DataFrame."""
216+
try:
217+
import pandas as pd # pylint: disable=g-import-not-at-top
218+
except ImportError as e:
219+
raise ImportError(
220+
"Reading a Cloud Storage file into a DataFrame requires 'pandas'. "
221+
"Please install it using pip install "
222+
"google-cloud-aiplatform[evaluation]"
223+
) from e
214224
file_contents = self.read_file_contents(gcs_filepath)
215225
if file_type == "csv":
216226
return pd.read_csv(io.StringIO(file_contents), encoding="utf-8")

‎tests/unit/agentplatform/genai/test_genai_client.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
# pylint: disable=protected-access,bad-continuation
1717

18+
import contextlib
1819
import importlib
1920
import pytest
21+
import sys
2022
import warnings
2123
from unittest import mock
2224

@@ -31,6 +33,10 @@
3133
_TEST_PROJECT = "test-project"
3234
_TEST_LOCATION = "us-central1"
3335

36+
_AGENTPLATFORM_GCS_UTILS = "agentplatform._genai._gcs_utils"
37+
_AGENTPLATFORM_RAG = "agentplatform._genai.rag"
38+
_VERTEXAI_GCS_UTILS = "vertexai._genai._gcs_utils"
39+
3440

3541
pytestmark = pytest.mark.usefixtures("google_auth_mock")
3642

@@ -122,3 +128,63 @@ def test_vertexai_client_deprecation_warning(self):
122128
warnings.simplefilter("error", FutureWarning)
123129
_ = vertexai.Client(project=_TEST_PROJECT, location=_TEST_LOCATION)
124130
_ = vertexai.Client(project=_TEST_PROJECT, location=_TEST_LOCATION)
131+
132+
133+
@contextlib.contextmanager
134+
def _pandas_unavailable(reimported_modules=()):
135+
"""Makes `import pandas` fail, as it does when the extra is not installed.
136+
137+
Modules named in `reimported_modules` are evicted from the module cache and
138+
from their parent package, so that their top-level code runs again while
139+
pandas is unavailable. Evicting the parent package attribute matters because
140+
`from . import <submodule>` reads it in preference to re-importing.
141+
"""
142+
evicted = []
143+
for name in reimported_modules:
144+
parent_name, _, attribute = name.rpartition(".")
145+
importlib.import_module(name)
146+
parent = sys.modules[parent_name]
147+
evicted.append((parent, attribute, getattr(parent, attribute)))
148+
try:
149+
with mock.patch.dict(sys.modules):
150+
for name in list(sys.modules):
151+
if name == "pandas" or name.startswith("pandas."):
152+
del sys.modules[name]
153+
# A None entry makes the import machinery raise ModuleNotFoundError.
154+
sys.modules["pandas"] = None
155+
for name in reimported_modules:
156+
del sys.modules[name]
157+
for parent, attribute, _ in evicted:
158+
delattr(parent, attribute)
159+
yield
160+
finally:
161+
for parent, attribute, original in evicted:
162+
setattr(parent, attribute, original)
163+
164+
165+
class TestPandasIsOptional:
166+
"""pandas is only a dependency of the [evaluation] extra."""
167+
168+
@pytest.mark.parametrize(
169+
"module_name", [_AGENTPLATFORM_GCS_UTILS, _VERTEXAI_GCS_UTILS]
170+
)
171+
def test_gcs_utils_imports_without_pandas(self, module_name):
172+
with _pandas_unavailable([module_name]):
173+
importlib.import_module(module_name)
174+
175+
@pytest.mark.usefixtures("google_auth_mock")
176+
def test_rag_does_not_require_pandas(self):
177+
test_client = agentplatform.Client(
178+
project=_TEST_PROJECT, location=_TEST_LOCATION
179+
)
180+
with _pandas_unavailable([_AGENTPLATFORM_RAG, _AGENTPLATFORM_GCS_UTILS]):
181+
assert test_client.rag is not None
182+
183+
def test_read_gcs_file_to_dataframe_without_pandas_names_the_extra(self):
184+
gcs_utils_module = importlib.import_module(_AGENTPLATFORM_GCS_UTILS)
185+
with mock.patch.object(gcs_utils_module.storage, "Client", autospec=True):
186+
gcs_utils = gcs_utils_module.GcsUtils(mock.MagicMock())
187+
188+
with _pandas_unavailable():
189+
with pytest.raises(ImportError, match=r"\[evaluation\]"):
190+
gcs_utils.read_gcs_file_to_dataframe("gs://bucket/data.csv", "csv")

‎vertexai/_genai/_gcs_utils.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
import io
1717
import json
1818
import logging
19-
from typing import Any, Union
19+
from typing import Any, TYPE_CHECKING, Union
2020

2121
from google.cloud import storage # type: ignore[attr-defined]
2222
from google.cloud.aiplatform.utils.gcs_utils import blob_from_uri
2323
from google.genai._api_client import BaseApiClient
24-
import pandas as pd
2524
import uuid
2625

26+
if TYPE_CHECKING:
27+
import pandas as pd
28+
2729

2830
logger = logging.getLogger(__name__)
2931

@@ -180,6 +182,14 @@ def read_gcs_file_to_dataframe(
180182
self, gcs_filepath: str, file_type: str
181183
) -> "pd.DataFrame":
182184
"""Reads a file from Google Cloud Storage into a Pandas DataFrame."""
185+
try:
186+
import pandas as pd # pylint: disable=g-import-not-at-top
187+
except ImportError as e:
188+
raise ImportError(
189+
"Reading a Cloud Storage file into a DataFrame requires 'pandas'. "
190+
"Please install it using pip install "
191+
"google-cloud-aiplatform[evaluation]"
192+
) from e
183193
file_contents = self.read_file_contents(gcs_filepath)
184194
if file_type == "csv":
185195
return pd.read_csv(io.StringIO(file_contents), encoding="utf-8")

0 commit comments

Comments
 (0)