litellm/tests/unit/test_router/test_router_provider_endpoints.py
ekpog200 ba61ecdf57
fix(videos)!: keep model_id in proxy video ids so per-model keys resolve on status/content (#44614)
* fix(videos): keep model_id in proxy video ids so per-model keys resolve on status/content

Provider transforms encode returned video ids with an empty model_id, so
GET /v1/videos/{id} and /content cannot resolve the deployment (and its
per-model api_key) and fail with invalid_api_key.

Re-stamp the returned VideoObject id with the resolved model_id in
video_generation, video_status, video_remix, video_edit and
video_extension.

Fixes #33423

* fix(videos): return a copy instead of mutating the response, read hidden params via helper

* test(videos): fully type the new video id re-stamping tests

Annotate every parameter and return added by this PR (hidden_params,
harness, response, -> None), drop Any from the test helper, mark locals
Final, and narrow the endpoint/helper results with isinstance(VideoObject)
instead of reading .id off an untyped value. Route the mocked processor
response through a typed Harness.respond_with, read NotRequired
DecodedVideoId keys with .get, and name the rule and reason on the
_hidden_params suppressions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(videos): fill only an empty model in returned video ids

Keep an id that already names a model as is, so the asked-for name,
routing group and served group in it still drive access checks and
routing. Fill an empty model from the model the caller asked for
before the routed deployment id, and read the request model through a
typed view so the type gate does not grow

* fix(videos): route video calls to the deployment that created the video

Video ids now carry the id of the deployment that created them. Status,
content, remix, edit and extension calls go to that deployment when it
belongs to the requested group, so a group spread over two provider
accounts no longer polls the wrong one. A provider model shared by
several groups resolves to the creating deployment's group, and the
asked-for model is written into an id only when the router can route it.

* fix(videos): pin video calls only to deployments the caller may use

Pin a video id's creating deployment at deployment selection, and only when the caller may use that
deployment in the requested group, so aliases, routing groups and access groups still apply and the
spend label and fallbacks keep the requested group.

A status poll keeps the deployment its id names and takes the deployment that answered when the id
names none. An alias that routes to the creating deployment's group is kept on status and content.

* test(router): reset module fallback globals between unit tests

* fix(videos): check and route polls by the name the router serves

A poll on a video created through a provider wildcard is checked against the name the wildcard serves, a team model is checked by its public name, a poll stamps the deployment that answered, and the sync Router pins video calls the same way the async path does

* fix(videos): pin polls only among the deployments routing allows

Video calls route by group again and the creating deployment is picked only from the deployments left after access, team, health, cooldown, budget and tag filters, so a pinned poll no longer skips them. Wildcard groups now pin too. Adds tests for cooldown, wildcard pinning and character lookups through a provider wildcard, and return types on the auth tests

* fix(videos): scope the video pin to video calls, keep fallback-served videos reachable, skip cooldown on video 404s

* fix(videos): stamp the requested group only when one of its router fallbacks served the create

* fix(videos): follow chained router fallbacks for fallback-served videos, test with a real Router

* test(router): list the video pin helper with its sibling in the router coverage exemptions

* refactor(auth): walk chained router fallbacks without recursion

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-20 05:42:59 +00:00

975 lines
36 KiB
Python

import asyncio
import contextlib
import io
import json
import uuid
from collections.abc import Callable, Iterator
from datetime import datetime, timezone
from typing import Final
import httpx
import litellm
import pytest
import respx
from litellm import CustomLogger, Router
from litellm.caching.dual_cache import DualCache
from litellm.caching.in_memory_cache import InMemoryCache
from litellm.constants import ROUTER_USAGE_COUNTED_TOKENS_METADATA_KEY
from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper
from litellm.proxy._types import UserAPIKeyAuth
from litellm.router_utils.router_callbacks.track_deployment_metrics import (
get_deployment_successes_for_current_minute,
)
from litellm.types.caching import RedisPipelineIncrementOperation
from litellm.types.llms.openai import HttpxBinaryResponseContent
from litellm.types.router import RoutingStrategy
from litellm.types.utils import ImageResponse, ModelResponse, RerankResponse
from litellm.types.videos.utils import encode_video_id_with_provider
from openai import AsyncAzureOpenAI, AsyncOpenAI
EVENT_TIMEOUT_SECONDS: Final = 5
ROUTING_SELECTIONS: Final = 40
ROUTING_MESSAGES: Final = ({"role": "user", "content": "route this request"},)
EXPENSIVE_COSTS: Final = {"input_cost_per_token": 1.0, "output_cost_per_token": 1.0}
CHEAP_COSTS: Final = {"input_cost_per_token": 1e-9, "output_cost_per_token": 1e-9}
CHAT_RESPONSE: Final = {
"id": "chatcmpl-1",
"object": "chat.completion",
"created": 1700000000,
"model": "gpt-4o-mini",
"choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 5, "completion_tokens": 1, "total_tokens": 6},
}
@pytest.fixture(autouse=True)
def httpx_transport(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
litellm.in_memory_llm_clients_cache.flush_cache()
yield
litellm.in_memory_llm_clients_cache.flush_cache()
@pytest.fixture
def router_minute_pinned(monkeypatch: pytest.MonkeyPatch) -> None:
pinned: Final = datetime(2026, 1, 1, 12, 0, 30, tzinfo=timezone.utc)
monkeypatch.setattr("litellm.router.get_utc_datetime", lambda: pinned)
class _RouterLoggingCapture(CustomLogger):
def __init__(self, model_id: str) -> None:
super().__init__()
self.model_id: Final = model_id
self.success_events: asyncio.Queue[tuple[object | None, object | None]] = asyncio.Queue()
async def async_log_success_event(
self,
kwargs: dict[str, object],
response_obj: object,
start_time: object,
end_time: object,
) -> None:
standard_logging_object: Final = kwargs.get("standard_logging_object")
if not isinstance(standard_logging_object, dict) or standard_logging_object.get("model_id") != self.model_id:
return
self.success_events.put_nowait((kwargs.get("client"), standard_logging_object))
async def next_event(self) -> tuple[object | None, object | None]:
return await asyncio.wait_for(self.success_events.get(), timeout=EVENT_TIMEOUT_SECONDS)
def _rpm_tpm_router(model_id: str) -> Router:
return Router(
model_list=[
{
"model_name": "gpt-5-mini",
"litellm_params": {"model": "gpt-5-mini", "api_key": "sk-fake", "tpm": 1000, "rpm": 100},
"model_info": {"id": model_id},
}
]
)
def _ratelimit_headers(response: ModelResponse | CustomStreamWrapper) -> dict[str, int]:
return {k: v for k, v in response._hidden_params["additional_headers"].items() if k.startswith("x-ratelimit-")}
def _openai_router_client() -> AsyncOpenAI:
return AsyncOpenAI(api_key="sk-fake")
def _azure_router_client() -> AsyncAzureOpenAI:
return AsyncAzureOpenAI(api_key="sk-fake", azure_endpoint="https://azure.test", api_version="2025-02-01-preview")
@pytest.mark.asyncio
@pytest.mark.parametrize(
("litellm_params", "build_client", "route_url"),
[
(
{"model": "whisper-1", "api_key": "sk-fake"},
_openai_router_client,
"https://api.openai.com/v1/audio/transcriptions",
),
(
{
"model": "azure/whisper",
"api_base": "https://azure.test",
"api_key": "sk-fake",
"api_version": "2025-02-01-preview",
},
_azure_router_client,
"https://azure.test/openai/deployments/whisper/audio/transcriptions?api-version=2025-02-01-preview",
),
],
ids=["openai", "azure"],
)
async def test_router_transcription_reuses_router_level_client_for_each_deployment(
litellm_params: dict[str, str],
build_client: Callable[[], AsyncOpenAI],
route_url: str,
monkeypatch: pytest.MonkeyPatch,
respx_mock: respx.MockRouter,
) -> None:
model_id: Final = f"whisper-{uuid.uuid4().hex}"
capture: Final = _RouterLoggingCapture(model_id)
monkeypatch.setattr(litellm, "callbacks", [capture])
router: Final = Router(
model_list=[{"model_name": "whisper", "litellm_params": dict(litellm_params), "model_info": {"id": model_id}}]
)
router_level_client: Final = build_client()
router.cache.set_cache(key=f"{model_id}_async_client", value=router_level_client, local_only=True)
route: Final = respx_mock.post(route_url).respond(200, json={"text": "hello"})
response: Final = await router.atranscription(
model="whisper", file=("speech.wav", io.BytesIO(b"offline audio"), "audio/wav")
)
public_client, public_logging = await capture.next_event()
internal_response: Final = await router._atranscription(
model="whisper", file=("speech.wav", io.BytesIO(b"offline audio"), "audio/wav")
)
internal_client, _ = await capture.next_event()
upstream_requests: Final = tuple(call.request for call in route.calls)
assert public_client == str(router_level_client)
assert internal_client == str(router_level_client)
assert tuple(str(request.url) for request in upstream_requests) == (route_url, route_url)
assert all(
request.headers.get("content-type", "").startswith("multipart/form-data")
and b'filename="speech.wav"' in request.content
and b"offline audio" in request.content
for request in upstream_requests
)
assert isinstance(public_logging, dict)
assert public_logging.get("model_group") == "whisper"
assert response.text == "hello"
assert internal_response.text == "hello"
@pytest.mark.asyncio
async def test_router_speech_returns_binary_content_and_logs_model_group(
respx_mock: respx.MockRouter,
monkeypatch: pytest.MonkeyPatch,
) -> None:
model_id: Final = f"tts-{uuid.uuid4().hex}"
capture: Final = _RouterLoggingCapture(model_id)
monkeypatch.setattr(litellm, "callbacks", [capture])
router: Final = Router(
model_list=[
{
"model_name": "tts",
"litellm_params": {"model": "openai/tts-1", "api_key": "sk-fake"},
"model_info": {"id": model_id},
}
]
)
route: Final = respx_mock.post("https://api.openai.com/v1/audio/speech").respond(200, content=b"audio")
response: Final = await router.aspeech(model="tts", input="hello", voice="alloy")
_, standard_logging_object = await capture.next_event()
assert route.call_count == 1
assert json.loads(route.calls[0].request.content) == {"model": "tts-1", "input": "hello", "voice": "alloy"}
assert isinstance(response, HttpxBinaryResponseContent)
assert response.content == b"audio"
assert isinstance(standard_logging_object, dict)
assert standard_logging_object["model_group"] == "tts"
@pytest.mark.asyncio
async def test_router_rerank_returns_valid_response_from_public_and_underlying_calls(
respx_mock: respx.MockRouter,
) -> None:
router: Final = Router(
model_list=[
{
"model_name": "cohere-rerank",
"litellm_params": {"model": "cohere/rerank-english-v3.0", "api_key": "sk-fake"},
}
]
)
route: Final = respx_mock.post("https://api.cohere.com/v2/rerank").respond(
200,
json={
"id": "rerank-1",
"results": [{"index": 0, "relevance_score": 0.9}],
"meta": {"api_version": {"version": "2"}},
},
)
public_response: Final = await router.arerank(
model="cohere-rerank",
query="hello",
documents=["hello", "world"],
top_n=1,
)
underlying_response: Final = await router._arerank(
model="cohere-rerank",
query="hello",
documents=["hello", "world"],
top_n=1,
)
assert route.call_count == 2
request_bodies: Final = tuple(json.loads(call.request.content) for call in route.calls)
assert all(
body["model"] == "rerank-english-v3.0"
and body["query"] == "hello"
and body["documents"] == ["hello", "world"]
and body["top_n"] == 1
for body in request_bodies
)
public_validated: Final = RerankResponse.model_validate(public_response)
assert public_validated.id == "rerank-1"
assert public_validated.results[0]["relevance_score"] == 0.9
underlying_validated: Final = RerankResponse.model_validate(underlying_response)
assert underlying_validated.id == "rerank-1"
assert underlying_validated.results[0]["relevance_score"] == 0.9
@pytest.mark.asyncio
@pytest.mark.parametrize(
("model", "expected_model", "expected_api_key"),
[
("omni-moderation-latest", "omni-moderation-latest", "sk-catch-all"),
("openai/omni-moderation-latest", "omni-moderation-latest", "sk-openai-wildcard"),
(None, None, "sk-env"),
],
)
async def test_router_moderation_routes_through_wildcard_deployments(
model: str | None,
expected_model: str | None,
expected_api_key: str,
respx_mock: respx.MockRouter,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("OPENAI_API_KEY", "sk-env")
router: Final = Router(
model_list=[
{
"model_name": "openai/*",
"litellm_params": {"model": "openai/*", "api_key": "sk-openai-wildcard"},
},
{
"model_name": "*",
"litellm_params": {"model": "openai/*", "api_key": "sk-catch-all"},
},
]
)
route: Final = respx_mock.post("https://api.openai.com/v1/moderations").respond(
200,
json={
"id": "modr-1",
"model": "omni-moderation-latest",
"results": [{"flagged": False, "categories": {}, "category_scores": {}}],
},
)
response: Final = await router.amoderation(model=model, input="hello")
assert route.call_count == 1
upstream_request: Final = route.calls[0].request
expected_body: Final = {"input": "hello"} if expected_model is None else {"input": "hello", "model": expected_model}
assert json.loads(upstream_request.content) == expected_body
assert upstream_request.headers["authorization"] == f"Bearer {expected_api_key}"
assert response.id == "modr-1"
assert response.model == "omni-moderation-latest"
assert response.results[0].flagged is False
@pytest.mark.asyncio
@pytest.mark.parametrize("sync_mode", [True, False])
async def test_router_image_generation_returns_valid_image_response(
sync_mode: bool,
respx_mock: respx.MockRouter,
) -> None:
router: Final = Router(
model_list=[
{
"model_name": "gpt-image-1",
"litellm_params": {"model": "openai/gpt-image-1", "api_key": "sk-fake"},
}
]
)
route: Final = respx_mock.post("https://api.openai.com/v1/images/generations").respond(
200,
json={"created": 1700000000, "data": [{"url": "https://images.test/result.png"}]},
)
response: Final = (
router._image_generation(model="gpt-image-1", prompt="a cat")
if sync_mode
else await router._aimage_generation(model="gpt-image-1", prompt="a cat")
)
assert route.call_count == 1
request_body: Final = json.loads(route.calls[0].request.content)
assert request_body["model"] == "gpt-image-1"
assert request_body["prompt"] == "a cat"
validated: Final = ImageResponse.model_validate(response)
assert validated.data[0].url == "https://images.test/result.png"
@pytest.mark.asyncio
@pytest.mark.usefixtures("router_minute_pinned")
async def test_router_acompletion_headers_read_post_increment_counter_and_count_once(
monkeypatch: pytest.MonkeyPatch,
) -> None:
capture: Final = _RouterLoggingCapture("lit-3058-async")
monkeypatch.setattr(litellm, "callbacks", [capture])
router: Final = _rpm_tpm_router("lit-3058-async")
response: Final = await router.acompletion(
model="gpt-5-mini", messages=[{"role": "user", "content": "hi"}], mock_response="pong"
)
total_tokens: Final = response.usage.total_tokens
headers: Final = _ratelimit_headers(response)
assert total_tokens > 0
assert headers["x-ratelimit-remaining-tokens"] == 1000 - total_tokens
assert headers["x-ratelimit-remaining-requests"] == 99
assert await router.get_model_group_usage("gpt-5-mini") == (total_tokens, 1)
await capture.next_event()
assert await router.get_model_group_usage("gpt-5-mini") == (total_tokens, 1)
@pytest.mark.asyncio
@pytest.mark.usefixtures("router_minute_pinned")
async def test_router_stream_counts_request_before_headers_and_tokens_once_on_completion(
monkeypatch: pytest.MonkeyPatch,
) -> None:
capture: Final = _RouterLoggingCapture("lit-3058-stream")
monkeypatch.setattr(litellm, "callbacks", [capture])
router: Final = _rpm_tpm_router("lit-3058-stream")
stream: Final = await router.acompletion(
model="gpt-5-mini",
messages=[{"role": "user", "content": "hi"}],
mock_response="pong pong pong",
stream=True,
stream_options={"include_usage": True},
)
headers: Final = _ratelimit_headers(stream)
assert headers["x-ratelimit-remaining-tokens"] == 1000
assert headers["x-ratelimit-remaining-requests"] == 99
assert await router.get_model_group_usage("gpt-5-mini") == (0, 1)
chunks: Final = [chunk async for chunk in stream]
total_tokens: Final = chunks[-1].usage.total_tokens
await capture.next_event()
assert total_tokens > 0
assert await router.get_model_group_usage("gpt-5-mini") == (total_tokens, 1)
def test_router_validate_fallbacks_accepts_well_formed_and_rejects_malformed_entries() -> None:
router: Final = Router(model_list=[])
assert router.validate_fallbacks([{"gpt-5.5": ["gpt-5-mini"]}, {"gpt-5-mini": ["gpt-5.5"]}]) is None
with pytest.raises(ValueError, match="must have exactly one key"):
router.validate_fallbacks([{"primary": "fallback", "other": "fallback"}])
with pytest.raises(ValueError, match="is not a dictionary"):
router.validate_fallbacks(["primary"])
def _routing_deployment(deployment_id: str, extra_params: dict[str, float]) -> dict[str, object]:
return {
"model_name": "gpt",
"litellm_params": {
"model": "openai/gpt-4o-mini",
"api_key": "sk-fake",
"api_base": f"https://{deployment_id}.test/v1",
**extra_params,
},
"model_info": {"id": deployment_id},
}
def _routing_router(
strategy: RoutingStrategy | str,
a_params: dict[str, float] | None = None,
b_params: dict[str, float] | None = None,
) -> Router:
return Router(
model_list=[_routing_deployment("a", a_params or {}), _routing_deployment("b", b_params or {})],
routing_strategy=strategy,
disable_cooldowns=True,
num_retries=0,
)
async def _selected_deployment_ids(router: Router) -> frozenset[str]:
deployments: Final = [
await router.async_get_available_deployment(model="gpt", messages=list(ROUTING_MESSAGES), request_kwargs={})
for _ in range(ROUTING_SELECTIONS)
]
return frozenset(deployment["model_info"]["id"] for deployment in deployments)
def _enum_and_string(strategy: RoutingStrategy) -> list[RoutingStrategy | str]:
return [strategy, strategy.value]
@pytest.mark.asyncio
@pytest.mark.parametrize("strategy", _enum_and_string(RoutingStrategy.COST_BASED))
async def test_router_cost_based_routing_selects_cheapest_deployment(strategy: RoutingStrategy | str) -> None:
router: Final = _routing_router(strategy, EXPENSIVE_COSTS, CHEAP_COSTS)
assert await _selected_deployment_ids(router) == frozenset({"b"})
@pytest.mark.asyncio
@pytest.mark.parametrize(
"strategy",
[*_enum_and_string(RoutingStrategy.USAGE_BASED_ROUTING), *_enum_and_string(RoutingStrategy.USAGE_BASED_ROUTING_V2)],
)
async def test_router_usage_based_routing_skips_deployment_over_tpm_limit(strategy: RoutingStrategy | str) -> None:
router: Final = _routing_router(strategy, {"tpm": 1}, {"tpm": 1_000_000})
assert await _selected_deployment_ids(router) == frozenset({"b"})
@pytest.mark.asyncio
@pytest.mark.parametrize("strategy", _enum_and_string(RoutingStrategy.LATENCY_BASED))
async def test_router_latency_based_routing_avoids_deployment_that_timed_out(
strategy: RoutingStrategy | str,
respx_mock: respx.MockRouter,
) -> None:
router: Final = _routing_router(strategy)
timed_out: Final = respx_mock.post("https://a.test/v1/chat/completions").mock(
side_effect=httpx.ReadTimeout("upstream timed out")
)
respx_mock.post("https://b.test/v1/chat/completions").respond(200, json=CHAT_RESPONSE)
for _ in range(5):
if timed_out.called:
break
with contextlib.suppress(litellm.Timeout):
await router.acompletion(model="gpt", messages=list(ROUTING_MESSAGES), max_retries=0)
assert timed_out.called
assert await _selected_deployment_ids(router) == frozenset({"b"})
@pytest.mark.asyncio
@pytest.mark.parametrize("strategy", _enum_and_string(RoutingStrategy.LEAST_BUSY))
async def test_router_least_busy_routing_avoids_deployment_with_request_in_flight(
strategy: RoutingStrategy | str,
respx_mock: respx.MockRouter,
) -> None:
router: Final = _routing_router(strategy)
upstream_hosts: Final = asyncio.Queue[str]()
release_upstream: Final = asyncio.Event()
async def hold_request(request: httpx.Request) -> httpx.Response:
upstream_hosts.put_nowait(request.url.host.split(".")[0])
await release_upstream.wait()
return httpx.Response(200, json=CHAT_RESPONSE)
respx_mock.post(url__regex=r"https://[ab]\.test/v1/chat/completions").mock(side_effect=hold_request)
in_flight: Final = asyncio.create_task(router.acompletion(model="gpt", messages=list(ROUTING_MESSAGES)))
try:
busy_id: Final = await asyncio.wait_for(upstream_hosts.get(), timeout=EVENT_TIMEOUT_SECONDS)
selected: Final = await _selected_deployment_ids(router)
finally:
release_upstream.set()
await in_flight
assert selected == frozenset({"a", "b"} - {busy_id})
@pytest.mark.asyncio
async def test_router_simple_shuffle_ignores_cost_and_spreads_across_deployments() -> None:
router: Final = _routing_router("simple-shuffle", EXPENSIVE_COSTS, CHEAP_COSTS)
assert await _selected_deployment_ids(router) == frozenset({"a", "b"})
@pytest.mark.parametrize("strategy", _enum_and_string(RoutingStrategy.PROVIDER_BUDGET_LIMITING))
def test_router_routing_strategy_init_accepts_provider_budget_strategy(strategy: RoutingStrategy | str) -> None:
router: Final = _routing_router(strategy)
router.routing_strategy_init(routing_strategy=strategy, routing_strategy_args={})
assert router.get_settings()["routing_strategy"] == "provider-budget-routing"
def test_router_track_deployment_metrics_updates_observable_usage() -> None:
router: Final = Router(
model_list=[
{
"model_name": "gpt-5-mini",
"litellm_params": {"model": "gpt-5-mini", "api_key": "sk-fake"},
"model_info": {"id": "metrics-deployment"},
}
]
)
deployment: Final = router.model_list[0]
router._track_deployment_metrics(deployment=deployment, parent_otel_span=None)
router._track_deployment_metrics(deployment=deployment, parent_otel_span=None)
assert router.cache.get_cache(key="metrics-deployment", local_only=True) == 2
class _GatedIncrementCache(DualCache):
def __init__(self) -> None:
super().__init__(in_memory_cache=InMemoryCache())
self.first_increment_started = asyncio.Event()
self.release_first_increment = asyncio.Event()
self.deployment_success_incremented = asyncio.Event()
self.increment_calls = 0
def increment_cache(self, key: str, value: int, local_only: bool = False, **kwargs: object) -> int:
result: Final = super().increment_cache(key=key, value=value, local_only=local_only, **kwargs)
if key.endswith(":successes"):
self.deployment_success_incremented.set()
return result
async def async_increment_cache_pipeline(
self,
increment_list: list[RedisPipelineIncrementOperation],
local_only: bool = False,
parent_otel_span: object = None,
**kwargs: object,
) -> list[float] | None:
self.increment_calls += 1
if self.increment_calls == 1:
self.first_increment_started.set()
await self.release_first_increment.wait()
return await super().async_increment_cache_pipeline(
increment_list=increment_list,
local_only=local_only,
parent_otel_span=parent_otel_span,
**kwargs,
)
class _UnavailableIncrementCache(DualCache):
def __init__(self) -> None:
super().__init__(in_memory_cache=InMemoryCache())
self.first_increment_started = asyncio.Event()
self.release_first_increment = asyncio.Event()
self.deployment_success_incremented = asyncio.Event()
self.increment_calls = 0
def increment_cache(self, key: str, value: int, local_only: bool = False, **kwargs: object) -> int:
result: Final = super().increment_cache(key=key, value=value, local_only=local_only, **kwargs)
if key.endswith(":successes"):
self.deployment_success_incremented.set()
return result
async def async_increment_cache_pipeline(
self,
increment_list: list[RedisPipelineIncrementOperation],
local_only: bool = False,
parent_otel_span: object = None,
**kwargs: object,
) -> list[float] | None:
self.increment_calls += 1
if self.increment_calls == 1:
self.first_increment_started.set()
await self.release_first_increment.wait()
raise RuntimeError("cache unavailable")
@pytest.mark.asyncio
@pytest.mark.usefixtures("router_minute_pinned")
async def test_router_success_callback_during_pre_header_increment_does_not_double_count() -> None:
router: Final = _rpm_tpm_router("lit-3058-race")
cache: Final = _GatedIncrementCache()
router.cache = cache
request: Final = asyncio.create_task(
router.acompletion(
model="gpt-5-mini",
messages=[{"role": "user", "content": "hi"}],
mock_response="pong",
)
)
await asyncio.wait_for(cache.first_increment_started.wait(), timeout=EVENT_TIMEOUT_SECONDS)
await asyncio.wait_for(cache.deployment_success_incremented.wait(), timeout=EVENT_TIMEOUT_SECONDS)
assert get_deployment_successes_for_current_minute(router, "lit-3058-race") == 1
assert cache.increment_calls == 1
cache.release_first_increment.set()
response: Final = await request
assert await router.get_model_group_usage("gpt-5-mini") == (response.usage.total_tokens, 1)
@pytest.mark.asyncio
@pytest.mark.usefixtures("router_minute_pinned")
async def test_router_failed_pre_header_increment_clears_counted_tokens_stamp() -> None:
router: Final = _rpm_tpm_router("lit-3058-fail")
cache: Final = _UnavailableIncrementCache()
router.cache = cache
metadata: Final[dict[str, object]] = {}
request: Final = asyncio.create_task(
router.acompletion(
model="gpt-5-mini",
messages=[{"role": "user", "content": "hi"}],
mock_response="pong",
metadata=metadata,
)
)
await asyncio.wait_for(cache.first_increment_started.wait(), timeout=EVENT_TIMEOUT_SECONDS)
await asyncio.wait_for(cache.deployment_success_incremented.wait(), timeout=EVENT_TIMEOUT_SECONDS)
assert metadata[ROUTER_USAGE_COUNTED_TOKENS_METADATA_KEY] == 30
assert get_deployment_successes_for_current_minute(router, "lit-3058-fail") == 1
assert cache.increment_calls == 1
cache.release_first_increment.set()
response: Final = await request
assert response.usage.total_tokens == 30
assert ROUTER_USAGE_COUNTED_TOKENS_METADATA_KEY not in metadata
assert _ratelimit_headers(response)["x-ratelimit-remaining-requests"] == 100
assert await router.get_model_group_usage("gpt-5-mini") == (None, None)
ASSISTANT_RESPONSE: Final = {
"object": "assistant",
"created_at": 1700000000,
"name": "offline",
"description": None,
"model": "gpt-4o-mini",
"instructions": "hello",
"tools": [],
"metadata": {},
}
@pytest.mark.asyncio
async def test_router_assistants_endpoint_factory_invokes_provider(
respx_mock: respx.MockRouter,
) -> None:
router: Final = Router(model_list=[])
route: Final = respx_mock.post("https://api.openai.com/v1/assistants").respond(
200, json={**ASSISTANT_RESPONSE, "id": "asst-1"}
)
response: Final = await router._pass_through_assistants_endpoint_factory(
original_function=litellm.acreate_assistants,
custom_llm_provider="openai",
model="gpt-4o-mini",
api_key="sk-fake",
name="offline",
)
assert route.call_count == 1
assert json.loads(route.calls[0].request.content) == {"model": "gpt-4o-mini", "name": "offline"}
assert response.id == "asst-1"
@pytest.mark.asyncio
async def test_router_factory_function_returns_invokable_assistants_wrapper(
respx_mock: respx.MockRouter,
) -> None:
router: Final = Router(model_list=[])
route: Final = respx_mock.post("https://api.openai.com/v1/assistants").respond(
200, json={**ASSISTANT_RESPONSE, "id": "asst-2"}
)
wrapper: Final = router.factory_function(litellm.acreate_assistants, call_type="assistants")
response: Final = await wrapper(
custom_llm_provider="openai",
model="gpt-4o-mini",
api_key="sk-fake",
name="offline",
)
assert route.call_count == 1
assert json.loads(route.calls[0].request.content) == {"model": "gpt-4o-mini", "name": "offline"}
assert response.id == "asst-2"
@pytest.mark.asyncio
async def test_router_moderation_endpoint_factory_invokes_default_model(
respx_mock: respx.MockRouter,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setenv("OPENAI_API_KEY", "sk-fake")
router: Final = Router(model_list=[])
route: Final = respx_mock.post("https://api.openai.com/v1/moderations").respond(
200,
json={
"id": "modr-2",
"model": "omni-moderation-latest",
"results": [{"flagged": False, "categories": {}, "category_scores": {}}],
},
)
response: Final = await router._pass_through_moderation_endpoint_factory(
original_function=litellm.amoderation,
custom_llm_provider="openai",
input="hello",
model=None,
api_key="sk-fake",
)
assert route.call_count == 1
assert json.loads(route.calls[0].request.content) == {"input": "hello"}
assert response.id == "modr-2"
def _sora_deployment(
model_name: str, api_key: str, deployment_id: str, access_groups: tuple[str, ...] = ()
) -> dict[str, object]:
return {
"model_name": model_name,
"litellm_params": {"model": "openai/sora-2", "api_key": api_key},
"model_info": {"id": deployment_id, "access_groups": list(access_groups)},
}
def _two_account_sora_router() -> Router:
return Router(
model_list=[
_sora_deployment("sora", "key-a", "dep-a"),
_sora_deployment("sora", "key-b", "dep-b"),
_sora_deployment("other", "key-x", "dep-x"),
],
model_group_alias={"sora-alias": "sora"},
)
async def _poll_status_keys(
router: Router,
model: str,
deployment_id: str,
respx_mock: respx.MockRouter,
caller: UserAPIKeyAuth | None = None,
) -> set[str]:
route: Final = respx_mock.get("https://api.openai.com/v1/videos/video_raw").respond(
200, json={"id": "video_raw", "object": "video", "status": "in_progress"}
)
video_id: Final = encode_video_id_with_provider("video_raw", "openai", "sora", deployment_id=deployment_id)
metadata: Final = {} if caller is None else {"user_api_key_auth": caller}
for _ in range(10):
await router.avideo_status(model=model, video_id=video_id, litellm_metadata=dict(metadata))
return {call.request.headers["authorization"] for call in route.calls}
@pytest.mark.asyncio
@pytest.mark.parametrize("model", ["sora", "sora-alias"])
async def test_video_status_polls_the_deployment_that_created_the_video(
model: str, respx_mock: respx.MockRouter
) -> None:
keys: Final = await _poll_status_keys(_two_account_sora_router(), model, "dep-b", respx_mock)
assert keys == {"Bearer key-b"}
def test_sync_video_status_polls_the_deployment_that_created_the_video(respx_mock: respx.MockRouter) -> None:
route: Final = respx_mock.get("https://api.openai.com/v1/videos/video_raw").respond(
200, json={"id": "video_raw", "object": "video", "status": "in_progress"}
)
router: Final = _two_account_sora_router()
video_id: Final = encode_video_id_with_provider("video_raw", "openai", "sora", deployment_id="dep-b")
for _ in range(10):
router.video_status(model="sora", video_id=video_id)
assert {call.request.headers["authorization"] for call in route.calls} == {"Bearer key-b"}
@pytest.mark.asyncio
async def test_video_status_on_a_wildcard_polls_the_deployment_that_created_the_video(
respx_mock: respx.MockRouter,
) -> None:
router: Final = Router(
model_list=[
{"model_name": "openai/*", "litellm_params": {"model": "openai/*", "api_key": key}, "model_info": {"id": dep}}
for dep, key in (("wc-a", "key-a"), ("wc-b", "key-b"))
]
)
keys: Final = await _poll_status_keys(router, "openai/sora-2", "wc-b", respx_mock)
assert keys == {"Bearer key-b"}
@pytest.mark.asyncio
async def test_video_status_never_pins_to_a_deployment_in_cooldown(respx_mock: respx.MockRouter) -> None:
router: Final = _two_account_sora_router()
router.cooldown_cache.add_deployment_to_cooldown(
model_id="dep-b",
original_exception=litellm.RateLimitError(message="upstream 429", llm_provider="openai", model="sora-2"),
exception_status=429,
cooldown_time=60,
)
keys: Final = await _poll_status_keys(router, "sora", "dep-b", respx_mock)
assert keys == {"Bearer key-a"}
@pytest.mark.asyncio
async def test_video_status_on_a_routing_group_polls_the_member_deployment_that_created_the_video(
respx_mock: respx.MockRouter,
) -> None:
router: Final = Router(
model_list=[_sora_deployment("sora-a", "key-a", "dep-a"), _sora_deployment("sora-b", "key-b", "dep-b")],
routing_groups=[
{"group_name": "sora-group", "models": ["sora-a", "sora-b"], "routing_strategy": "simple-shuffle"}
],
)
keys: Final = await _poll_status_keys(router, "sora-group", "dep-b", respx_mock)
assert keys == {"Bearer key-b"}
@pytest.mark.asyncio
async def test_video_status_never_pins_to_a_deployment_outside_the_requested_group(
respx_mock: respx.MockRouter,
) -> None:
keys: Final = await _poll_status_keys(_two_account_sora_router(), "sora", "dep-x", respx_mock)
assert keys <= {"Bearer key-a", "Bearer key-b"}
@pytest.mark.asyncio
async def test_video_status_with_a_removed_deployment_routes_across_the_group(respx_mock: respx.MockRouter) -> None:
keys: Final = await _poll_status_keys(_two_account_sora_router(), "sora", "dep-gone", respx_mock)
assert keys <= {"Bearer key-a", "Bearer key-b"}
assert keys
@pytest.mark.asyncio
@pytest.mark.parametrize(("key_access_group", "expected_key"), [("ag-a", "Bearer key-a"), ("ag-b", "Bearer key-b")])
async def test_video_status_pins_only_to_a_deployment_in_the_callers_access_groups(
key_access_group: str, expected_key: str, respx_mock: respx.MockRouter
) -> None:
router: Final = Router(
model_list=[
_sora_deployment("sora", "key-a", "dep-a", access_groups=("ag-a",)),
_sora_deployment("sora", "key-b", "dep-b", access_groups=("ag-b",)),
]
)
keys: Final = await _poll_status_keys(
router, "sora", "dep-b", respx_mock, caller=UserAPIKeyAuth(models=[key_access_group])
)
assert keys == {expected_key}
@pytest.mark.asyncio
@pytest.mark.parametrize("model", ["sora", "sora-alias"])
async def test_pinned_video_status_logs_the_requested_model_group(
model: str, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
) -> None:
capture: Final = _RouterLoggingCapture("dep-b")
monkeypatch.setattr(litellm, "callbacks", [capture])
keys: Final = await _poll_status_keys(_two_account_sora_router(), model, "dep-b", respx_mock)
_, standard_logging_object = await capture.next_event()
assert keys == {"Bearer key-b"}
assert isinstance(standard_logging_object, dict)
assert standard_logging_object["model_group"] == model
def _ordered_chat_router() -> Router:
return Router(
model_list=[
{
"model_name": "chat-ordered",
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": key, "order": order},
"model_info": {"id": deployment_id},
}
for deployment_id, key, order in (("dep-primary", "key-a", 1), ("dep-backup", "key-b", 2))
]
)
@pytest.mark.asyncio
async def test_a_video_id_in_a_chat_body_never_overrides_deployment_order() -> None:
router: Final = _ordered_chat_router()
video_id: Final = encode_video_id_with_provider("video_raw", "openai", "chat-ordered", deployment_id="dep-backup")
chats: Final = [
await router.acompletion(
model="chat-ordered", messages=list(ROUTING_MESSAGES), mock_response="ok", video_id=video_id
)
for _ in range(4)
]
responses: Final = [
await router.aresponses(model="chat-ordered", input="route this request", mock_response="ok", video_id=video_id)
for _ in range(4)
]
assert {chat._hidden_params["model_id"] for chat in chats} == {"dep-primary"}
assert {response._hidden_params["model_id"] for response in responses} == {"dep-primary"}
@pytest.mark.asyncio
async def test_video_lookup_404_never_cools_down_the_deployment_that_answered(respx_mock: respx.MockRouter) -> None:
respx_mock.get("https://api.openai.com/v1/videos/video_gone").respond(
404, json={"error": {"code": "NotFound", "message": "Video not found"}}
)
router: Final = Router(
model_list=[_sora_deployment("sora", "key-a", "dep-a"), _sora_deployment("sora", "key-b", "dep-b")],
num_retries=0,
allowed_fails=1,
)
video_id: Final = encode_video_id_with_provider("video_gone", "openai", "sora", deployment_id="dep-b")
for _ in range(3):
with pytest.raises(litellm.NotFoundError):
await router.avideo_status(model="sora", video_id=video_id)
assert await router.cooldown_cache.async_get_active_cooldowns(["dep-a", "dep-b"], parent_otel_span=None) == []
@pytest.mark.asyncio
async def test_chat_404_still_cools_down_the_deployment(respx_mock: respx.MockRouter) -> None:
respx_mock.post("https://api.openai.com/v1/chat/completions").respond(
404, json={"error": {"code": "NotFound", "message": "Deployment not found"}}
)
router: Final = Router(
model_list=[
{
"model_name": "chat",
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "key-a"},
"model_info": {"id": "dep-chat"},
},
{
"model_name": "chat",
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "key-b"},
"model_info": {"id": "dep-chat-b"},
},
],
num_retries=0,
allowed_fails=1,
)
for _ in range(3):
with contextlib.suppress(litellm.NotFoundError, litellm.RateLimitError):
await router.acompletion(model="chat", messages=list(ROUTING_MESSAGES))
assert await router.cooldown_cache.async_get_active_cooldowns(["dep-chat", "dep-chat-b"], parent_otel_span=None)