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]

test: roll back live router replay membership between tests (#36278)

Mateo Wangf6df762b2537
diff --git a/tests/test_litellm/conftest.py b/tests/test_litellm/conftest.py
index c0993051d33..0dc8f56f3ce 100644
--- a/tests/test_litellm/conftest.py
+++ b/tests/test_litellm/conftest.py
@@ -19,6 +19,7 @@ sys.path.insert(
import asyncio
import litellm
+from litellm import router as litellm_router_module
from litellm import utils as litellm_utils_module
from litellm._logging import ALL_LOGGERS
from litellm.litellm_core_utils.prompt_templates import (
@@ -244,6 +245,8 @@ def isolate_litellm_state():
for model_key, model_value in litellm_utils_module._runtime_registered_model_cost.items()
}
+ original_live_routers = set(litellm_router_module._live_routers)
+
# Store LiteLLM logger state. Some tests reconfigure handlers/propagation for
# JSON logging and do not restore them, which breaks later caplog-based tests.
logger_state = {}
@@ -313,6 +316,11 @@ def isolate_litellm_state():
litellm_utils_module._runtime_registered_model_cost.clear()
litellm_utils_module._runtime_registered_model_cost.update(original_runtime_registered_model_cost)
+ for _router in tuple(litellm_router_module._live_routers):
+ litellm_router_module._live_routers.discard(_router)
+ for _router in original_live_routers:
+ litellm_router_module._live_routers.add(_router)
+
# Restore logger configuration mutated by logging-focused tests.
for logger in ALL_LOGGERS:
original_logger_state = logger_state.get(logger.name)
diff --git a/tests/test_litellm/test_conftest_isolation.py b/tests/test_litellm/test_conftest_isolation.py
index 88889ad7740..15183e68f66 100644
--- a/tests/test_litellm/test_conftest_isolation.py
+++ b/tests/test_litellm/test_conftest_isolation.py
@@ -1,9 +1,15 @@
import litellm
+from litellm import Router
+from litellm import router as litellm_router_module
from litellm import utils as litellm_utils_module
CANARY_MODEL = "conftest-isolation-canary-model"
+class _CanaryRouterHolder:
+ router: Router | None = None
+
+
def test_register_model_ledger_entry_is_scoped_to_this_test():
litellm.register_model({CANARY_MODEL: {"litellm_provider": "openai", "input_cost_per_token": 0.001}})
assert CANARY_MODEL in litellm_utils_module._runtime_registered_model_cost
@@ -11,3 +17,20 @@ def test_register_model_ledger_entry_is_scoped_to_this_test():
def test_register_model_ledger_entry_was_rolled_back():
assert CANARY_MODEL not in litellm_utils_module._runtime_registered_model_cost
+
+
+def test_live_router_membership_is_scoped_to_this_test():
+ _CanaryRouterHolder.router = Router(
+ model_list=[
+ {
+ "model_name": "conftest-isolation-canary-router",
+ "litellm_params": {"model": "openai/conftest-isolation-canary-backend", "api_key": "sk-canary"},
+ }
+ ]
+ )
+ assert _CanaryRouterHolder.router in litellm_router_module._live_routers
+
+
+def test_live_router_membership_was_rolled_back():
+ assert _CanaryRouterHolder.router is not None
+ assert _CanaryRouterHolder.router not in litellm_router_module._live_routers