Skip to content

Commit 61adfcd

Browse files
Jacksunweicopybara-github
authored andcommitted
refactor: Adds a util method to check the enablement of any env variable
PiperOrigin-RevId: 825199260
1 parent 971eafa commit 61adfcd

File tree

5 files changed

+115
-11
lines changed

5 files changed

+115
-11
lines changed

src/google/adk/models/apigee_llm.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from google.genai import types
2828
from typing_extensions import override
2929

30+
from ..utils.env_utils import is_env_enabled
3031
from .google_llm import Gemini
3132

3233
if TYPE_CHECKING:
@@ -142,10 +143,7 @@ def _identify_vertexai(model: str) -> bool:
142143
"""Returns True if the model spec starts with apigee/vertex_ai."""
143144
return not model.startswith('apigee/gemini/') and (
144145
model.startswith('apigee/vertex_ai/')
145-
or os.environ.get(
146-
_GOOGLE_GENAI_USE_VERTEXAI_ENV_VARIABLE_NAME, '0'
147-
).lower()
148-
in ['true', '1']
146+
or is_env_enabled(_GOOGLE_GENAI_USE_VERTEXAI_ENV_VARIABLE_NAME)
149147
)
150148

151149

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from . import _session_util
3636
from ..events.event import Event
3737
from ..events.event_actions import EventActions
38+
from ..utils.env_utils import is_env_enabled
3839
from .base_session_service import BaseSessionService
3940
from .base_session_service import GetSessionConfig
4041
from .base_session_service import ListSessionsResponse
@@ -364,7 +365,7 @@ def _is_vertex_express_mode(
364365
) -> bool:
365366
"""Check if Vertex AI and API key are both enabled replacing project and location, meaning the user is using the Vertex Express Mode."""
366367
return (
367-
os.environ.get('GOOGLE_GENAI_USE_VERTEXAI', '0').lower() in ['true', '1']
368+
is_env_enabled('GOOGLE_GENAI_USE_VERTEXAI')
368369
and os.environ.get('GOOGLE_API_KEY', None) is not None
369370
and project is None
370371
and location is None

src/google/adk/utils/env_utils.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
"""Utilities for environment variable handling.
16+
17+
This module is for ADK internal use only.
18+
Please do not rely on the implementation details.
19+
"""
20+
21+
from __future__ import annotations
22+
23+
import os
24+
25+
26+
def is_env_enabled(env_var_name: str, default: str = '0') -> bool:
27+
"""Check if an environment variable is enabled.
28+
29+
An environment variable is considered enabled if its value (case-insensitive)
30+
is 'true' or '1'.
31+
32+
Args:
33+
env_var_name: The name of the environment variable to check.
34+
default: The default value to use if the environment variable is not set.
35+
Defaults to '0'.
36+
37+
Returns:
38+
True if the environment variable is enabled, False otherwise.
39+
40+
Examples:
41+
>>> os.environ['MY_FLAG'] = 'true'
42+
>>> is_env_enabled('MY_FLAG')
43+
True
44+
45+
>>> os.environ['MY_FLAG'] = '1'
46+
>>> is_env_enabled('MY_FLAG')
47+
True
48+
49+
>>> os.environ['MY_FLAG'] = 'false'
50+
>>> is_env_enabled('MY_FLAG')
51+
False
52+
53+
>>> is_env_enabled('NONEXISTENT_FLAG')
54+
False
55+
56+
>>> is_env_enabled('NONEXISTENT_FLAG', default='1')
57+
True
58+
"""
59+
return os.environ.get(env_var_name, default).lower() in ['true', '1']

src/google/adk/utils/variant_utils.py

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

2323
from enum import Enum
24-
import os
24+
25+
from .env_utils import is_env_enabled
2526

2627
_GOOGLE_LLM_VARIANT_VERTEX_AI = 'VERTEX_AI'
2728
_GOOGLE_LLM_VARIANT_GEMINI_API = 'GEMINI_API'
@@ -42,10 +43,6 @@ class GoogleLLMVariant(Enum):
4243
def get_google_llm_variant() -> GoogleLLMVariant:
4344
return (
4445
GoogleLLMVariant.VERTEX_AI
45-
if os.environ.get('GOOGLE_GENAI_USE_VERTEXAI', '0').lower()
46-
in [
47-
'true',
48-
'1',
49-
]
46+
if is_env_enabled('GOOGLE_GENAI_USE_VERTEXAI')
5047
else GoogleLLMVariant.GEMINI_API
5148
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
from google.adk.utils.env_utils import is_env_enabled
16+
import pytest
17+
18+
19+
@pytest.mark.parametrize(
20+
'env_value,expected',
21+
[
22+
('true', True),
23+
('TRUE', True),
24+
('TrUe', True),
25+
('1', True),
26+
('false', False),
27+
('FALSE', False),
28+
('0', False),
29+
('', False),
30+
],
31+
)
32+
def test_is_env_enabled(monkeypatch, env_value, expected):
33+
"""Test is_env_enabled with various environment variable values."""
34+
monkeypatch.setenv('TEST_FLAG', env_value)
35+
assert is_env_enabled('TEST_FLAG') is expected
36+
37+
38+
@pytest.mark.parametrize(
39+
'default,expected',
40+
[
41+
('0', False),
42+
('1', True),
43+
('true', True),
44+
],
45+
)
46+
def test_is_env_enabled_with_defaults(monkeypatch, default, expected):
47+
"""Test is_env_enabled when env var is not set with different defaults."""
48+
monkeypatch.delenv('TEST_FLAG', raising=False)
49+
assert is_env_enabled('TEST_FLAG', default=default) is expected

0 commit comments

Comments
 (0)