mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-08 03:08:45 +00:00
feat(guardrails): warn when default_on env var resolves to None
Surface a verbose_proxy_logger warning when get_secret_bool returns
None for a default_on os.environ/ reference, so an unset variable
(e.g. a mistyped name) or an unrecognized value ("1", "yes", "on")
no longer silently leaves the guardrail disabled. Warns on the unset
case too, not just unrecognized values, since a referenced-but-missing
variable is the more common misconfiguration for the multi-environment
config use case this feature targets.
This commit is contained in:
parent
85c751c6b5
commit
ae852cf444
2 changed files with 35 additions and 5 deletions
|
|
@ -448,9 +448,17 @@ class InMemoryGuardrailHandler:
|
|||
if isinstance(default_on_raw, str) and default_on_raw.startswith(
|
||||
"os.environ/"
|
||||
):
|
||||
resolved_default_on = get_secret_bool(default_on_raw)
|
||||
if resolved_default_on is None:
|
||||
verbose_proxy_logger.warning(
|
||||
"Guardrail default_on env var %s could not be resolved "
|
||||
"to a bool (unset or value not 'true'/'false'); "
|
||||
"defaulting to False",
|
||||
default_on_raw,
|
||||
)
|
||||
litellm_params_data = {
|
||||
**litellm_params_data,
|
||||
"default_on": get_secret_bool(default_on_raw),
|
||||
"default_on": resolved_default_on,
|
||||
}
|
||||
litellm_params = LitellmParams(**litellm_params_data)
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -412,16 +412,38 @@ def test_initialize_guardrail_resolves_default_on_env_var_false(monkeypatch):
|
|||
assert callback.default_on is False
|
||||
|
||||
|
||||
def test_initialize_guardrail_unsets_default_on_env_var_falls_back_false(monkeypatch):
|
||||
def test_initialize_guardrail_unsets_default_on_env_var_falls_back_false(
|
||||
monkeypatch, caplog
|
||||
):
|
||||
captured: dict = {}
|
||||
monkeypatch.delenv("LITELLM_TEST_DEFAULT_ON_MISSING", raising=False)
|
||||
handler = _init_handler_with_fake_initializer(
|
||||
captured, "os.environ/LITELLM_TEST_DEFAULT_ON_MISSING"
|
||||
)
|
||||
import logging
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="LiteLLM Proxy"):
|
||||
handler = _init_handler_with_fake_initializer(
|
||||
captured, "os.environ/LITELLM_TEST_DEFAULT_ON_MISSING"
|
||||
)
|
||||
|
||||
assert captured["default_on"] is False
|
||||
callback = handler.guardrail_id_to_custom_guardrail["default-on-env-var"]
|
||||
assert callback.default_on is False
|
||||
assert "LITELLM_TEST_DEFAULT_ON_MISSING" in caplog.text
|
||||
|
||||
|
||||
def test_initialize_guardrail_unrecognized_default_on_env_value_warns(
|
||||
monkeypatch, caplog
|
||||
):
|
||||
captured: dict = {}
|
||||
monkeypatch.setenv("LITELLM_TEST_DEFAULT_ON_UNRECOGNIZED", "yes")
|
||||
import logging
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger="LiteLLM Proxy"):
|
||||
_init_handler_with_fake_initializer(
|
||||
captured, "os.environ/LITELLM_TEST_DEFAULT_ON_UNRECOGNIZED"
|
||||
)
|
||||
|
||||
assert captured["default_on"] is False
|
||||
assert "LITELLM_TEST_DEFAULT_ON_UNRECOGNIZED" in caplog.text
|
||||
|
||||
|
||||
def test_initialize_guardrail_non_prefixed_bool_string_not_env_resolved(monkeypatch):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue