mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-13 03:59:12 +00:00
feat(sdk): add LITELLM_DISABLE_DOTENV opt-out for implicit .env loading
This commit is contained in:
parent
77813ae906
commit
b047841bf2
3 changed files with 40 additions and 2 deletions
|
|
@ -26,7 +26,14 @@ def _dev_env_hot_reload_enabled() -> bool:
|
|||
return os.getenv("LITELLM_DEV_ENV_HOT_RELOAD") == "True"
|
||||
|
||||
|
||||
if os.getenv("LITELLM_MODE", "DEV") == "DEV":
|
||||
def _should_load_dotenv() -> bool:
|
||||
"""Check if dotenv should be implicitly loaded (skipped in PRODUCTION or when LITELLM_DISABLE_DOTENV is set)."""
|
||||
if os.getenv("LITELLM_DISABLE_DOTENV", "").lower() in ("1", "true", "t", "yes", "y"):
|
||||
return False
|
||||
return os.getenv("LITELLM_MODE", "DEV") == "DEV"
|
||||
|
||||
|
||||
if _should_load_dotenv():
|
||||
_dotenv.load_dotenv(override=_dev_env_hot_reload_enabled())
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
|
|
|||
|
|
@ -46,7 +46,8 @@ sys.path.append(os.getcwd())
|
|||
config_filename: Final = "litellm.secrets"
|
||||
|
||||
litellm_mode: Final = os.getenv("LITELLM_MODE", "DEV") # "PRODUCTION", "DEV"
|
||||
if litellm_mode == "DEV":
|
||||
disable_dotenv: Final = os.getenv("LITELLM_DISABLE_DOTENV", "").lower() in ("1", "true", "t", "yes", "y")
|
||||
if litellm_mode == "DEV" and not disable_dotenv:
|
||||
load_dotenv()
|
||||
from enum import Enum
|
||||
|
||||
|
|
|
|||
30
tests/test_dotenv_opt_out.py
Normal file
30
tests/test_dotenv_opt_out.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import os
|
||||
from unittest.mock import patch
|
||||
|
||||
from litellm import _should_load_dotenv
|
||||
|
||||
|
||||
def test_should_load_dotenv_defaults():
|
||||
"""By default in DEV mode, dotenv should load."""
|
||||
with patch.dict(os.environ, {"LITELLM_MODE": "DEV", "LITELLM_DISABLE_DOTENV": ""}, clear=True):
|
||||
assert _should_load_dotenv() is True
|
||||
|
||||
|
||||
def test_should_load_dotenv_production():
|
||||
"""In PRODUCTION mode, dotenv should not load."""
|
||||
with patch.dict(os.environ, {"LITELLM_MODE": "PRODUCTION", "LITELLM_DISABLE_DOTENV": ""}, clear=True):
|
||||
assert _should_load_dotenv() is False
|
||||
|
||||
|
||||
def test_should_load_dotenv_disabled():
|
||||
"""When LITELLM_DISABLE_DOTENV is truthy, dotenv should not load even in DEV mode."""
|
||||
for truthy_val in ("1", "true", "True", "t", "T", "yes", "YES", "y", "Y"):
|
||||
with patch.dict(os.environ, {"LITELLM_MODE": "DEV", "LITELLM_DISABLE_DOTENV": truthy_val}, clear=True):
|
||||
assert _should_load_dotenv() is False, f"Failed for LITELLM_DISABLE_DOTENV={truthy_val}"
|
||||
|
||||
|
||||
def test_should_load_dotenv_falsy():
|
||||
"""When LITELLM_DISABLE_DOTENV is explicitly false, dotenv should load in DEV mode."""
|
||||
for falsy_val in ("0", "false", "False", "no", "NO"):
|
||||
with patch.dict(os.environ, {"LITELLM_MODE": "DEV", "LITELLM_DISABLE_DOTENV": falsy_val}, clear=True):
|
||||
assert _should_load_dotenv() is True, f"Failed for LITELLM_DISABLE_DOTENV={falsy_val}"
|
||||
Loading…
Add table
Reference in a new issue