Skip to content

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]

fix(tests): isolate litellm.cache and CLI env vars in flaky tests (#21821)

Ishaan Jaffdd6a74da6352
diff --git a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py
index ad61d7ffc9b..6e34d275482 100644
--- a/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py
+++ b/tests/test_litellm/proxy/spend_tracking/test_spend_management_endpoints.py
@@ -1231,9 +1231,12 @@ async def _wait_for_mock_call(mock, timeout=10, interval=0.1):
class TestSpendLogsPayload:
def setup_method(self):
self._original_callbacks = litellm.callbacks[:]
+ self._original_cache = litellm.cache
+ litellm.cache = None
def teardown_method(self):
litellm.callbacks = self._original_callbacks
+ litellm.cache = self._original_cache
@pytest.mark.asyncio
async def test_spend_logs_payload_e2e(self):
diff --git a/tests/test_litellm/proxy/test_proxy_cli.py b/tests/test_litellm/proxy/test_proxy_cli.py
index e8a60e595ca..236080aab7e 100644
--- a/tests/test_litellm/proxy/test_proxy_cli.py
+++ b/tests/test_litellm/proxy/test_proxy_cli.py
@@ -586,9 +586,6 @@ class TestHealthAppFactory:
@patch("litellm.proxy.db.prisma_client.PrismaManager.setup_database")
@patch("litellm.proxy.db.check_migration.check_prisma_schema_diff")
@patch("litellm.proxy.db.prisma_client.should_update_prisma_schema")
- @patch.dict(
- os.environ, {"DATABASE_URL": "postgresql://test:test@localhost:5432/test"}
- )
def test_use_prisma_db_push_flag_behavior(
self,
mock_should_update_schema,
@@ -616,7 +613,19 @@ class TestHealthAppFactory:
save_worker_config=MagicMock(),
)
+ # Strip DIRECT_URL and set DATABASE_URL to a test value so Click's
+ # CliRunner doesn't encounter real DB env vars (causes stream lifecycle
+ # issues with Click 8.3.x in CI environments).
+ clean_env = {
+ k: v
+ for k, v in os.environ.items()
+ if k not in ("DATABASE_URL", "DIRECT_URL")
+ }
+ clean_env["DATABASE_URL"] = "postgresql://test:test@localhost:5432/test"
+
with patch.dict(
+ os.environ, clean_env, clear=True
+ ), patch.dict(
"sys.modules",
{
"proxy_server": mock_proxy_module,