Skip to content

Commit 4e85e9c

Browse files
haranrkcopybara-github
authored andcommitted
feat(utils): add GOOGLE_GENAI_USE_ENTERPRISE env var with deprecation fallback
Co-authored-by: Haran Rajkumar <haranrk@google.com> PiperOrigin-RevId: 929280981
1 parent 9127feb commit 4e85e9c

7 files changed

Lines changed: 135 additions & 15 deletions

File tree

src/google/adk/utils/env_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from __future__ import annotations
2222

2323
import os
24+
import warnings
2425

2526

2627
def is_env_enabled(env_var_name: str, default: str = '0') -> bool:
@@ -57,3 +58,22 @@ def is_env_enabled(env_var_name: str, default: str = '0') -> bool:
5758
True
5859
"""
5960
return os.environ.get(env_var_name, default).lower() in ['true', '1']
61+
62+
63+
def is_enterprise_mode_enabled() -> bool:
64+
"""Check if Google GenAI Enterprise mode is enabled via environment variables.
65+
66+
Returns:
67+
True if enabled, False otherwise.
68+
"""
69+
if 'GOOGLE_GENAI_USE_ENTERPRISE' in os.environ:
70+
return is_env_enabled('GOOGLE_GENAI_USE_ENTERPRISE')
71+
if 'GOOGLE_GENAI_USE_VERTEXAI' in os.environ:
72+
warnings.warn(
73+
'GOOGLE_GENAI_USE_VERTEXAI is deprecated, please use'
74+
' GOOGLE_GENAI_USE_ENTERPRISE instead',
75+
DeprecationWarning,
76+
stacklevel=2,
77+
)
78+
return is_env_enabled('GOOGLE_GENAI_USE_VERTEXAI')
79+
return False

src/google/adk/utils/variant_utils.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from enum import Enum
2424

25-
from .env_utils import is_env_enabled
25+
from .env_utils import is_enterprise_mode_enabled
2626

2727
_GOOGLE_LLM_VARIANT_VERTEX_AI = 'VERTEX_AI'
2828
_GOOGLE_LLM_VARIANT_GEMINI_API = 'GEMINI_API'
@@ -41,8 +41,6 @@ class GoogleLLMVariant(Enum):
4141

4242

4343
def get_google_llm_variant() -> GoogleLLMVariant:
44-
return (
45-
GoogleLLMVariant.VERTEX_AI
46-
if is_env_enabled('GOOGLE_GENAI_USE_VERTEXAI')
47-
else GoogleLLMVariant.GEMINI_API
48-
)
44+
if is_enterprise_mode_enabled():
45+
return GoogleLLMVariant.VERTEX_AI
46+
return GoogleLLMVariant.GEMINI_API

src/google/adk/utils/vertex_ai_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import os
2424
from typing import Optional
2525

26-
from ..utils.env_utils import is_env_enabled
26+
from .env_utils import is_enterprise_mode_enabled
2727

2828

2929
def get_express_mode_api_key(
@@ -37,7 +37,7 @@ def get_express_mode_api_key(
3737
'Cannot specify project or location and express_mode_api_key. '
3838
'Either use project and location, or just the express_mode_api_key.'
3939
)
40-
if is_env_enabled('GOOGLE_GENAI_USE_VERTEXAI'):
40+
if is_enterprise_mode_enabled():
4141
return express_mode_api_key or os.environ.get('GOOGLE_API_KEY', None)
4242
else:
4343
return None

tests/unittests/utils/test_env_utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import warnings
16+
17+
from google.adk.utils.env_utils import is_enterprise_mode_enabled
1518
from google.adk.utils.env_utils import is_env_enabled
1619
import pytest
1720

@@ -47,3 +50,35 @@ def test_is_env_enabled_with_defaults(monkeypatch, default, expected):
4750
"""Test is_env_enabled when env var is not set with different defaults."""
4851
monkeypatch.delenv('TEST_FLAG', raising=False)
4952
assert is_env_enabled('TEST_FLAG', default=default) is expected
53+
54+
55+
def test_is_enterprise_mode_enabled_via_enterprise_env(monkeypatch):
56+
"""Enterprise mode is on when GOOGLE_GENAI_USE_ENTERPRISE is truthy."""
57+
monkeypatch.setenv('GOOGLE_GENAI_USE_ENTERPRISE', 'true')
58+
59+
assert is_enterprise_mode_enabled() is True
60+
61+
62+
def test_is_enterprise_mode_enabled_falls_back_to_vertexai_with_warning(
63+
monkeypatch,
64+
):
65+
"""The deprecated GOOGLE_GENAI_USE_VERTEXAI still enables enterprise mode and warns."""
66+
monkeypatch.delenv('GOOGLE_GENAI_USE_ENTERPRISE', raising=False)
67+
monkeypatch.setenv('GOOGLE_GENAI_USE_VERTEXAI', 'true')
68+
69+
with warnings.catch_warnings(record=True) as caught:
70+
warnings.simplefilter('always')
71+
result = is_enterprise_mode_enabled()
72+
73+
assert result is True
74+
assert len(caught) == 1
75+
assert issubclass(caught[-1].category, DeprecationWarning)
76+
assert 'GOOGLE_GENAI_USE_VERTEXAI is deprecated' in str(caught[-1].message)
77+
78+
79+
def test_is_enterprise_mode_enabled_defaults_to_false(monkeypatch):
80+
"""Enterprise mode is off when no relevant env var is set."""
81+
monkeypatch.delenv('GOOGLE_GENAI_USE_ENTERPRISE', raising=False)
82+
monkeypatch.delenv('GOOGLE_GENAI_USE_VERTEXAI', raising=False)
83+
84+
assert is_enterprise_mode_enabled() is False

tests/unittests/utils/test_output_schema_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ def test_can_use_output_schema_with_tools(
6969
) -> None:
7070
"""Test can_use_output_schema_with_tools."""
7171
if env_value is not None:
72-
monkeypatch.setenv("GOOGLE_GENAI_USE_VERTEXAI", env_value)
72+
monkeypatch.setenv("GOOGLE_GENAI_USE_ENTERPRISE", env_value)
7373
else:
74-
monkeypatch.delenv("GOOGLE_GENAI_USE_VERTEXAI", raising=False)
74+
monkeypatch.delenv("GOOGLE_GENAI_USE_ENTERPRISE", raising=False)
7575
assert can_use_output_schema_with_tools(model) == expected
7676

7777

@@ -90,9 +90,9 @@ def test_can_use_output_schema_with_tools_claude(
9090
"""Test can_use_output_schema_with_tools with Claude models."""
9191
claude_model = _make_claude(model)
9292
if env_value is not None:
93-
monkeypatch.setenv("GOOGLE_GENAI_USE_VERTEXAI", env_value)
93+
monkeypatch.setenv("GOOGLE_GENAI_USE_ENTERPRISE", env_value)
9494
else:
95-
monkeypatch.delenv("GOOGLE_GENAI_USE_VERTEXAI", raising=False)
95+
monkeypatch.delenv("GOOGLE_GENAI_USE_ENTERPRISE", raising=False)
9696
assert can_use_output_schema_with_tools(claude_model) == expected
9797

9898

@@ -113,7 +113,7 @@ def test_can_use_output_schema_with_tools_litellm(
113113
"""Test can_use_output_schema_with_tools with LiteLLM models."""
114114
litellm_model = _make_litellm(model)
115115
if env_value is not None:
116-
monkeypatch.setenv("GOOGLE_GENAI_USE_VERTEXAI", env_value)
116+
monkeypatch.setenv("GOOGLE_GENAI_USE_ENTERPRISE", env_value)
117117
else:
118-
monkeypatch.delenv("GOOGLE_GENAI_USE_VERTEXAI", raising=False)
118+
monkeypatch.delenv("GOOGLE_GENAI_USE_ENTERPRISE", raising=False)
119119
assert can_use_output_schema_with_tools(litellm_model) == expected
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Tests for variant_utils."""
16+
17+
import warnings
18+
19+
from google.adk.utils import variant_utils
20+
from google.adk.utils.variant_utils import GoogleLLMVariant
21+
22+
23+
def test_get_google_llm_variant_enterprise(monkeypatch):
24+
monkeypatch.setenv('GOOGLE_GENAI_USE_ENTERPRISE', 'true')
25+
assert variant_utils.get_google_llm_variant() == GoogleLLMVariant.VERTEX_AI
26+
27+
28+
def test_get_google_llm_variant_vertexai_fallback(monkeypatch):
29+
monkeypatch.delenv('GOOGLE_GENAI_USE_ENTERPRISE', raising=False)
30+
monkeypatch.setenv('GOOGLE_GENAI_USE_VERTEXAI', 'true')
31+
with warnings.catch_warnings(record=True) as w:
32+
warnings.simplefilter('always')
33+
result = variant_utils.get_google_llm_variant()
34+
assert result == GoogleLLMVariant.VERTEX_AI
35+
assert len(w) == 1
36+
assert issubclass(w[-1].category, DeprecationWarning)
37+
assert 'GOOGLE_GENAI_USE_VERTEXAI is deprecated' in str(w[-1].message)
38+
39+
40+
def test_get_google_llm_variant_default(monkeypatch):
41+
monkeypatch.delenv('GOOGLE_GENAI_USE_ENTERPRISE', raising=False)
42+
monkeypatch.delenv('GOOGLE_GENAI_USE_VERTEXAI', raising=False)
43+
assert variant_utils.get_google_llm_variant() == GoogleLLMVariant.GEMINI_API

tests/unittests/utils/test_vertex_ai_utils.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Tests for vertex_utils."""
1616

1717
from unittest import mock
18+
import warnings
1819

1920
from google.adk.utils import vertex_ai_utils
2021
import pytest
@@ -77,7 +78,7 @@ def test_get_express_mode_api_key(
7778
):
7879
env_vars = {}
7980
if use_vertexai_env:
80-
env_vars['GOOGLE_GENAI_USE_VERTEXAI'] = use_vertexai_env
81+
env_vars['GOOGLE_GENAI_USE_ENTERPRISE'] = use_vertexai_env
8182
if google_api_key_env:
8283
env_vars['GOOGLE_API_KEY'] = google_api_key_env
8384
with mock.patch.dict('os.environ', env_vars, clear=True):
@@ -89,3 +90,26 @@ def test_get_express_mode_api_key(
8990
)
9091
== expected
9192
)
93+
94+
95+
def test_get_express_mode_api_key_enterprise(monkeypatch):
96+
monkeypatch.setenv('GOOGLE_GENAI_USE_ENTERPRISE', 'true')
97+
monkeypatch.setenv('GOOGLE_API_KEY', 'google_key')
98+
assert (
99+
vertex_ai_utils.get_express_mode_api_key(None, None, None) == 'google_key'
100+
)
101+
102+
103+
def test_get_express_mode_api_key_vertexai_fallback_warning(monkeypatch):
104+
monkeypatch.delenv('GOOGLE_GENAI_USE_ENTERPRISE', raising=False)
105+
monkeypatch.setenv('GOOGLE_GENAI_USE_VERTEXAI', 'true')
106+
monkeypatch.setenv('GOOGLE_API_KEY', 'google_key')
107+
# Should trigger a deprecation warning and return the key
108+
with warnings.catch_warnings(record=True) as w:
109+
warnings.simplefilter('always')
110+
result = vertex_ai_utils.get_express_mode_api_key(None, None, None)
111+
112+
assert result == 'google_key'
113+
assert len(w) == 1
114+
assert issubclass(w[-1].category, DeprecationWarning)
115+
assert 'GOOGLE_GENAI_USE_VERTEXAI is deprecated' in str(w[-1].message)

0 commit comments

Comments
 (0)