The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): warn at boot when a config environment_variables key replaces a value already set in the process environment (#47165)
Kerry Lu1552b5b69f71
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 3ed3c57a96c..dba4b987725 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -6535,10 +6535,13 @@ class ProxyConfig:environment_variables: Final = config.get("environment_variables", None)if environment_variables:for key, value in environment_variables.items():+ if not isinstance(key, str):+ verbose_proxy_logger.warning("Skipping an environment_variables key that is not a string")+ continueif key in self._BLOCKED_ENV_KEYS:verbose_proxy_logger.warning("Skipping blocked environment variable key: %s", key)continue- if isinstance(key, str) and is_mcp_stdio_flag_key(key):+ if is_mcp_stdio_flag_key(key):verbose_proxy_logger.warning("Ignoring %s set in the config file. Set it in the proxy's environment instead",MCP_STDIO_ENABLED_ENV_VAR,@@ -6554,7 +6557,7 @@ class ProxyConfig:if isinstance(value, str) and value.startswith("os.environ/"):resolved_secret_string: str | None = get_secret_str(secret_name=value)if resolved_secret_string is not None:- os.environ[key] = resolved_secret_string+ self._set_config_environment_variable(key=key, value=resolved_secret_string)else:########################################################## handles this scenario:@@ -6563,13 +6566,21 @@ class ProxyConfig:# ARIZE_ENDPOINT: https://otlp.arize.com/v1# ```#########################################################- os.environ[key] = str(value)+ self._set_config_environment_variable(key=key, value=str(value))# check if litellm_license in general_settingsif "LITELLM_LICENSE" in environment_variables:_license_check.license_str = os.getenv("LITELLM_LICENSE", None)premium_user = _license_check.is_premium()+ def _set_config_environment_variable(self, key: str, value: str) -> None:+ if key in os.environ and os.environ[key] != value:+ verbose_proxy_logger.warning(+ "environment_variables.%s from the config file replaces the value already set in the process environment",+ key,+ )+ os.environ[key] = value+def _warn_on_misplaced_jwt_keys(self, config: dict) -> tuple[str, ...]:misplaced_jwt_keys = tuple(key for key in ("enable_jwt_auth", "litellm_jwtauth") if key in config)if not misplaced_jwt_keys:diff --git a/tests/unit/proxy/proxy_server/test_proxy_config.py b/tests/unit/proxy/proxy_server/test_proxy_config.pyindex 51c84618a74..8837fa095e8 100644--- a/tests/unit/proxy/proxy_server/test_proxy_config.py+++ b/tests/unit/proxy/proxy_server/test_proxy_config.py@@ -2381,6 +2381,30 @@ def test_ProxyConfig__load_environment_variables_sets_env(monkeypatch):assert result == {"TEST_LOAD_ENV_X": "hello", "set": True, "len": 1}+def test_ProxyConfig__load_environment_variables_warns_when_the_config_replaces_a_process_value(+ monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture+):+ monkeypatch.setenv("TEST_LOAD_ENV_OVERRIDDEN", "from-process")+ monkeypatch.setenv("TEST_LOAD_ENV_SAME", "same-value")+ monkeypatch.delenv("TEST_LOAD_ENV_FRESH", raising=False)+ with caplog.at_level(logging.WARNING, logger="LiteLLM Proxy"):+ ProxyConfig()._load_environment_variables(+ {+ "environment_variables": {+ "TEST_LOAD_ENV_OVERRIDDEN": "from-config",+ "TEST_LOAD_ENV_SAME": "same-value",+ "TEST_LOAD_ENV_FRESH": "fresh-value",+ }+ }+ )+ warnings = [record.getMessage() for record in caplog.records if record.levelno == logging.WARNING]+ assert os.environ["TEST_LOAD_ENV_OVERRIDDEN"] == "from-config"+ assert len(warnings) == 1+ assert "TEST_LOAD_ENV_OVERRIDDEN" in warnings[0]+ assert "from-process" not in warnings[0] and "from-config" not in warnings[0]+ assert "TEST_LOAD_ENV_SAME" not in warnings[0] and "TEST_LOAD_ENV_FRESH" not in warnings[0]++def test_ProxyConfig__load_environment_variables_blocks_dangerous_keys(monkeypatch):original_path = os.environ.get("PATH", "")pc = ProxyConfig()