Skip to content

Commit 327bbe0

Browse files
sararobcopybara-github
authored andcommitted
feat: Add warning on vertexai.Client instantiation and recommend agentplatform.Client
PiperOrigin-RevId: 949154018
1 parent 9e80e1d commit 327bbe0

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

tests/unit/agentplatform/genai/test_genai_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717

1818
import importlib
1919
import pytest
20+
import warnings
2021
from unittest import mock
2122

2223
from google.cloud import aiplatform
2324
import agentplatform
2425
from agentplatform._genai import client as agentplatform_client
2526
from google.cloud.aiplatform import initializer as aiplatform_initializer
27+
import vertexai
28+
from vertexai._genai import client as vertexai_client
2629

2730

2831
_TEST_PROJECT = "test-project"
@@ -106,3 +109,16 @@ async def test_call_aclose_async_client(self):
106109
).aio
107110
await async_client.aclose()
108111
mock_aclose.assert_called()
112+
113+
@pytest.mark.usefixtures("google_auth_mock")
114+
def test_vertexai_client_deprecation_warning(self):
115+
116+
with mock.patch.object(vertexai_client, "_CLIENT_WARNING_SHOWN", False):
117+
# Assert that the warning is triggered on the first instantiation
118+
with pytest.warns(FutureWarning, match="The vertexai.Client class is deprecated"):
119+
_ = vertexai.Client(project=_TEST_PROJECT, location=_TEST_LOCATION)
120+
# Assert that the warning is NOT triggered on subsequent instantiations
121+
with warnings.catch_warnings():
122+
warnings.simplefilter("error", FutureWarning)
123+
_ = vertexai.Client(project=_TEST_PROJECT, location=_TEST_LOCATION)
124+
_ = vertexai.Client(project=_TEST_PROJECT, location=_TEST_LOCATION)

vertexai/_genai/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import asyncio
1717
import importlib
1818
import sys
19+
import warnings
1920
from typing import Optional, Union, TYPE_CHECKING
2021
from types import TracebackType, ModuleType
2122

@@ -43,6 +44,7 @@
4344

4445

4546
_GENAI_MODULES_TELEMETRY_HEADER = "vertex-genai-modules"
47+
_CLIENT_WARNING_SHOWN = False
4648

4749

4850
def _custom_append_library_version_headers(headers: dict[str, str]) -> None:
@@ -239,6 +241,14 @@ def __init__(
239241
http_options (Union[HttpOptions, HttpOptionsDict]): Http options to use
240242
for the client.
241243
"""
244+
global _CLIENT_WARNING_SHOWN
245+
if not _CLIENT_WARNING_SHOWN:
246+
warnings.warn(
247+
"The vertexai.Client class is deprecated. Please use agentplatform.Client instead.",
248+
FutureWarning,
249+
stacklevel=2,
250+
)
251+
_CLIENT_WARNING_SHOWN = True
242252

243253
self._debug_config = debug_config or genai_client.DebugConfig()
244254
if isinstance(http_options, dict):

0 commit comments

Comments
 (0)