diff --git a/litellm/rust_bridge/configuration.py b/litellm/rust_bridge/configuration.py index 0edcffe972b..065431a51a4 100644 --- a/litellm/rust_bridge/configuration.py +++ b/litellm/rust_bridge/configuration.py @@ -6,9 +6,11 @@ from enum import Enum from types import MappingProxyType from typing import Final +from pydantic import TypeAdapter + DEFAULT_RUST_ENABLED: Final = False -_TRUE_ENV_VALUES: Final = frozenset({"1", "true", "yes", "on"}) _GLOBAL_ENV_NAME: Final = "LITELLM_RUST" +_ENV_BOOL: Final = TypeAdapter(bool) class RouteName(str, Enum): @@ -65,7 +67,7 @@ _CONFIGURATION: Final = _RustConfiguration() def _parse_env_bool(value: str | None) -> bool | None: if value is None: return None - return value.strip().lower() in _TRUE_ENV_VALUES + return _ENV_BOOL.validate_python(value.strip()) def resolve_rust_enabled( diff --git a/tests/test_litellm/rust_bridge/test_configuration_env.py b/tests/test_litellm/rust_bridge/test_configuration_env.py new file mode 100644 index 00000000000..0fab10e2bed --- /dev/null +++ b/tests/test_litellm/rust_bridge/test_configuration_env.py @@ -0,0 +1,27 @@ +from __future__ import annotations + +import pytest +from pydantic import ValidationError + +from litellm.rust_bridge.configuration import ( + _parse_env_bool, # pyright: ignore[reportPrivateUsage] # directly test env parsing contract +) + + +@pytest.mark.parametrize("value", ("1", "true", "t", "yes", "y", "on", "TRUE", " yes ")) +def test_parse_env_bool_accepts_standard_true_values(value: str) -> None: + assert _parse_env_bool(value) is True + + +@pytest.mark.parametrize("value", ("0", "false", "f", "no", "n", "off", "FALSE", " no ")) +def test_parse_env_bool_accepts_standard_false_values(value: str) -> None: + assert _parse_env_bool(value) is False + + +def test_parse_env_bool_preserves_unset_value() -> None: + assert _parse_env_bool(None) is None + + +def test_parse_env_bool_rejects_unknown_value() -> None: + with pytest.raises(ValidationError): + _parse_env_bool("enabled")