mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +00:00
Since #35491, every Router joins the module-global _live_routers weak set at construction, and every model cost map swap replays the deployments of every member on top of the freshly adopted map. #36039 isolated the register_model ledger half of that replay but not this half: under pytest-xdist, a Router created by an earlier test in the same worker that was still referenced (or simply not yet garbage collected) re-registered its deployments during TestPriceDataReloadIntegration::test_distributed_reload_check_function, and register_model hydrated the sparse mocked gpt-3.5-turbo entry into a full ModelInfo dict, failing the exact-equality assert (reruns cannot help since the polluting router survives in the worker process) The autouse isolate_litellm_state fixture now snapshots _live_routers before each test and restores its membership on teardown, so a test's routers stop contributing to cost map rebuilds once the test ends. A canary pair in test_conftest_isolation.py asserts the rollback
36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
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
|
|
|
|
|
|
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
|