refactor: Adds a util method to check the enablement of any env variable

PiperOrigin-RevId: 825199260
This commit is contained in:
Wei Sun (Jack)
2025-10-28 14:12:01 -07:00
committed by Copybara-Service
parent 971eafab96
commit 61adfcd69a
5 changed files with 115 additions and 11 deletions
+2 -4
View File
@@ -27,6 +27,7 @@ from google.genai import Client
from google.genai import types
from typing_extensions import override
from ..utils.env_utils import is_env_enabled
from .google_llm import Gemini
if TYPE_CHECKING:
@@ -142,10 +143,7 @@ def _identify_vertexai(model: str) -> bool:
"""Returns True if the model spec starts with apigee/vertex_ai."""
return not model.startswith('apigee/gemini/') and (
model.startswith('apigee/vertex_ai/')
or os.environ.get(
_GOOGLE_GENAI_USE_VERTEXAI_ENV_VARIABLE_NAME, '0'
).lower()
in ['true', '1']
or is_env_enabled(_GOOGLE_GENAI_USE_VERTEXAI_ENV_VARIABLE_NAME)
)
@@ -35,6 +35,7 @@ import vertexai
from . import _session_util
from ..events.event import Event
from ..events.event_actions import EventActions
from ..utils.env_utils import is_env_enabled
from .base_session_service import BaseSessionService
from .base_session_service import GetSessionConfig
from .base_session_service import ListSessionsResponse
@@ -364,7 +365,7 @@ def _is_vertex_express_mode(
) -> bool:
"""Check if Vertex AI and API key are both enabled replacing project and location, meaning the user is using the Vertex Express Mode."""
return (
os.environ.get('GOOGLE_GENAI_USE_VERTEXAI', '0').lower() in ['true', '1']
is_env_enabled('GOOGLE_GENAI_USE_VERTEXAI')
and os.environ.get('GOOGLE_API_KEY', None) is not None
and project is None
and location is None
+59
View File
@@ -0,0 +1,59 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for environment variable handling.
This module is for ADK internal use only.
Please do not rely on the implementation details.
"""
from __future__ import annotations
import os
def is_env_enabled(env_var_name: str, default: str = '0') -> bool:
"""Check if an environment variable is enabled.
An environment variable is considered enabled if its value (case-insensitive)
is 'true' or '1'.
Args:
env_var_name: The name of the environment variable to check.
default: The default value to use if the environment variable is not set.
Defaults to '0'.
Returns:
True if the environment variable is enabled, False otherwise.
Examples:
>>> os.environ['MY_FLAG'] = 'true'
>>> is_env_enabled('MY_FLAG')
True
>>> os.environ['MY_FLAG'] = '1'
>>> is_env_enabled('MY_FLAG')
True
>>> os.environ['MY_FLAG'] = 'false'
>>> is_env_enabled('MY_FLAG')
False
>>> is_env_enabled('NONEXISTENT_FLAG')
False
>>> is_env_enabled('NONEXISTENT_FLAG', default='1')
True
"""
return os.environ.get(env_var_name, default).lower() in ['true', '1']
+3 -6
View File
@@ -21,7 +21,8 @@ Please do not rely on the implementation details.
from __future__ import annotations
from enum import Enum
import os
from .env_utils import is_env_enabled
_GOOGLE_LLM_VARIANT_VERTEX_AI = 'VERTEX_AI'
_GOOGLE_LLM_VARIANT_GEMINI_API = 'GEMINI_API'
@@ -42,10 +43,6 @@ class GoogleLLMVariant(Enum):
def get_google_llm_variant() -> GoogleLLMVariant:
return (
GoogleLLMVariant.VERTEX_AI
if os.environ.get('GOOGLE_GENAI_USE_VERTEXAI', '0').lower()
in [
'true',
'1',
]
if is_env_enabled('GOOGLE_GENAI_USE_VERTEXAI')
else GoogleLLMVariant.GEMINI_API
)
+49
View File
@@ -0,0 +1,49 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from google.adk.utils.env_utils import is_env_enabled
import pytest
@pytest.mark.parametrize(
'env_value,expected',
[
('true', True),
('TRUE', True),
('TrUe', True),
('1', True),
('false', False),
('FALSE', False),
('0', False),
('', False),
],
)
def test_is_env_enabled(monkeypatch, env_value, expected):
"""Test is_env_enabled with various environment variable values."""
monkeypatch.setenv('TEST_FLAG', env_value)
assert is_env_enabled('TEST_FLAG') is expected
@pytest.mark.parametrize(
'default,expected',
[
('0', False),
('1', True),
('true', True),
],
)
def test_is_env_enabled_with_defaults(monkeypatch, default, expected):
"""Test is_env_enabled when env var is not set with different defaults."""
monkeypatch.delenv('TEST_FLAG', raising=False)
assert is_env_enabled('TEST_FLAG', default=default) is expected