mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-08 22:21:35 +00:00
fix(init): keep non-flag LITELLM_DROP_PARAMS values on with a warning
The merge base read the variable by truthiness, so any non-empty value turned the global flag on. Parsing it as a flag made a value such as temperature or enabled silently turn it off, and the only docs for the variable describe it as a list of parameter names, so keep those values on and log a warning that asks for true or false. A blank value stays off without a warning
This commit is contained in:
parent
d594b9385e
commit
704013dbb6
4 changed files with 53 additions and 6 deletions
|
|
@ -47,7 +47,7 @@ from typing import (
|
|||
)
|
||||
from litellm.types.integrations.datadog import DatadogInitParams
|
||||
from litellm.types.integrations.newrelic import NewRelicInitParams
|
||||
from litellm.litellm_core_utils.core_helpers import drop_params_flag
|
||||
from litellm.litellm_core_utils.core_helpers import drop_params_env_flag
|
||||
from litellm._logging import (
|
||||
set_verbose,
|
||||
_turn_on_debug,
|
||||
|
|
@ -239,7 +239,7 @@ token: Optional[str] = (
|
|||
)
|
||||
telemetry = True
|
||||
max_tokens: int = DEFAULT_MAX_TOKENS # OpenAI Defaults
|
||||
drop_params = drop_params_flag(os.getenv("LITELLM_DROP_PARAMS"), "LITELLM_DROP_PARAMS", verbose_logger)
|
||||
drop_params = drop_params_env_flag(os.environ, verbose_logger)
|
||||
modify_params = bool(os.getenv("LITELLM_MODIFY_PARAMS", False))
|
||||
use_chat_completions_url_for_anthropic_messages: bool = bool(
|
||||
os.getenv("LITELLM_USE_CHAT_COMPLETIONS_URL_FOR_ANTHROPIC_MESSAGES", False)
|
||||
|
|
|
|||
|
|
@ -58,6 +58,22 @@ def drop_params_flag(value: object, source: str, logger: logging.Logger) -> bool
|
|||
return bool(normalized)
|
||||
|
||||
|
||||
DROP_PARAMS_ENV_VAR: Final = "LITELLM_DROP_PARAMS"
|
||||
|
||||
|
||||
def drop_params_env_flag(environ: Mapping[str, str], logger: logging.Logger) -> bool:
|
||||
configured: Final = environ.get(DROP_PARAMS_ENV_VAR, "").strip()
|
||||
if configured == "":
|
||||
return False
|
||||
normalized: Final = normalize_drop_params(configured)
|
||||
if normalized is None:
|
||||
logger.warning(
|
||||
"%s=%r is not a flag value, treating it as on. Set it to true or false", DROP_PARAMS_ENV_VAR, configured
|
||||
)
|
||||
return True
|
||||
return normalized
|
||||
|
||||
|
||||
def safe_divide(
|
||||
numerator: float,
|
||||
denominator: float,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import pytest
|
|||
|
||||
from litellm.litellm_core_utils.core_helpers import (
|
||||
_FINISH_REASON_MAP,
|
||||
drop_params_env_flag,
|
||||
drop_params_flag,
|
||||
get_or_create_metadata_bucket,
|
||||
map_finish_reason,
|
||||
|
|
@ -301,6 +302,33 @@ def test_drop_params_flag_treats_non_flag_values_as_off_with_a_warning(value, ca
|
|||
assert f"LITELLM_DROP_PARAMS={value!r} is not a flag value, treating it as off" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"environ, expected",
|
||||
[
|
||||
({}, False),
|
||||
({"LITELLM_DROP_PARAMS": ""}, False),
|
||||
({"LITELLM_DROP_PARAMS": " "}, False),
|
||||
({"LITELLM_DROP_PARAMS": "true"}, True),
|
||||
({"LITELLM_DROP_PARAMS": " False "}, False),
|
||||
({"LITELLM_DROP_PARAMS": "0"}, False),
|
||||
],
|
||||
)
|
||||
def test_drop_params_env_flag_reads_a_flag_without_a_warning(environ, expected, caplog):
|
||||
with caplog.at_level(logging.WARNING, logger="drop-params-test"):
|
||||
assert drop_params_env_flag(environ, logging.getLogger("drop-params-test")) is expected
|
||||
assert caplog.text == ""
|
||||
|
||||
|
||||
@pytest.mark.parametrize("configured", ["temperature", "temperature,top_p", "enabled"])
|
||||
def test_drop_params_env_flag_keeps_a_non_flag_value_on_with_a_warning(configured, caplog):
|
||||
with caplog.at_level(logging.WARNING, logger="drop-params-test"):
|
||||
assert drop_params_env_flag({"LITELLM_DROP_PARAMS": configured}, logging.getLogger("drop-params-test")) is True
|
||||
assert (
|
||||
f"LITELLM_DROP_PARAMS={configured!r} is not a flag value, treating it as on. Set it to true or false"
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
|
||||
class TestIsExpectedClientError:
|
||||
def test_status_ranges(self):
|
||||
from litellm.litellm_core_utils.core_helpers import is_expected_client_error
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ def _import_litellm_with(configured: str) -> subprocess.CompletedProcess[str]:
|
|||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("configured, expected", [("false", "False"), ("true", "True")])
|
||||
@pytest.mark.parametrize("configured, expected", [("false", "False"), ("true", "True"), ("", "False")])
|
||||
def test_litellm_drop_params_env_var_is_parsed_as_a_flag(configured, expected):
|
||||
result = _import_litellm_with(configured)
|
||||
|
||||
|
|
@ -23,8 +23,11 @@ def test_litellm_drop_params_env_var_is_parsed_as_a_flag(configured, expected):
|
|||
assert "is not a flag value" not in result.stderr
|
||||
|
||||
|
||||
def test_litellm_drop_params_env_var_non_flag_value_is_off_with_a_warning():
|
||||
def test_litellm_drop_params_env_var_non_flag_value_stays_on_with_a_warning():
|
||||
result = _import_litellm_with("temperature")
|
||||
|
||||
assert result.stdout.strip() == "False"
|
||||
assert "LITELLM_DROP_PARAMS='temperature' is not a flag value, treating it as off" in result.stderr
|
||||
assert result.stdout.strip() == "True"
|
||||
assert (
|
||||
"LITELLM_DROP_PARAMS='temperature' is not a flag value, treating it as on. Set it to true or false"
|
||||
in result.stderr
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue