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]

Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_deflake_20260821

Devin AI3cd49b9e0c73
diff --cc litellm/proxy/spend_tracking/ptu_flat_cost_rollup.py
index a4eac992d89,6f1bbaa722b..0e6412a2c64
--- a/litellm/proxy/spend_tracking/ptu_flat_cost_rollup.py
+++ b/litellm/proxy/spend_tracking/ptu_flat_cost_rollup.py
@@@ -323,9 -324,18 +323,8 @@@ class _LoadedDeployments
models: tuple[PTUModel, ...]
scanned_ids: frozenset[str]
- config_sourced: bool
-def _running_router() -> object | None:
- """The proxy's router, or None outside a running proxy.
-
- Read out of ``sys.modules`` rather than imported, so a rollup driven from a test or a
- script does not pull the whole proxy server in behind it.
- """
- proxy_server: Final = sys.modules.get("litellm.proxy.proxy_server")
- return getattr(proxy_server, "llm_router", None) if proxy_server is not None else None
-
-
def _config_deployments(router: object | None, *, owned_by_db: frozenset[str]) -> tuple[_PTUDeployment, ...]:
"""Deployments the router holds that no ``LiteLLM_ProxyModelTable`` row owns.
diff --cc tests/test_litellm/proxy/spend_tracking/test_ptu_flat_cost_rollup.py
index 17a487ddd4b,dda2f5a4d73..76fa41c83be
--- a/tests/test_litellm/proxy/spend_tracking/test_ptu_flat_cost_rollup.py
+++ b/tests/test_litellm/proxy/spend_tracking/test_ptu_flat_cost_rollup.py
@@@ -2019,27 -2013,45 +2020,27 @@@ async def test_a_stale_database_backed_
assert loaded.models == ()
-def test_the_router_lookup_reads_the_proxys_own_global():
- """Every other config test replaces this helper, so without one test driving the real
- body a typo in the module path or the attribute name leaves the whole feature dead in
- production with the suite still green."""
- import sys
- import types as _types
-
- assert ptu_rollup._running_router() is None or "litellm.proxy.proxy_server" in sys.modules
-
- sentinel = object()
- stub = _types.SimpleNamespace(llm_router=sentinel)
- real = sys.modules.get("litellm.proxy.proxy_server")
- sys.modules["litellm.proxy.proxy_server"] = stub
- try:
- assert ptu_rollup._running_router() is sentinel
- del stub.llm_router
- assert ptu_rollup._running_router() is None
- finally:
- if real is None:
- del sys.modules["litellm.proxy.proxy_server"]
- else:
- sys.modules["litellm.proxy.proxy_server"] = real
+async def test_a_router_left_on_the_proxy_module_is_not_scanned(monkeypatch):
+ """A run scans the router its caller hands it and nothing else. Reading the proxy module's
+ global instead made every run depend on whatever else in the process had set one, which
+ is what a caller passing no router is asking not to happen."""
+ import litellm.proxy.proxy_server as proxy_server
+ ambient = _router_holding(_router_entry(model_id="ambient-1", model_info=dict(_VALID_PTU)))
+ monkeypatch.setattr(proxy_server, "llm_router", ambient, raising=False)
-def test_the_router_lookup_returns_none_outside_a_proxy():
- import sys
+ loaded = await ptu_rollup._load_ptu_models(_prisma_for([], _FakeSentinelTable()), router=None)
- real = sys.modules.pop("litellm.proxy.proxy_server", None)
- try:
- assert ptu_rollup._running_router() is None
- finally:
- if real is not None:
- sys.modules["litellm.proxy.proxy_server"] = real
+ assert loaded.models == ()
+ assert loaded.scanned_ids == frozenset()
+ assert loaded.config_sourced is False
- @pytest.mark.parametrize("chunk", [None, ("dep-a", "dep-b")], ids=["unbounded", "bounded"])
- def test_the_prune_filter_is_a_plain_dict(chunk):
+ def test_the_prune_filter_is_a_plain_dict():
"""The query builder serialises the mapping it is handed and rejects a read-only view of
one, which the in-memory table in these tests accepts happily. Only a live run caught it."""
+ chunk = ("dep-a", "dep-b")
predicate = ptu_rollup._prune_filter(date_str=DAY.isoformat(), cutoff=datetime.now(timezone.utc), chunk=chunk)
assert type(predicate) is dict