From 61adfcd69adf7980ceccfee943932ae34cb2025e Mon Sep 17 00:00:00 2001 From: "Wei Sun (Jack)" Date: Tue, 28 Oct 2025 14:12:01 -0700 Subject: [PATCH] refactor: Adds a util method to check the enablement of any env variable PiperOrigin-RevId: 825199260 --- src/google/adk/models/apigee_llm.py | 6 +- .../adk/sessions/vertex_ai_session_service.py | 3 +- src/google/adk/utils/env_utils.py | 59 +++++++++++++++++++ src/google/adk/utils/variant_utils.py | 9 +-- tests/unittests/utils/test_env_utils.py | 49 +++++++++++++++ 5 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 src/google/adk/utils/env_utils.py create mode 100644 tests/unittests/utils/test_env_utils.py diff --git a/src/google/adk/models/apigee_llm.py b/src/google/adk/models/apigee_llm.py index c93e5fe2..0229f976 100644 --- a/src/google/adk/models/apigee_llm.py +++ b/src/google/adk/models/apigee_llm.py @@ -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) ) diff --git a/src/google/adk/sessions/vertex_ai_session_service.py b/src/google/adk/sessions/vertex_ai_session_service.py index 4bd2ea36..48b112b6 100644 --- a/src/google/adk/sessions/vertex_ai_session_service.py +++ b/src/google/adk/sessions/vertex_ai_session_service.py @@ -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 diff --git a/src/google/adk/utils/env_utils.py b/src/google/adk/utils/env_utils.py new file mode 100644 index 00000000..bb37b658 --- /dev/null +++ b/src/google/adk/utils/env_utils.py @@ -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'] diff --git a/src/google/adk/utils/variant_utils.py b/src/google/adk/utils/variant_utils.py index 5de82b42..c0b4bc6e 100644 --- a/src/google/adk/utils/variant_utils.py +++ b/src/google/adk/utils/variant_utils.py @@ -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 ) diff --git a/tests/unittests/utils/test_env_utils.py b/tests/unittests/utils/test_env_utils.py new file mode 100644 index 00000000..954065a6 --- /dev/null +++ b/tests/unittests/utils/test_env_utils.py @@ -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