mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* fix(router)!: name the request size when usage-based routing refuses an over-tpm request usage-based-routing-v2 answered "No deployments available" with no usage shown when one request alone was over every deployment's tpm, and a single-deployment group held the caller for 3 x 60 seconds on retries that could never clear it. The router now raises RequestOverDeploymentTpmError, a RateLimitError that names the estimated tokens and the largest tpm limit, carries no retry-after, skips retries, and still reaches group fallbacks. * fix(router): count cooling deployments before refusing an over-tpm request, keep the Prometheus label The over-tpm refusal now also reads every deployment in the model group, so a request that fits a cooling deployment keeps the old retryable 429. RequestOverDeploymentTpmError keeps the RateLimitError exception_class label in Prometheus. * fix(router): count only cooling deployments the request can still reach, and keep the logged error class The over-tpm refusal now reads cooling deployments from the caller's pool, kept to the ones team, region, and tag routing would still send the request to, so team models and tag-filtered requests get the right answer. Logs, OTEL, and Datadog keep naming the refusal RateLimitError like Prometheus does, and a tpm written as a number string counts as that number * fix(router): keep the sync over-tpm refusal a ValueError and count every deployment the caller may use The sync Router raises SyncRequestOverDeploymentTpmError, which is still a ValueError, so existing except ValueError handlers keep catching it. The never-retryable refusal now counts every deployment in the caller's pool that region and tag routing allow, so a deployment left out of this pick by a budget, health check, order, or pin keeps the retryable 429 * fix(router): keep retry records and trace events naming the over-tpm refusal RateLimitError Fallback records in previous_models and the litellm.routing.retry trace event now read the error's logged class name, as the logging payload already did, so they keep RateLimitError for the over-tpm refusal as they did before this change --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
20544 lines
791 KiB
Python
20544 lines
791 KiB
Python
import ast, asyncio, importlib, time
|
|
import contextlib
|
|
import copy
|
|
import functools
|
|
import gc
|
|
import itertools
|
|
import io
|
|
import base64
|
|
import json
|
|
import linecache
|
|
import logging
|
|
import os
|
|
import sys
|
|
import threading
|
|
import warnings
|
|
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Callable, Iterator, Mapping, Sequence
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
from types import CodeType, FrameType, MappingProxyType, SimpleNamespace
|
|
from typing import TYPE_CHECKING, Final, Literal, TypeVar, cast
|
|
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
|
|
|
import httpx
|
|
import openai
|
|
import pytest
|
|
import respx
|
|
from fastapi import HTTPException
|
|
from opentelemetry import trace
|
|
from pydantic import BaseModel, TypeAdapter
|
|
|
|
import litellm
|
|
from litellm import APIConnectionError, Router
|
|
from litellm._logging import verbose_logger, verbose_router_logger
|
|
from litellm.caching import RedisClusterCache
|
|
from litellm.caching.in_memory_cache import DEFAULT_MAX_SIZE_IN_MEMORY
|
|
from litellm.caching.redis_cache import _redis_circuit_breaker_guard
|
|
from litellm.exceptions import (
|
|
GuardrailRaisedException,
|
|
MidStreamFallbackError,
|
|
ModifyResponseException,
|
|
RequestOverDeploymentTpmError,
|
|
)
|
|
from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper
|
|
from litellm.integrations.custom_guardrail import CustomGuardrail
|
|
from litellm.integrations.custom_logger import CustomLogger
|
|
from litellm.integrations.otel.plumbing.context import set_request_root_span
|
|
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLogging
|
|
from litellm.llms.anthropic.messages.native.streaming_iterator import closing_superseded_attempt
|
|
from litellm.llms.anthropic.messages.native.agentic_streaming_iterator import (
|
|
AGENTIC_LOOP_LIMIT_ERROR_SSE_BYTES,
|
|
SERVER_FULFILLED_TOOL_LEAK_ERROR_SSE_BYTES,
|
|
)
|
|
from litellm.llms.bedrock.common_utils import BedrockError
|
|
from litellm.models.access_group import LiteLLM_AccessGroupTable
|
|
from litellm.proxy._types import LiteLLM_TeamTable, LitellmUserRoles, Member, ProxyException, UserAPIKeyAuth
|
|
from litellm.router_rust.invoke.streaming import (
|
|
MAX_BUFFERED_PRE_CONTENT_ANTHROPIC_CHUNKS,
|
|
MAX_HELD_PRE_OUTPUT_RESPONSES_EVENTS,
|
|
FallbackAwareAnthropicMessagesStream,
|
|
_stream_commits_now,
|
|
_anthropic_stream_error_is_gateway_verdict,
|
|
_stream_fallback_error_for_raised,
|
|
_anthropic_stream_forwards_ping_live,
|
|
_stream_raised_error_status,
|
|
_anthropic_stream_should_decline_fallback,
|
|
_is_retriable_anthropic_status,
|
|
_responses_stream_holds_event,
|
|
) # noqa: PLC2701
|
|
from litellm.router_rust.surface.registry import _search_tool_support # noqa: PLC2701
|
|
from litellm.proxy.response_polling.polling_handler import should_use_polling_for_request
|
|
from litellm.router_strategy import simple_shuffle
|
|
from litellm.router_strategy.complexity_router.complexity_router import ComplexityRouter
|
|
from litellm.router_strategy.savings_baseline import Baseline, _models_in
|
|
from litellm.router_utils.client_initalization_utils import MaxParallelRequestsLimit
|
|
from litellm.router_utils.cooldown_handlers import (
|
|
_async_get_cooldown_deployments,
|
|
_is_cooldown_required,
|
|
async_get_cooldown_deployments,
|
|
get_cooldown_deployments,
|
|
)
|
|
from litellm.router_utils.fallback_event_handlers import (
|
|
DISABLE_FALLBACKS_METADATA_KEY,
|
|
MID_STREAM_FALLBACK_CONTROLS_KEY,
|
|
)
|
|
from litellm.router_utils.prompt_caching_cache import PromptCachingCache
|
|
from litellm.router_utils.router_callbacks.track_deployment_metrics import get_deployment_successes_for_current_minute
|
|
from litellm.scheduler import FlowItem
|
|
from litellm.types.integrations.custom_logger import AgenticLoopPlan, AgenticLoopRequestPatch
|
|
from litellm.types.llms.openai import AllMessageValues, ChatCompletionRequest
|
|
from litellm.router_api import RoutingContext as RouterContext
|
|
from litellm.types.router import (
|
|
CustomRoutingStrategyBase,
|
|
Deployment,
|
|
DeploymentTypedDict,
|
|
LiteLLM_Params,
|
|
ModelGroupInfo,
|
|
ModelInfo,
|
|
PreRoutingHookResponse,
|
|
RetryPolicy,
|
|
RouterRateLimitError,
|
|
RoutingContext,
|
|
)
|
|
from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER
|
|
from litellm.llms.base_llm.vector_store.transformation import (
|
|
LiteLLMVectorStoreEmbeddingExecutor,
|
|
RouterVectorStoreEmbeddingExecutor,
|
|
)
|
|
from litellm.types.utils import CallTypes, CredentialItem, Usage
|
|
from litellm.litellm_core_utils.model_cost_table import publish
|
|
from tests._vcr_conftest_common import install_live_call_probe, record_vcr_outcome
|
|
from tests.fake_openai_endpoint import FAKE_OPENAI_API_BASE
|
|
from tests.large_text import text
|
|
from tests.unit.conftest import redis_cache_double
|
|
import traceback
|
|
import inspect
|
|
from typing import List, Optional
|
|
|
|
if TYPE_CHECKING:
|
|
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
|
|
|
|
from litellm.integrations.otel.logger import OpenTelemetryV2
|
|
|
|
|
|
_ROUTER_DECISIONS_RESPONSE: Final[Mapping[str, object]] = {
|
|
"model": "gpt-6-luna",
|
|
"answers": [{"type": "predicate", "name": "damaged", "probability": 0.9}],
|
|
"usage": {
|
|
"input_tokens": 1,
|
|
"input_tokens_details": {"cached_tokens": 0, "cache_write_tokens": 0},
|
|
"output_tokens": 1,
|
|
"output_tokens_details": {"reasoning_tokens": 0},
|
|
"total_tokens": 2,
|
|
},
|
|
}
|
|
|
|
|
|
_OPENAI_DECISIONS_PARAMS: Final[Mapping[str, object]] = {
|
|
"model": "openai/gpt-6-luna",
|
|
"api_base": "https://api.openai.com/v1",
|
|
"api_key": "k",
|
|
}
|
|
_BEDROCK_DECISIONS_PARAMS: Final[Mapping[str, object]] = {
|
|
"model": "bedrock/us.openai.gpt-5.6-terra",
|
|
"aws_access_key_id": "fake",
|
|
"aws_secret_access_key": "fake",
|
|
"aws_region_name": "us-east-1",
|
|
}
|
|
|
|
|
|
def _decisions_leg(leg_id: str, params: Mapping[str, object], weight: int | None = None) -> dict[str, object]:
|
|
return {
|
|
"model_name": "decision-group",
|
|
"litellm_params": params if weight is None else {**params, "weight": weight},
|
|
"model_info": {"id": leg_id},
|
|
}
|
|
|
|
|
|
_OPENAI_DECISIONS_LEG: Final = _decisions_leg("openai-leg", _OPENAI_DECISIONS_PARAMS)
|
|
_BEDROCK_DECISIONS_LEG: Final = _decisions_leg("bedrock-leg", _BEDROCK_DECISIONS_PARAMS)
|
|
|
|
|
|
def _mixed_decisions_group_router(*legs: dict[str, object]) -> Router:
|
|
return Router(model_list=list(legs), routing_strategy="simple-shuffle")
|
|
|
|
|
|
@pytest.fixture
|
|
def _router_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.mark.asyncio
|
|
async def test_decisions_on_a_mixed_group_never_routes_to_a_leg_without_decisions_support(
|
|
respx_mock: respx.MockRouter,
|
|
_router_httpx_transport: None,
|
|
) -> None:
|
|
router: Final = _mixed_decisions_group_router(_OPENAI_DECISIONS_LEG, _BEDROCK_DECISIONS_LEG)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/decisions").respond(json=_ROUTER_DECISIONS_RESPONSE)
|
|
|
|
for _ in range(20):
|
|
await router.adecisions(
|
|
model="decision-group",
|
|
input=[{"role": "user", "content": "The package arrived with a broken screen."}],
|
|
questions=[
|
|
{"type": "predicate", "name": "damaged", "instructions": "Does the customer report a damaged item?"}
|
|
],
|
|
)
|
|
|
|
assert route.call_count == 20
|
|
|
|
|
|
def test_decisions_on_a_mixed_group_never_routes_to_a_leg_without_decisions_support_sync(
|
|
respx_mock: respx.MockRouter,
|
|
) -> None:
|
|
router: Final = _mixed_decisions_group_router(_OPENAI_DECISIONS_LEG, _BEDROCK_DECISIONS_LEG)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/decisions").respond(json=_ROUTER_DECISIONS_RESPONSE)
|
|
|
|
for _ in range(20):
|
|
router.decisions(
|
|
model="decision-group",
|
|
input=[{"role": "user", "content": "The package arrived with a broken screen."}],
|
|
questions=[
|
|
{"type": "predicate", "name": "damaged", "instructions": "Does the customer report a damaged item?"}
|
|
],
|
|
)
|
|
|
|
assert route.call_count == 20
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_decisions_on_a_group_with_no_capable_leg_still_returns_the_provider_error() -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-only",
|
|
"litellm_params": {
|
|
"model": "bedrock/us.openai.gpt-5.6-terra",
|
|
"aws_access_key_id": "fake",
|
|
"aws_secret_access_key": "fake",
|
|
"aws_region_name": "us-east-1",
|
|
},
|
|
"model_info": {"id": "bedrock-leg"},
|
|
}
|
|
]
|
|
)
|
|
|
|
with pytest.raises(litellm.BadRequestError, match="Unknown Decisions provider 'bedrock'"):
|
|
await router.adecisions(
|
|
model="bedrock-only",
|
|
input=[{"role": "user", "content": "The package arrived with a broken screen."}],
|
|
questions=[
|
|
{"type": "predicate", "name": "damaged", "instructions": "Does the customer report a damaged item?"}
|
|
],
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_chat_on_a_mixed_group_still_reaches_the_leg_without_decisions_support() -> None:
|
|
router: Final = _mixed_decisions_group_router(
|
|
_decisions_leg("openai-leg", _OPENAI_DECISIONS_PARAMS, weight=0),
|
|
_decisions_leg("bedrock-leg", _BEDROCK_DECISIONS_PARAMS, weight=1),
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="decision-group",
|
|
messages=[{"role": "user", "content": "Say hi"}],
|
|
mock_response="hi",
|
|
)
|
|
|
|
assert response._hidden_params["model_id"] == "bedrock-leg"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_encrypted_content_affinity_model_group_config_is_additive():
|
|
from litellm.responses.utils import ResponsesAPIRequestUtils
|
|
from litellm.router_utils.pre_call_checks.encrypted_content_affinity_check import (
|
|
EncryptedContentAffinityCheck,
|
|
)
|
|
|
|
model_group = "openai.gpt-5.1-codex"
|
|
target_deployment = {
|
|
"model_name": model_group,
|
|
"litellm_params": {"model": "openai/gpt-5.1-codex"},
|
|
"model_info": {"id": "deployment-b"},
|
|
}
|
|
healthy_deployments = [
|
|
{
|
|
"model_name": model_group,
|
|
"litellm_params": {"model": "openai/gpt-5.1-codex"},
|
|
"model_info": {"id": "deployment-a"},
|
|
},
|
|
target_deployment,
|
|
]
|
|
encoded_id = ResponsesAPIRequestUtils._build_encrypted_item_id("deployment-b", "rs_test")
|
|
|
|
assert EncryptedContentAffinityCheck.has_model_group_affinity_enabled({model_group: ["encrypted_content_affinity"]})
|
|
assert not EncryptedContentAffinityCheck.has_model_group_affinity_enabled(None)
|
|
|
|
per_group_check = EncryptedContentAffinityCheck(
|
|
enable_global_affinity=False,
|
|
model_group_affinity_config={
|
|
model_group: ["encrypted_content_affinity"],
|
|
},
|
|
)
|
|
request_kwargs = {
|
|
"input": [{"type": "reasoning", "id": encoded_id}],
|
|
"litellm_metadata": {},
|
|
}
|
|
filtered = await per_group_check.async_filter_deployments(
|
|
model=model_group,
|
|
healthy_deployments=healthy_deployments,
|
|
messages=None,
|
|
request_kwargs=request_kwargs,
|
|
)
|
|
|
|
assert filtered == [target_deployment]
|
|
assert request_kwargs["litellm_metadata"]["encrypted_content_affinity_enabled"]
|
|
|
|
disabled_check = EncryptedContentAffinityCheck(
|
|
enable_global_affinity=False,
|
|
model_group_affinity_config={
|
|
"other-model-group": ["encrypted_content_affinity"],
|
|
},
|
|
)
|
|
disabled_request_kwargs = {
|
|
"input": [{"type": "reasoning", "id": encoded_id}],
|
|
"litellm_metadata": {},
|
|
}
|
|
unfiltered = await disabled_check.async_filter_deployments(
|
|
model=model_group,
|
|
healthy_deployments=healthy_deployments,
|
|
messages=None,
|
|
request_kwargs=disabled_request_kwargs,
|
|
)
|
|
|
|
assert unfiltered == healthy_deployments
|
|
assert "encrypted_content_affinity_enabled" not in disabled_request_kwargs["litellm_metadata"]
|
|
|
|
global_check = EncryptedContentAffinityCheck(
|
|
enable_global_affinity=True,
|
|
model_group_affinity_config={
|
|
model_group: ["deployment_affinity"],
|
|
},
|
|
)
|
|
global_request_kwargs = {
|
|
"input": [{"type": "reasoning", "id": encoded_id}],
|
|
"litellm_metadata": {},
|
|
}
|
|
globally_filtered = await global_check.async_filter_deployments(
|
|
model=model_group,
|
|
healthy_deployments=healthy_deployments,
|
|
messages=None,
|
|
request_kwargs=global_request_kwargs,
|
|
)
|
|
|
|
assert globally_filtered == [target_deployment]
|
|
assert global_request_kwargs["litellm_metadata"]["encrypted_content_affinity_enabled"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_encrypted_content_affinity_takes_priority_over_user_key_affinity():
|
|
from litellm.responses.utils import ResponsesAPIRequestUtils
|
|
from litellm.router_utils.pre_call_checks.deployment_affinity_check import (
|
|
DeploymentAffinityCheck,
|
|
)
|
|
from litellm.router_utils.pre_call_checks.encrypted_content_affinity_check import (
|
|
EncryptedContentAffinityCheck,
|
|
)
|
|
|
|
model_group = "openai.gpt-5.1-codex"
|
|
user_api_key_hash = "test-user-key"
|
|
deployment_a = {
|
|
"model_name": model_group,
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.1-codex",
|
|
"api_key": "mock-api-key-a",
|
|
},
|
|
"model_info": {"id": "deployment-a"},
|
|
}
|
|
deployment_b = {
|
|
"model_name": model_group,
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.1-codex",
|
|
"api_key": "mock-api-key-b",
|
|
},
|
|
"model_info": {"id": "deployment-b"},
|
|
}
|
|
original_callbacks = list(litellm.callbacks)
|
|
litellm.callbacks = []
|
|
router = None
|
|
|
|
try:
|
|
router = litellm.Router(
|
|
model_list=[deployment_a, deployment_b],
|
|
model_group_affinity_config={
|
|
model_group: [
|
|
"deployment_affinity",
|
|
"encrypted_content_affinity",
|
|
],
|
|
},
|
|
num_retries=0,
|
|
)
|
|
callbacks = router.optional_callbacks or []
|
|
deployment_callback = next(cb for cb in callbacks if isinstance(cb, DeploymentAffinityCheck))
|
|
encrypted_content_callback = next(cb for cb in callbacks if isinstance(cb, EncryptedContentAffinityCheck))
|
|
assert callbacks.index(encrypted_content_callback) < callbacks.index(deployment_callback)
|
|
assert litellm.callbacks.index(encrypted_content_callback) < (litellm.callbacks.index(deployment_callback))
|
|
|
|
cache_key = DeploymentAffinityCheck.get_affinity_cache_key(
|
|
model_group=model_group,
|
|
user_key=user_api_key_hash,
|
|
)
|
|
await deployment_callback.pins.set(cache_key, {"model_id": "deployment-a"}, 60)
|
|
encoded_id = ResponsesAPIRequestUtils._build_encrypted_item_id("deployment-b", "rs_test")
|
|
request_kwargs = {
|
|
"input": [{"type": "reasoning", "id": encoded_id}],
|
|
"litellm_metadata": {"user_api_key_hash": user_api_key_hash},
|
|
}
|
|
|
|
filtered = await router.async_callback_filter_deployments(
|
|
model=model_group,
|
|
healthy_deployments=[deployment_a, deployment_b],
|
|
messages=None,
|
|
parent_otel_span=None,
|
|
request_kwargs=request_kwargs,
|
|
)
|
|
|
|
assert filtered == [deployment_b]
|
|
assert request_kwargs.get("_encrypted_content_affinity_pinned") is True
|
|
finally:
|
|
if router is not None:
|
|
router.discard()
|
|
litellm.callbacks = original_callbacks
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_arouter_with_tags_and_fallbacks():
|
|
"""
|
|
If fallback model missing tag, raise error
|
|
"""
|
|
from litellm import Router
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"mock_response": "Hello, world!",
|
|
"tags": ["test"],
|
|
},
|
|
},
|
|
{
|
|
"model_name": "anthropic-claude-3-5-sonnet",
|
|
"litellm_params": {
|
|
"model": "claude-sonnet-4-5-20250929",
|
|
"mock_response": "Hello, world 2!",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[
|
|
{"gpt-3.5-turbo": ["anthropic-claude-3-5-sonnet"]},
|
|
],
|
|
enable_tag_filtering=True,
|
|
)
|
|
|
|
with pytest.raises(litellm.InternalServerError):
|
|
response = await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hello, world!"}],
|
|
mock_testing_fallbacks=True,
|
|
metadata={"tags": ["test"]},
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_acreate_file():
|
|
"""
|
|
Write to all deployments of a model
|
|
"""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
},
|
|
{"model_name": "gpt-3.5-turbo", "litellm_params": {"model": "gpt-4o-mini"}},
|
|
],
|
|
)
|
|
|
|
with patch("litellm.acreate_file", return_value=MagicMock()) as mock_acreate_file:
|
|
mock_acreate_file.return_value = MagicMock()
|
|
response = await router.acreate_file(
|
|
model="gpt-3.5-turbo",
|
|
purpose="test",
|
|
file=MagicMock(),
|
|
)
|
|
|
|
# assert that the mock_acreate_file was called twice
|
|
assert mock_acreate_file.call_count == 2
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_acreate_file_with_jsonl():
|
|
"""
|
|
Test router.acreate_file with both JSONL and non-JSONL files
|
|
"""
|
|
import json
|
|
from io import BytesIO
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
# Create test JSONL content
|
|
jsonl_data = [
|
|
{
|
|
"body": {
|
|
"model": "gpt-3.5-turbo-router",
|
|
"messages": [{"role": "user", "content": "test"}],
|
|
}
|
|
},
|
|
{
|
|
"body": {
|
|
"model": "gpt-3.5-turbo-router",
|
|
"messages": [{"role": "user", "content": "test2"}],
|
|
}
|
|
},
|
|
]
|
|
jsonl_content = "\n".join(json.dumps(item) for item in jsonl_data)
|
|
jsonl_file = BytesIO(jsonl_content.encode("utf-8"))
|
|
jsonl_file.name = "test.jsonl"
|
|
|
|
# Create test non-JSONL content
|
|
non_jsonl_content = "This is not a JSONL file"
|
|
non_jsonl_file = BytesIO(non_jsonl_content.encode("utf-8"))
|
|
non_jsonl_file.name = "test.txt"
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo-router",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo-router",
|
|
"litellm_params": {"model": "gpt-4o-mini"},
|
|
},
|
|
],
|
|
)
|
|
|
|
with patch("litellm.acreate_file", return_value=MagicMock()) as mock_acreate_file:
|
|
# Test with JSONL file
|
|
response = await router.acreate_file(
|
|
model="gpt-3.5-turbo-router",
|
|
purpose="batch",
|
|
file=jsonl_file,
|
|
)
|
|
|
|
# Verify mock was called twice (once for each deployment)
|
|
print(f"mock_acreate_file.call_count: {mock_acreate_file.call_count}")
|
|
print(f"mock_acreate_file.call_args_list: {mock_acreate_file.call_args_list}")
|
|
assert mock_acreate_file.call_count == 2
|
|
|
|
# Get the file content passed to the first call
|
|
first_call_file = mock_acreate_file.call_args_list[0][1]["file"]
|
|
first_call_content = first_call_file.read().decode("utf-8")
|
|
|
|
# Verify the model name was replaced in the JSONL content
|
|
first_line = json.loads(first_call_content.split("\n")[0])
|
|
assert first_line["body"]["model"] == "gpt-3.5-turbo"
|
|
|
|
# Reset mock for next test
|
|
mock_acreate_file.reset_mock()
|
|
|
|
# Test with non-JSONL file
|
|
response = await router.acreate_file(
|
|
model="gpt-3.5-turbo-router",
|
|
purpose="user_data",
|
|
file=non_jsonl_file,
|
|
)
|
|
|
|
# Verify mock was called twice
|
|
assert mock_acreate_file.call_count == 2
|
|
|
|
# Get the file content passed to the first call
|
|
first_call_file = mock_acreate_file.call_args_list[0][1]["file"]
|
|
first_call_content = first_call_file.read().decode("utf-8")
|
|
|
|
# Verify the non-JSONL content was not modified
|
|
assert first_call_content == non_jsonl_content
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"litellm_params, uploaded_rows",
|
|
[
|
|
({"model": "gpt-5.5"}, ["a", "c"]),
|
|
({"model": "vertex_ai/gemini-3.8-flash", "vertex_project": "p"}, [None, "a", None, "c"]),
|
|
],
|
|
ids=["openai", "vertex_ai"],
|
|
)
|
|
async def test_async_router_acreate_file_keeps_blank_lines_only_for_vertex(litellm_params, uploaded_rows):
|
|
import json
|
|
from io import BytesIO
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
content = b'\n{"custom_id": "a", "body": {"model": "batch-model"}}\n\n{"custom_id": "c", "body": {"model": "batch-model"}}\n'
|
|
router = litellm.Router(model_list=[{"model_name": "batch-model", "litellm_params": litellm_params}])
|
|
|
|
with patch("litellm.acreate_file", return_value=MagicMock()) as mock_acreate_file:
|
|
await router.acreate_file(model="batch-model", purpose="batch", file=BytesIO(content))
|
|
|
|
lines = mock_acreate_file.call_args.kwargs["file"].read().decode("utf-8").split("\n")
|
|
assert [json.loads(line)["custom_id"] if line else None for line in lines] == uploaded_rows
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_acreate_file_passthrough_keeps_the_file_and_forwards_the_flag():
|
|
"""A passthrough batch upload must reach the provider byte for byte: the router
|
|
neither rewrites body.model to the deployment model nor drops the flag."""
|
|
from io import BytesIO
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
jsonl_content = (
|
|
b'{"custom_id": "r1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "vertex-batch"}}\n'
|
|
)
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "vertex-batch",
|
|
"litellm_params": {"model": "vertex_ai/gemini-2.5-flash", "vertex_project": "p"},
|
|
}
|
|
],
|
|
)
|
|
|
|
with patch("litellm.acreate_file", return_value=MagicMock()) as mock_acreate_file:
|
|
await router.acreate_file(model="vertex-batch", purpose="batch", file=BytesIO(jsonl_content), passthrough=True)
|
|
forwarded = mock_acreate_file.call_args.kwargs
|
|
assert forwarded["passthrough"] is True
|
|
forwarded["file"].seek(0)
|
|
assert forwarded["file"].read() == jsonl_content
|
|
|
|
mock_acreate_file.reset_mock()
|
|
await router.acreate_file(model="vertex-batch", purpose="batch", file=BytesIO(jsonl_content))
|
|
rewritten = mock_acreate_file.call_args.kwargs["file"]
|
|
rewritten.seek(0)
|
|
assert b'"gemini-2.5-flash"' in rewritten.read()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_acreate_file_does_not_fall_back_across_model_groups():
|
|
"""A file created for batches only exists under the credentials of the model group
|
|
the caller named. A cross-group fallback silently stores it with the wrong provider
|
|
and the later batch create against the named group permanently fails."""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "azure-gpt",
|
|
"litellm_params": {
|
|
"model": "azure/my-azure-deployment",
|
|
"api_base": "http://127.0.0.1:9",
|
|
"api_key": "dummy-key",
|
|
"api_version": "2024-06-01",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "openai-gpt",
|
|
"litellm_params": {"model": "gpt-4o-mini"},
|
|
},
|
|
],
|
|
fallbacks=[{"azure-gpt": ["openai-gpt"]}],
|
|
)
|
|
|
|
def fail_azure(*args: object, **kwargs: object) -> MagicMock:
|
|
if kwargs.get("model") == "azure/my-azure-deployment":
|
|
raise litellm.APIConnectionError(
|
|
message="Connection error.",
|
|
llm_provider="azure",
|
|
model="azure/my-azure-deployment",
|
|
)
|
|
return MagicMock()
|
|
|
|
with patch("litellm.acreate_file", side_effect=fail_azure) as mock_acreate_file:
|
|
with pytest.raises(litellm.APIConnectionError):
|
|
await router.acreate_file(
|
|
model="azure-gpt",
|
|
purpose="batch",
|
|
file=MagicMock(),
|
|
)
|
|
|
|
called_models = [call.kwargs.get("model") for call in mock_acreate_file.call_args_list]
|
|
assert "azure/my-azure-deployment" in called_models
|
|
assert "gpt-4o-mini" not in called_models
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_acancel_batch_does_not_fall_back_across_model_groups(monkeypatch: pytest.MonkeyPatch):
|
|
"""The proxy cancels a managed batch by handing the router the deployment id decoded
|
|
from the unified batch id. A default (``*``) fallback matches that id like any other
|
|
model string, and the fallback provider is then asked to cancel a batch it never
|
|
issued, which can only answer not-found. The router re-raises the owner's error after
|
|
that wasted round trip, so the pin's observable is the foreign call never happening."""
|
|
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "azure-gpt",
|
|
"litellm_params": {
|
|
"model": "azure/my-azure-deployment",
|
|
"api_base": "http://127.0.0.1:9",
|
|
"api_key": "dummy-key",
|
|
"api_version": "2024-06-01",
|
|
},
|
|
"model_info": {"id": "azure-batch-dep"},
|
|
},
|
|
{
|
|
"model_name": "openai-gpt",
|
|
"litellm_params": {"model": "gpt-4o-mini", "api_key": "dummy-key"},
|
|
},
|
|
],
|
|
default_fallbacks=["openai-gpt"],
|
|
)
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
azure_route = respx_mock.post(host="127.0.0.1").mock(
|
|
return_value=httpx.Response(401, json={"error": {"code": "401", "message": "invalid subscription key"}})
|
|
)
|
|
openai_route = respx_mock.post("https://api.openai.com/v1/batches/batch_owned_by_azure/cancel").mock(
|
|
return_value=httpx.Response(
|
|
404,
|
|
json={
|
|
"error": {
|
|
"message": "No batch found with id 'batch_owned_by_azure'.",
|
|
"type": "invalid_request_error",
|
|
"code": "batch_not_found",
|
|
}
|
|
},
|
|
)
|
|
)
|
|
with pytest.raises(openai.AuthenticationError, match="invalid subscription key"):
|
|
await router.acancel_batch(model="azure-batch-dep", batch_id="batch_owned_by_azure")
|
|
|
|
assert azure_route.called
|
|
assert not openai_route.called
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_acreate_file_uses_deployment_custom_llm_provider():
|
|
"""
|
|
Ensure file routing preserves deployment custom_llm_provider instead of
|
|
inferring provider from model string alone.
|
|
"""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "team-azure-batch",
|
|
"litellm_params": {
|
|
"model": "gpt-4.1-mini",
|
|
"custom_llm_provider": "azure",
|
|
"api_base": "https://example-resource.openai.azure.com",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
with patch("litellm.acreate_file", return_value=MagicMock()) as mock_acreate_file:
|
|
await router.acreate_file(
|
|
model="team-azure-batch",
|
|
purpose="batch",
|
|
file=MagicMock(),
|
|
)
|
|
|
|
assert mock_acreate_file.call_count == 1
|
|
assert mock_acreate_file.call_args.kwargs["custom_llm_provider"] == "azure"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_acreate_file_forwards_target_model_names_to_litellm_proxy():
|
|
import json
|
|
from io import BytesIO
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
jsonl_file = BytesIO(
|
|
json.dumps({"body": {"model": "chained-batch", "messages": [{"role": "user", "content": "hi"}]}}).encode(
|
|
"utf-8"
|
|
)
|
|
)
|
|
jsonl_file.name = "test.jsonl"
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "chained-batch",
|
|
"litellm_params": {
|
|
"model": "litellm_proxy/gpt-4.1-batch",
|
|
"api_base": "http://localhost:4001/v1",
|
|
"api_key": "sk-proxy-b",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
with patch("litellm.acreate_file", return_value=MagicMock()) as mock_acreate_file:
|
|
await router.acreate_file(
|
|
model="chained-batch",
|
|
purpose="batch",
|
|
file=jsonl_file,
|
|
)
|
|
|
|
assert mock_acreate_file.call_count == 1
|
|
call_kwargs = mock_acreate_file.call_args.kwargs
|
|
assert call_kwargs["custom_llm_provider"] == "litellm_proxy"
|
|
assert call_kwargs["extra_body"] == {"target_model_names": "gpt-4.1-batch"}
|
|
uploaded_line = json.loads(call_kwargs["file"].read().decode("utf-8").split("\n")[0])
|
|
assert uploaded_line["body"]["model"] == "gpt-4.1-batch"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_acreate_file_does_not_inject_target_model_names_for_other_providers():
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4.1-batch",
|
|
"litellm_params": {"model": "gpt-4.1"},
|
|
},
|
|
],
|
|
)
|
|
|
|
with patch("litellm.acreate_file", return_value=MagicMock()) as mock_acreate_file:
|
|
await router.acreate_file(
|
|
model="gpt-4.1-batch",
|
|
purpose="batch",
|
|
file=MagicMock(),
|
|
)
|
|
|
|
assert mock_acreate_file.call_count == 1
|
|
assert mock_acreate_file.call_args.kwargs.get("extra_body") is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_acreate_file_litellm_proxy_sends_target_model_names_in_multipart_form():
|
|
import json
|
|
from io import BytesIO
|
|
|
|
import httpx
|
|
|
|
jsonl_file = BytesIO(
|
|
json.dumps({"body": {"model": "chained-batch", "messages": [{"role": "user", "content": "hi"}]}}).encode(
|
|
"utf-8"
|
|
)
|
|
)
|
|
jsonl_file.name = "test.jsonl"
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "chained-batch",
|
|
"litellm_params": {
|
|
"model": "litellm_proxy/gpt-4.1-batch",
|
|
"api_base": "http://localhost:4001/v1",
|
|
"api_key": "sk-proxy-b",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
file_object_json = {
|
|
"id": "file-abc123",
|
|
"object": "file",
|
|
"bytes": 100,
|
|
"created_at": 1700000000,
|
|
"filename": "test.jsonl",
|
|
"purpose": "batch",
|
|
"status": "processed",
|
|
}
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
create_route = respx_mock.post("http://localhost:4001/v1/files").mock(
|
|
return_value=httpx.Response(200, json=file_object_json)
|
|
)
|
|
response = await router.acreate_file(
|
|
model="chained-batch",
|
|
purpose="batch",
|
|
file=jsonl_file,
|
|
)
|
|
|
|
assert response.id == "file-abc123"
|
|
request_body = create_route.calls.last.request.content
|
|
assert b'name="target_model_names"' in request_body
|
|
assert b"gpt-4.1-batch" in request_body
|
|
assert b'name="purpose"' in request_body
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_afile_content_uses_deployment_custom_llm_provider():
|
|
"""
|
|
Regression test: Ensure afile_content preserves deployment custom_llm_provider
|
|
when model name lacks provider prefix (e.g., "gpt-4.1-mini" instead of "azure/gpt-4.1-mini").
|
|
|
|
This prevents "None is not a valid LlmProviders" errors when calling file content operations.
|
|
"""
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
from litellm.types.llms.openai import HttpxBinaryResponseContent
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "team-azure-batch",
|
|
"litellm_params": {
|
|
"model": "gpt-4.1-mini", # No provider prefix
|
|
"custom_llm_provider": "azure",
|
|
"api_base": "https://example-resource.openai.azure.com",
|
|
"api_key": "test-key",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
# Mock the Azure file handler's afile_content method
|
|
mock_response = MagicMock(spec=HttpxBinaryResponseContent)
|
|
mock_response.response = MagicMock()
|
|
|
|
with patch(
|
|
"litellm.llms.azure.files.handler.AzureOpenAIFilesAPI.afile_content",
|
|
return_value=mock_response,
|
|
) as mock_afile_content:
|
|
result = await router.afile_content(
|
|
model="team-azure-batch",
|
|
file_id="file-123",
|
|
)
|
|
|
|
# Verify the call was made (proves custom_llm_provider was correctly passed)
|
|
assert mock_afile_content.call_count == 1
|
|
assert result == mock_response
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_arouter_async_get_healthy_deployments():
|
|
"""
|
|
Test that afile_content returns the correct file content
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
},
|
|
],
|
|
)
|
|
|
|
result = await router.async_get_healthy_deployments(
|
|
model="gpt-3.5-turbo",
|
|
request_kwargs={},
|
|
messages=None,
|
|
input=None,
|
|
specific_deployment=False,
|
|
parent_otel_span=None,
|
|
)
|
|
|
|
assert len(result) == 1
|
|
assert result[0]["model_name"] == "gpt-3.5-turbo"
|
|
assert result[0]["litellm_params"]["model"] == "gpt-3.5-turbo"
|
|
|
|
|
|
def test_arouter_test_team_model():
|
|
"""
|
|
Test that router.test_team_model returns the correct model
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
"model_info": {
|
|
"team_id": "test-team",
|
|
"team_public_model_name": "test-model",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
result = router.map_team_model(team_model_name="test-model", team_id="test-team")
|
|
assert result is not None
|
|
|
|
|
|
def test_team_public_model_names_of_lists_only_that_teams_live_public_names():
|
|
def team_deployment(deployment_id: str, team_id: str, public_model_name: str) -> DeploymentTypedDict:
|
|
return {
|
|
"model_name": f"model_name_{team_id}_{deployment_id}",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test"},
|
|
"model_info": {"id": deployment_id, "team_id": team_id, "team_public_model_name": public_model_name},
|
|
}
|
|
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
team_deployment("team-a-1", "team-a", "shared-model"),
|
|
team_deployment("team-a-2", "team-a", "shared-model"),
|
|
team_deployment("team-a-solo", "team-a", "solo-model"),
|
|
team_deployment("team-b-1", "team-b", "team-b-model"),
|
|
{
|
|
"model_name": "plain-model",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test"},
|
|
"model_info": {"id": "plain-1"},
|
|
},
|
|
],
|
|
)
|
|
|
|
assert router.team_public_model_names_of("team-a") == frozenset({"shared-model", "solo-model"})
|
|
assert router.team_public_model_names_of("team-c") == frozenset()
|
|
|
|
router.delete_deployment(id="team-a-solo")
|
|
|
|
assert router.team_public_model_names_of("team-a") == frozenset({"shared-model"})
|
|
|
|
|
|
def test_team_model_has_alternatives():
|
|
def team_deployment(
|
|
deployment_id: str, team_id: str, public_model_name: str, blocked: bool = False
|
|
) -> DeploymentTypedDict:
|
|
return {
|
|
"model_name": f"model_name_{team_id}_{deployment_id}",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test"},
|
|
"model_info": {
|
|
"id": deployment_id,
|
|
"team_id": team_id,
|
|
"team_public_model_name": public_model_name,
|
|
"blocked": blocked,
|
|
},
|
|
}
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
team_deployment("team-a-1", "team-a", "shared-model"),
|
|
team_deployment("team-a-2", "team-a", "shared-model"),
|
|
team_deployment("team-a-solo", "team-a", "solo-model"),
|
|
team_deployment("team-b-1", "team-b", "shared-model"),
|
|
team_deployment("team-c-1", "team-c", "paused-sibling-model"),
|
|
team_deployment("team-c-paused", "team-c", "paused-sibling-model", blocked=True),
|
|
{
|
|
"model_name": "plain-model",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test"},
|
|
"model_info": {"id": "plain-1"},
|
|
},
|
|
],
|
|
)
|
|
|
|
assert router.team_model_has_alternatives("team-a-1") is True
|
|
assert router.team_model_has_alternatives("team-a-2") is True
|
|
assert router.team_model_has_alternatives("team-a-solo") is False
|
|
assert router.team_model_has_alternatives("team-b-1") is False
|
|
assert router.team_model_has_alternatives("team-c-1") is False
|
|
assert router.team_model_has_alternatives("plain-1") is False
|
|
assert router.team_model_has_alternatives("missing-deployment") is False
|
|
|
|
|
|
def test_arouter_ignore_invalid_deployments():
|
|
"""
|
|
Test that router.ignore_invalid_deployments is set to True
|
|
"""
|
|
from litellm.types.router import Deployment
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "my-bad-model"},
|
|
},
|
|
],
|
|
ignore_invalid_deployments=True,
|
|
)
|
|
|
|
assert router.ignore_invalid_deployments is True
|
|
assert router.get_model_list() == []
|
|
|
|
## check upsert deployment
|
|
router.upsert_deployment(
|
|
Deployment(
|
|
model_name="gpt-3.5-turbo",
|
|
litellm_params={"model": "my-bad-model"}, # type: ignore
|
|
model_info={"tpm": 1000, "rpm": 1000},
|
|
)
|
|
)
|
|
|
|
assert router.get_model_list() == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_arouter_aretrieve_batch():
|
|
"""
|
|
Test that router.aretrieve_batch returns the correct response
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"custom_llm_provider": "azure",
|
|
"api_key": "my-custom-key",
|
|
"api_base": "my-custom-base",
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
with patch.object(litellm, "aretrieve_batch", return_value=AsyncMock()) as mock_aretrieve_batch:
|
|
try:
|
|
response = await router.aretrieve_batch(
|
|
model="gpt-3.5-turbo",
|
|
)
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
mock_aretrieve_batch.assert_called_once()
|
|
|
|
print(mock_aretrieve_batch.call_args.kwargs)
|
|
assert mock_aretrieve_batch.call_args.kwargs["api_key"] == "my-custom-key"
|
|
assert mock_aretrieve_batch.call_args.kwargs["api_base"] == "my-custom-base"
|
|
|
|
|
|
_BATCH_GROUP = "gemini-batch-group"
|
|
_BATCH_DEPLOYMENT_MODEL = "openai/gpt-4o-mini"
|
|
_BATCH_API_BASE = "http://localhost:4001/v1"
|
|
_BATCH_ID = "batch-1"
|
|
_BATCH_ROWS = 2
|
|
_BATCH_TOKENS_PER_ROW = 600
|
|
|
|
_BATCH_COMPLETED = {
|
|
"id": _BATCH_ID,
|
|
"object": "batch",
|
|
"endpoint": "/v1/chat/completions",
|
|
"errors": None,
|
|
"input_file_id": "file-in-1",
|
|
"completion_window": "24h",
|
|
"status": "completed",
|
|
"output_file_id": "file-out-1",
|
|
"error_file_id": None,
|
|
"created_at": 0,
|
|
"completed_at": 1,
|
|
"request_counts": {"total": _BATCH_ROWS, "completed": _BATCH_ROWS, "failed": 0},
|
|
"metadata": None,
|
|
}
|
|
|
|
_BATCH_OUTPUT_JSONL = "\n".join(
|
|
json.dumps(
|
|
{
|
|
"id": f"req-{row}",
|
|
"custom_id": f"row-{row}",
|
|
"response": {
|
|
"status_code": 200,
|
|
"body": {
|
|
"id": f"chatcmpl-{row}",
|
|
"object": "chat.completion",
|
|
"model": "gpt-4o-mini",
|
|
"choices": [
|
|
{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}
|
|
],
|
|
"usage": {
|
|
"prompt_tokens": 500,
|
|
"completion_tokens": 100,
|
|
"total_tokens": _BATCH_TOKENS_PER_ROW,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
)
|
|
for row in range(_BATCH_ROWS)
|
|
)
|
|
|
|
|
|
class _BatchPayloadCollector(CustomLogger):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.payloads = []
|
|
|
|
async def async_log_success_event(self, kwargs, response_obj, start_time, end_time):
|
|
self.payloads.append(kwargs.get("standard_logging_object"))
|
|
|
|
async def retrieve_batch_payload(self):
|
|
for _ in range(100):
|
|
for payload in self.payloads:
|
|
if payload and payload.get("call_type") == "aretrieve_batch":
|
|
return payload
|
|
await asyncio.sleep(0.05)
|
|
raise AssertionError(f"no aretrieve_batch payload was emitted: {self.payloads}")
|
|
|
|
|
|
def _batch_model_group_router():
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": _BATCH_GROUP,
|
|
"litellm_params": {
|
|
"model": _BATCH_DEPLOYMENT_MODEL,
|
|
"api_base": _BATCH_API_BASE,
|
|
"api_key": "sk-fake",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
|
|
def _mock_batch_provider(respx_mock):
|
|
respx_mock.get(f"{_BATCH_API_BASE}/batches/{_BATCH_ID}").mock(
|
|
return_value=httpx.Response(200, json=_BATCH_COMPLETED)
|
|
)
|
|
respx_mock.get(f"{_BATCH_API_BASE}/files/file-out-1/content").mock(
|
|
return_value=httpx.Response(200, text=_BATCH_OUTPUT_JSONL)
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_arouter_aretrieve_batch_without_model_stamps_model_group(monkeypatch: pytest.MonkeyPatch):
|
|
"""
|
|
The proxy retrieves a managed batch by id only - no `model` in the request.
|
|
The router fans out over its deployments, so the model group is only known
|
|
from the deployment that answered.
|
|
"""
|
|
import respx
|
|
|
|
collector = _BatchPayloadCollector()
|
|
monkeypatch.setattr(litellm, "callbacks", [collector])
|
|
router = _batch_model_group_router()
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
_mock_batch_provider(respx_mock)
|
|
response = await router.aretrieve_batch(batch_id=_BATCH_ID)
|
|
payload = await collector.retrieve_batch_payload()
|
|
|
|
assert response.id == _BATCH_ID
|
|
assert payload["total_tokens"] == _BATCH_ROWS * _BATCH_TOKENS_PER_ROW
|
|
assert payload["model"] == _BATCH_DEPLOYMENT_MODEL
|
|
assert payload["model_group"] == _BATCH_GROUP
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_arouter_aretrieve_batch_with_model_stamps_requested_model_group(monkeypatch: pytest.MonkeyPatch):
|
|
"""An explicitly requested model group is what gets logged."""
|
|
import respx
|
|
|
|
collector = _BatchPayloadCollector()
|
|
monkeypatch.setattr(litellm, "callbacks", [collector])
|
|
router = _batch_model_group_router()
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
_mock_batch_provider(respx_mock)
|
|
await router.aretrieve_batch(model=_BATCH_GROUP, batch_id=_BATCH_ID)
|
|
payload = await collector.retrieve_batch_payload()
|
|
|
|
assert payload["model_group"] == _BATCH_GROUP
|
|
|
|
|
|
_UNRELATED_BATCH_GROUP = "unrelated-batch-group"
|
|
_UNRELATED_BATCH_API_BASE = "http://localhost:4002/v1"
|
|
|
|
_BATCH_NOT_FOUND = {
|
|
"error": {
|
|
"message": f"No batch found with id '{_BATCH_ID}'.",
|
|
"type": "invalid_request_error",
|
|
"code": "batch_not_found",
|
|
}
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_arouter_aretrieve_batch_does_not_consume_deployment_rate_limits(monkeypatch: pytest.MonkeyPatch):
|
|
"""
|
|
A batch reports the whole job's tokens on retrieve, and reports them again on every
|
|
poll of the finished batch, so they are not a measure of load in the current minute.
|
|
The fan-out also probes deployments the caller never named. Neither may reach the
|
|
per-minute tpm/rpm counters that gate live traffic.
|
|
"""
|
|
import respx
|
|
|
|
collector = _BatchPayloadCollector()
|
|
monkeypatch.setattr(litellm, "callbacks", [collector])
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": _BATCH_GROUP,
|
|
"litellm_params": {
|
|
"model": _BATCH_DEPLOYMENT_MODEL,
|
|
"api_base": _BATCH_API_BASE,
|
|
"api_key": "sk-fake",
|
|
},
|
|
"model_info": {"id": "batch-dep"},
|
|
"tpm": 1000,
|
|
"rpm": 10,
|
|
},
|
|
{
|
|
"model_name": _UNRELATED_BATCH_GROUP,
|
|
"litellm_params": {
|
|
"model": _BATCH_DEPLOYMENT_MODEL,
|
|
"api_base": _UNRELATED_BATCH_API_BASE,
|
|
"api_key": "sk-fake",
|
|
},
|
|
"model_info": {"id": "unrelated-dep"},
|
|
"tpm": 1000,
|
|
"rpm": 10,
|
|
},
|
|
]
|
|
)
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
_mock_batch_provider(respx_mock)
|
|
respx_mock.get(f"{_UNRELATED_BATCH_API_BASE}/batches/{_BATCH_ID}").mock(
|
|
return_value=httpx.Response(404, json=_BATCH_NOT_FOUND)
|
|
)
|
|
response = await router.aretrieve_batch(batch_id=_BATCH_ID)
|
|
payload = await collector.retrieve_batch_payload()
|
|
requested_usage: Final = await router.get_model_group_usage(_BATCH_GROUP)
|
|
unrelated_usage: Final = await router.get_model_group_usage(_UNRELATED_BATCH_GROUP)
|
|
|
|
assert response.id == _BATCH_ID
|
|
assert payload["model_group"] == _BATCH_GROUP
|
|
assert requested_usage == (None, None)
|
|
assert unrelated_usage == (None, None)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("call_type", "expected_key", "expected_successes"),
|
|
[
|
|
("aretrieve_batch", None, 0),
|
|
("retrieve_batch", None, 0),
|
|
("acompletion", "batch-dep:successes", 1),
|
|
],
|
|
)
|
|
def test_sync_deployment_callback_on_success_skips_batch_retrieves(
|
|
call_type: str, expected_key: str | None, expected_successes: int
|
|
):
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": _BATCH_GROUP,
|
|
"litellm_params": {"model": _BATCH_DEPLOYMENT_MODEL, "api_base": _BATCH_API_BASE, "api_key": "sk-fake"},
|
|
"model_info": {"id": "batch-dep"},
|
|
}
|
|
]
|
|
)
|
|
|
|
key = router.sync_deployment_callback_on_success(
|
|
kwargs={
|
|
"call_type": call_type,
|
|
"litellm_params": {"metadata": {"model_group": _BATCH_GROUP}, "model_info": {"id": "batch-dep"}},
|
|
},
|
|
completion_response=None,
|
|
start_time=datetime.now(),
|
|
end_time=datetime.now(),
|
|
)
|
|
|
|
assert key == expected_key
|
|
assert (
|
|
get_deployment_successes_for_current_minute(ctx=router._core_ctx, deployment_id="batch-dep")
|
|
== expected_successes
|
|
)
|
|
|
|
|
|
_ROUTING_STRATEGY_CACHE_MARKERS = ("_map", "_request_count", ":tpm:", ":rpm:")
|
|
|
|
|
|
async def _moved_routing_counters(router, timeout: float = 2.0) -> list[str]:
|
|
loop = asyncio.get_event_loop()
|
|
deadline = loop.time() + timeout
|
|
while loop.time() < deadline:
|
|
cache_dict = router.cache.in_memory_cache.cache_dict
|
|
moved = sorted(
|
|
f"{key}={cache_dict[key]}"
|
|
for key in cache_dict
|
|
if any(marker in key for marker in _ROUTING_STRATEGY_CACHE_MARKERS) and cache_dict[key]
|
|
)
|
|
if moved:
|
|
return moved
|
|
await asyncio.sleep(0.05)
|
|
return []
|
|
|
|
|
|
def _batch_fan_out_router(routing_strategy: str):
|
|
return litellm.Router(
|
|
routing_strategy=routing_strategy,
|
|
model_list=[
|
|
{
|
|
"model_name": _BATCH_GROUP,
|
|
"litellm_params": {
|
|
"model": _BATCH_DEPLOYMENT_MODEL,
|
|
"api_base": _BATCH_API_BASE,
|
|
"api_key": "sk-fake",
|
|
},
|
|
"model_info": {"id": "batch-dep"},
|
|
},
|
|
{
|
|
"model_name": _UNRELATED_BATCH_GROUP,
|
|
"litellm_params": {
|
|
"model": _BATCH_DEPLOYMENT_MODEL,
|
|
"api_base": _UNRELATED_BATCH_API_BASE,
|
|
"api_key": "sk-fake",
|
|
},
|
|
"model_info": {"id": "unrelated-dep"},
|
|
},
|
|
],
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"routing_strategy",
|
|
[
|
|
"usage-based-routing",
|
|
"usage-based-routing-v2",
|
|
"latency-based-routing",
|
|
"cost-based-routing",
|
|
"least-busy",
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_arouter_aretrieve_batch_does_not_feed_routing_strategies(
|
|
monkeypatch: pytest.MonkeyPatch, routing_strategy: str
|
|
):
|
|
"""
|
|
Every routing strategy picks a deployment from what recent live traffic did.
|
|
A batch retrieve reports the whole job on every poll and probes deployments the
|
|
caller never named, so polling a finished batch must not move the numbers that
|
|
decide where the next chat request goes.
|
|
"""
|
|
import respx
|
|
|
|
collector = _BatchPayloadCollector()
|
|
monkeypatch.setattr(litellm, "callbacks", [collector])
|
|
monkeypatch.setattr(litellm, "input_callback", [])
|
|
router = _batch_fan_out_router(routing_strategy)
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
_mock_batch_provider(respx_mock)
|
|
respx_mock.get(f"{_UNRELATED_BATCH_API_BASE}/batches/{_BATCH_ID}").mock(
|
|
return_value=httpx.Response(404, json=_BATCH_NOT_FOUND)
|
|
)
|
|
for _ in range(3):
|
|
response = await router.aretrieve_batch(batch_id=_BATCH_ID)
|
|
await collector.retrieve_batch_payload()
|
|
moved_counters = await _moved_routing_counters(router)
|
|
|
|
assert response.id == _BATCH_ID
|
|
assert moved_counters == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_arouter_aretrieve_file_content():
|
|
"""
|
|
Test that router.acreate_file with JSONL file returns the correct response
|
|
"""
|
|
|
|
with patch.object(litellm, "afile_content", return_value=AsyncMock()) as mock_afile_content:
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"custom_llm_provider": "azure",
|
|
"api_key": "my-custom-key",
|
|
"api_base": "my-custom-base",
|
|
},
|
|
}
|
|
],
|
|
)
|
|
try:
|
|
response = await router.afile_content(
|
|
**{
|
|
"model": "gpt-3.5-turbo",
|
|
"file_id": "my-unique-file-id",
|
|
}
|
|
) # type: ignore
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
mock_afile_content.assert_called_once()
|
|
|
|
print(mock_afile_content.call_args.kwargs)
|
|
assert mock_afile_content.call_args.kwargs["api_key"] == "my-custom-key"
|
|
assert mock_afile_content.call_args.kwargs["api_base"] == "my-custom-base"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_arouter_filter_team_based_models():
|
|
"""
|
|
Test that router.filter_team_based_models filters out models that are not in the team
|
|
"""
|
|
from litellm.types.router import Deployment
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
"model_info": {
|
|
"team_id": "test-team",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
# WORKS
|
|
result = await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hello, world!"}],
|
|
metadata={"user_api_key_team_id": "test-team"},
|
|
mock_response="Hello, world!",
|
|
)
|
|
|
|
assert result is not None
|
|
|
|
# FAILS
|
|
with pytest.raises(Exception, match="No deployments available for selected model, Try again in") as e:
|
|
result = await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hello, world!"}],
|
|
metadata={"user_api_key_team_id": "test-team-2"},
|
|
mock_response="Hello, world!",
|
|
)
|
|
assert "No deployments available" in str(e.value)
|
|
|
|
## ADD A MODEL THAT IS NOT IN THE TEAM
|
|
router.add_deployment(
|
|
Deployment(
|
|
model_name="gpt-3.5-turbo",
|
|
litellm_params={"model": "gpt-3.5-turbo"}, # type: ignore
|
|
model_info={"tpm": 1000, "rpm": 1000},
|
|
)
|
|
)
|
|
|
|
result = await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hello, world!"}],
|
|
metadata={"user_api_key_team_id": "test-team-2"},
|
|
mock_response="Hello, world!",
|
|
)
|
|
|
|
assert result is not None
|
|
|
|
|
|
def test_arouter_should_include_deployment():
|
|
"""
|
|
Test the should_include_deployment method with various scenarios
|
|
|
|
The method logic:
|
|
1. Returns True if: team_id matches AND model_name matches team_public_model_name
|
|
2. Returns True if: model_name matches AND deployment has no team_id
|
|
3. Otherwise returns False
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
"model_info": {
|
|
"team_id": "test-team",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
# Test deployment structures
|
|
deployment_with_team_and_public_name = {
|
|
"model_name": "gpt-3.5-turbo",
|
|
"model_info": {
|
|
"team_id": "test-team",
|
|
"team_public_model_name": "team-gpt-model",
|
|
},
|
|
}
|
|
|
|
deployment_with_team_no_public_name = {
|
|
"model_name": "gpt-3.5-turbo",
|
|
"model_info": {
|
|
"team_id": "test-team",
|
|
},
|
|
}
|
|
|
|
deployment_without_team = {
|
|
"model_name": "gpt-4",
|
|
"model_info": {},
|
|
}
|
|
|
|
deployment_different_team = {
|
|
"model_name": "claude-3",
|
|
"model_info": {
|
|
"team_id": "other-team",
|
|
"team_public_model_name": "team-claude-model",
|
|
},
|
|
}
|
|
|
|
# Test Case 1: Team-specific deployment - team_id and team_public_model_name match
|
|
result = router.should_include_deployment(
|
|
model_name="team-gpt-model",
|
|
model=deployment_with_team_and_public_name,
|
|
team_id="test-team",
|
|
)
|
|
assert result is True, "Should return True when team_id and team_public_model_name match"
|
|
|
|
# Test Case 2: Team-specific deployment - team_id matches but model_name doesn't match team_public_model_name
|
|
result = router.should_include_deployment(
|
|
model_name="different-model",
|
|
model=deployment_with_team_and_public_name,
|
|
team_id="test-team",
|
|
)
|
|
assert result is False, (
|
|
"Should return False when team_id matches but model_name doesn't match team_public_model_name"
|
|
)
|
|
|
|
# Test Case 3: Team-specific deployment - team_id doesn't match
|
|
result = router.should_include_deployment(
|
|
model_name="team-gpt-model",
|
|
model=deployment_with_team_and_public_name,
|
|
team_id="different-team",
|
|
)
|
|
assert result is False, "Should return False when team_id doesn't match"
|
|
|
|
# Test Case 4: Team-specific deployment with no team_public_model_name - should fail
|
|
result = router.should_include_deployment(
|
|
model_name="gpt-3.5-turbo",
|
|
model=deployment_with_team_no_public_name,
|
|
team_id="test-team",
|
|
)
|
|
assert result is True, "Should return True when team deployment has no team_public_model_name to match"
|
|
|
|
# Test Case 5: Non-team deployment - model_name matches and no team_id
|
|
result = router.should_include_deployment(model_name="gpt-4", model=deployment_without_team, team_id=None)
|
|
assert result is True, "Should return True when model_name matches and deployment has no team_id"
|
|
|
|
# Test Case 6: Non-team deployment - model_name matches but team_id provided (should still work)
|
|
result = router.should_include_deployment(model_name="gpt-4", model=deployment_without_team, team_id="any-team")
|
|
assert result is True, "Should return True when model_name matches non-team deployment, regardless of team_id param"
|
|
|
|
# Test Case 7: Non-team deployment - model_name doesn't match
|
|
result = router.should_include_deployment(model_name="different-model", model=deployment_without_team, team_id=None)
|
|
assert result is False, "Should return False when model_name doesn't match"
|
|
|
|
# Test Case 8: Team deployment accessed without matching team_id
|
|
result = router.should_include_deployment(
|
|
model_name="gpt-3.5-turbo",
|
|
model=deployment_with_team_and_public_name,
|
|
team_id=None,
|
|
)
|
|
assert result is True, "Should return True when matching model with exact model_name"
|
|
|
|
|
|
def test_arouter_responses_api_bridge():
|
|
"""
|
|
Test that router.responses_api_bridge returns the correct response
|
|
"""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from litellm.llms.custom_httpx.http_handler import HTTPHandler
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "[IP-approved] o3-pro",
|
|
"litellm_params": {
|
|
"model": "azure/responses/o_series/webinterface-o3-pro",
|
|
"api_base": "https://webhook.site/fba79dae-220a-4bb7-9a3a-8caa49604e55",
|
|
"api_key": "sk-9876567890",
|
|
"api_version": "preview",
|
|
"stream": True,
|
|
},
|
|
"model_info": {
|
|
"input_cost_per_token": 0.00002,
|
|
"output_cost_per_token": 0.00008,
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
## CONFIRM BRIDGE IS CALLED
|
|
# an empty stream: the Router reads the first chunk before returning, and a stream of mocks never produces one
|
|
with patch.object(litellm, "responses", return_value=iter([])) as mock_responses:
|
|
result = router.completion(
|
|
model="[IP-approved] o3-pro",
|
|
messages=[{"role": "user", "content": "Hello, world!"}],
|
|
)
|
|
assert mock_responses.call_count == 1
|
|
|
|
## CONFIRM MODEL NAME IS STRIPPED
|
|
client = HTTPHandler()
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.headers = {"content-type": "application/json"}
|
|
mock_response.json.return_value = {
|
|
"id": "resp_test",
|
|
"object": "response",
|
|
"status": "completed",
|
|
"output": [],
|
|
}
|
|
mock_response.text = '{"id": "resp_test", "object": "response", "status": "completed", "output": []}'
|
|
|
|
with patch.object(client, "post", return_value=mock_response) as mock_post:
|
|
try:
|
|
result = router.completion(
|
|
model="[IP-approved] o3-pro",
|
|
messages=[{"role": "user", "content": "Hello, world!"}],
|
|
client=client,
|
|
num_retries=0,
|
|
)
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
assert mock_post.call_count == 1
|
|
assert (
|
|
mock_post.call_args.kwargs["url"]
|
|
== "https://webhook.site/fba79dae-220a-4bb7-9a3a-8caa49604e55/openai/v1/responses?api-version=preview"
|
|
)
|
|
assert mock_post.call_args.kwargs["json"]["model"] == "webinterface-o3-pro"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_v1_messages_fallbacks():
|
|
"""
|
|
Test that router.v1_messages_fallbacks returns the correct response
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude-sonnet-4-5-20250929",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-sonnet-4-5-20250929",
|
|
"mock_response": "litellm.InternalServerError",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "bedrock-claude",
|
|
"litellm_params": {
|
|
"model": "anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
"mock_response": "Hello, world I am a fallback!",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[
|
|
{"claude-sonnet-4-5-20250929": ["bedrock-claude"]},
|
|
],
|
|
)
|
|
|
|
result = await router.aanthropic_messages(
|
|
model="claude-sonnet-4-5-20250929",
|
|
messages=[{"role": "user", "content": "Hello, world!"}],
|
|
max_tokens=256,
|
|
)
|
|
assert result is not None
|
|
|
|
print(result)
|
|
assert result["content"][0]["text"] == "Hello, world I am a fallback!"
|
|
|
|
|
|
def test_add_invalid_provider_to_router():
|
|
"""
|
|
Test that router.add_deployment raises an error if the provider is invalid
|
|
"""
|
|
from litellm.types.router import Deployment
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
}
|
|
],
|
|
)
|
|
|
|
with pytest.raises(Exception, match="Unsupported provider - vertex_ai_eu") as e:
|
|
router.add_deployment(
|
|
Deployment(
|
|
model_name="vertex_ai/*",
|
|
litellm_params={
|
|
"model": "vertex_ai/*",
|
|
"custom_llm_provider": "vertex_ai_eu",
|
|
},
|
|
)
|
|
)
|
|
|
|
assert router.pattern_router.patterns == {}
|
|
|
|
|
|
@pytest.fixture
|
|
def registered_custom_provider(monkeypatch: pytest.MonkeyPatch) -> str:
|
|
from litellm import CustomLLM
|
|
from litellm.types.utils import ModelResponse
|
|
|
|
class OnPremLLM(CustomLLM):
|
|
def completion(self, *args, **kwargs) -> ModelResponse:
|
|
return litellm.completion(
|
|
model="gpt-5.6", messages=[{"role": "user", "content": "hi"}], mock_response="served by onprem handler"
|
|
)
|
|
|
|
monkeypatch.setattr(
|
|
litellm, "custom_provider_map", [{"provider": "test-onprem-llm", "custom_handler": OnPremLLM()}]
|
|
)
|
|
monkeypatch.setattr(litellm, "provider_list", list(litellm.provider_list))
|
|
monkeypatch.setattr(litellm, "_custom_providers", list(litellm._custom_providers))
|
|
return "test-onprem-llm"
|
|
|
|
|
|
def test_router_init_accepts_custom_provider_map_prefix_before_first_completion(registered_custom_provider: str):
|
|
assert registered_custom_provider not in litellm.provider_list
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "onprem", "litellm_params": {"model": f"{registered_custom_provider}/my-model"}},
|
|
],
|
|
)
|
|
|
|
assert router.get_model_list(model_name="onprem")[0]["litellm_params"]["model"] == (
|
|
f"{registered_custom_provider}/my-model"
|
|
)
|
|
response = router.completion(model="onprem", messages=[{"role": "user", "content": "hi"}])
|
|
assert response.choices[0].message.content == "served by onprem handler"
|
|
|
|
|
|
def test_router_add_deployment_accepts_explicit_custom_provider_from_custom_provider_map(
|
|
registered_custom_provider: str,
|
|
):
|
|
from litellm.types.router import Deployment
|
|
|
|
router = litellm.Router(model_list=[])
|
|
|
|
router.add_deployment(
|
|
Deployment(
|
|
model_name="onprem",
|
|
litellm_params={"model": "my-model", "custom_llm_provider": registered_custom_provider},
|
|
)
|
|
)
|
|
|
|
assert router.get_model_list(model_name="onprem")[0]["litellm_params"]["model"] == "my-model"
|
|
|
|
|
|
def test_router_get_model_access_groups_team_only_models():
|
|
"""
|
|
Test that Router._core_get_model_access_groups returns the correct response for team-only models
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "my-custom-model-name",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
"model_info": {
|
|
"team_id": "team_1",
|
|
"access_groups": ["default-models"],
|
|
"team_public_model_name": "gpt-3.5-turbo",
|
|
},
|
|
},
|
|
]
|
|
)
|
|
|
|
access_groups = router.get_model_access_groups(model_name="gpt-3.5-turbo", team_id=None)
|
|
assert len(access_groups) == 0
|
|
|
|
access_groups = router.get_model_access_groups(model_name="gpt-3.5-turbo", team_id="team_1")
|
|
assert list(access_groups.keys()) == ["default-models"]
|
|
|
|
|
|
def test_model_group_info_cost_none_for_unpriced_deployment_but_zero_when_declared():
|
|
"""A deployment with no cost fields anywhere must report None, not the 0 that
|
|
get_model_info defaults to, so the reported price matches what the zero-cost
|
|
budget bypass accepts. A deployment declaring 0 keeps reporting 0."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "vllm-unpriced",
|
|
"litellm_params": {
|
|
"model": "openai/my-vllm-unpriced",
|
|
"api_key": "fake",
|
|
"api_base": "http://localhost:8000/v1",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "vllm-free",
|
|
"litellm_params": {
|
|
"model": "openai/my-vllm-free",
|
|
"api_key": "fake",
|
|
"api_base": "http://localhost:8000/v1",
|
|
"input_cost_per_token": 0,
|
|
"output_cost_per_token": 0,
|
|
},
|
|
},
|
|
{
|
|
"model_name": "gpt-priced",
|
|
"litellm_params": {"model": "gpt-4o", "api_key": "fake"},
|
|
},
|
|
]
|
|
)
|
|
|
|
unpriced = router.get_model_group_info(model_group="vllm-unpriced")
|
|
assert unpriced is not None
|
|
assert unpriced.input_cost_per_token is None
|
|
assert unpriced.output_cost_per_token is None
|
|
|
|
free = router.get_model_group_info(model_group="vllm-free")
|
|
assert free is not None
|
|
assert free.input_cost_per_token == 0
|
|
assert free.output_cost_per_token == 0
|
|
|
|
priced = router.get_model_group_info(model_group="gpt-priced")
|
|
assert priced is not None
|
|
assert priced.input_cost_per_token is not None and priced.input_cost_per_token > 0
|
|
assert priced.output_cost_per_token is not None and priced.output_cost_per_token > 0
|
|
|
|
|
|
_HUB_PRICES: Final = MappingProxyType(
|
|
{
|
|
"input_cost_per_token": 1e-06,
|
|
"output_cost_per_token": 4e-06,
|
|
"cache_read_input_token_cost": 1e-07,
|
|
"cache_creation_input_token_cost": 1.25e-06,
|
|
"input_cost_per_token_above_200k_tokens": 2e-06,
|
|
"output_cost_per_token_above_200k_tokens": 8e-06,
|
|
"input_cost_per_token_priority": 3e-06,
|
|
"input_cost_per_token_flex": 5e-07,
|
|
"input_cost_per_token_batches": 5e-07,
|
|
}
|
|
)
|
|
|
|
|
|
def _cost_map_model(key: str, entry: Mapping[str, object]) -> str:
|
|
litellm.register_model(
|
|
{key: {"litellm_provider": "openai", "mode": "chat", **entry}},
|
|
persist_across_reloads=False,
|
|
add_to_provider_model_sets=False,
|
|
)
|
|
return f"openai/{key}"
|
|
|
|
|
|
def test_model_group_info_lists_every_cost_map_price_of_a_deployment(local_model_cost_map: None) -> None:
|
|
model: Final = _cost_map_model("hub-pricing-model", {**_HUB_PRICES, "max_input_tokens": 400000})
|
|
router: Final = litellm.Router(model_list=[{"model_name": "hub", "litellm_params": {"model": model}}])
|
|
|
|
group: Final = router.registry().group("hub")
|
|
|
|
assert group is not None
|
|
assert [(p.provider, dict(p.costs), p.tiered_pricing) for p in group.deployment_pricing] == [
|
|
("openai", dict(_HUB_PRICES), ())
|
|
]
|
|
|
|
|
|
def test_model_group_info_lists_each_deployments_price_once_next_to_the_unchanged_max(
|
|
local_model_cost_map: None,
|
|
) -> None:
|
|
model: Final = _cost_map_model("hub-pricing-model", _HUB_PRICES)
|
|
override: Final = 3 * _HUB_PRICES["input_cost_per_token"]
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "hub", "litellm_params": {"model": model, "api_base": "https://a.example"}},
|
|
{"model_name": "hub", "litellm_params": {"model": model, "api_base": "https://b.example"}},
|
|
{"model_name": "hub", "litellm_params": {"model": model, "input_cost_per_token": override}},
|
|
]
|
|
)
|
|
|
|
group: Final = router.registry().group("hub")
|
|
|
|
assert group is not None
|
|
assert [p.costs["input_cost_per_token"] for p in group.deployment_pricing] == [
|
|
_HUB_PRICES["input_cost_per_token"],
|
|
override,
|
|
]
|
|
assert group.input_cost_per_token == override
|
|
assert group.output_cost_per_token == _HUB_PRICES["output_cost_per_token"]
|
|
|
|
|
|
def _usage_billed_at(cost_key: str, prompt_tokens: int = 1000) -> tuple[Usage, str | None]:
|
|
match cost_key:
|
|
case "input_cost_per_token":
|
|
return Usage(prompt_tokens=prompt_tokens, completion_tokens=0, total_tokens=prompt_tokens), None
|
|
case "output_cost_per_token":
|
|
return Usage(prompt_tokens=0, completion_tokens=1000, total_tokens=1000), None
|
|
case "cache_read_input_token_cost":
|
|
return Usage(
|
|
prompt_tokens=1000,
|
|
completion_tokens=0,
|
|
total_tokens=1000,
|
|
prompt_tokens_details={"cached_tokens": 1000},
|
|
), None
|
|
case "cache_creation_input_token_cost":
|
|
return Usage(
|
|
prompt_tokens=1000, completion_tokens=0, total_tokens=1000, cache_creation_input_tokens=1000
|
|
), None
|
|
case "input_cost_per_token_priority":
|
|
return Usage(prompt_tokens=1000, completion_tokens=0, total_tokens=1000), "priority"
|
|
case "input_cost_per_token_flex":
|
|
return Usage(prompt_tokens=1000, completion_tokens=0, total_tokens=1000), "flex"
|
|
case _:
|
|
raise AssertionError(f"no billing scenario for {cost_key}")
|
|
|
|
|
|
def _billed_rate(model: str, deployment_id: str, usage: Usage, service_tier: str | None) -> float:
|
|
cost: Final = litellm.completion_cost(
|
|
completion_response=litellm.ModelResponse(usage=usage),
|
|
model=model,
|
|
custom_llm_provider="openai",
|
|
custom_pricing=True,
|
|
router_model_id=deployment_id,
|
|
service_tier=service_tier,
|
|
)
|
|
return cost / max(usage.prompt_tokens, usage.completion_tokens)
|
|
|
|
|
|
def test_model_group_info_shows_an_overridden_deployment_only_the_prices_billing_charges(
|
|
local_model_cost_map: None,
|
|
) -> None:
|
|
model: Final = _cost_map_model("hub-pricing-model", _HUB_PRICES)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "hub",
|
|
"litellm_params": {
|
|
"model": model,
|
|
"input_cost_per_token": 3 * _HUB_PRICES["input_cost_per_token"],
|
|
"output_cost_per_token": 3 * _HUB_PRICES["output_cost_per_token"],
|
|
},
|
|
"model_info": {"id": "hub-override"},
|
|
}
|
|
]
|
|
)
|
|
|
|
group: Final = router.registry().group("hub")
|
|
|
|
assert group is not None
|
|
costs: Final = group.deployment_pricing[0].costs
|
|
assert {key: _billed_rate(model, "hub-override", *_usage_billed_at(key)) for key in costs} == pytest.approx(
|
|
dict(costs)
|
|
)
|
|
long_prompt: Final = _usage_billed_at("input_cost_per_token", prompt_tokens=250_000)
|
|
assert _billed_rate(model, "hub-override", *long_prompt) == pytest.approx(costs["input_cost_per_token"])
|
|
shadowed: Final = {
|
|
"input_cost_per_token_above_200k_tokens",
|
|
"output_cost_per_token_above_200k_tokens",
|
|
"input_cost_per_token_batches",
|
|
}
|
|
assert shadowed.isdisjoint(costs), dict(costs)
|
|
|
|
|
|
@pytest.mark.parametrize("vertex_location", ["us-east5", "global"])
|
|
def test_model_group_info_shows_a_vertex_deployment_the_token_prices_billing_charges_at_its_location(
|
|
local_model_cost_map: None, vertex_location: str
|
|
) -> None:
|
|
model: Final = "vertex_ai/hub-vertex-model"
|
|
litellm.register_model(
|
|
{
|
|
model: {
|
|
"litellm_provider": "vertex_ai-language-models",
|
|
"mode": "chat",
|
|
"input_cost_per_token": 1e-06,
|
|
"output_cost_per_token": 4e-06,
|
|
"cache_read_input_token_cost": 1e-07,
|
|
"regional_endpoint_uplift_multiplier": 1.1,
|
|
}
|
|
},
|
|
persist_across_reloads=False,
|
|
add_to_provider_model_sets=False,
|
|
)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "hub",
|
|
"litellm_params": {"model": model, "vertex_project": "p", "vertex_location": vertex_location},
|
|
"model_info": {"id": "hub-vertex"},
|
|
}
|
|
]
|
|
)
|
|
|
|
group: Final = router.registry().group("hub")
|
|
|
|
assert group is not None
|
|
costs: Final = group.deployment_pricing[0].costs
|
|
|
|
def billed(cost_key: str) -> float:
|
|
usage, _ = _usage_billed_at(cost_key)
|
|
cost: Final = litellm.completion_cost(
|
|
completion_response=litellm.ModelResponse(usage=usage),
|
|
model=model,
|
|
custom_llm_provider="vertex_ai",
|
|
router_model_id="hub-vertex",
|
|
vertex_location=vertex_location,
|
|
)
|
|
return cost / max(usage.prompt_tokens, usage.completion_tokens)
|
|
|
|
shown: Final = ("input_cost_per_token", "output_cost_per_token", "cache_read_input_token_cost")
|
|
assert {key: costs[key] for key in shown} == pytest.approx({key: billed(key) for key in shown})
|
|
|
|
|
|
def test_model_group_info_lists_tiered_pricing_ranges(local_model_cost_map: None) -> None:
|
|
tiers: Final = (
|
|
{"range": [0, 256000], "input_cost_per_token": 5e-08, "output_cost_per_token": 4e-07},
|
|
{"range": [256000, 1000000], "input_cost_per_token": 2.5e-07, "output_cost_per_token": 2e-06},
|
|
)
|
|
model: Final = _cost_map_model("hub-tiered-model", {"tiered_pricing": list(tiers)})
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "tiered", "litellm_params": {"model": model}},
|
|
{
|
|
"model_name": "tiered",
|
|
"litellm_params": {"model": model, "input_cost_per_token": 1e-06, "output_cost_per_token": 4e-06},
|
|
},
|
|
]
|
|
)
|
|
|
|
group: Final = router.registry().group("tiered")
|
|
|
|
assert group is not None
|
|
assert [
|
|
[(tier.range, dict(tier.costs)) for tier in pricing.tiered_pricing] for pricing in group.deployment_pricing
|
|
] == [
|
|
[
|
|
((0.0, 256000.0), {"input_cost_per_token": 5e-08, "output_cost_per_token": 4e-07}),
|
|
((256000.0, 1000000.0), {"input_cost_per_token": 2.5e-07, "output_cost_per_token": 2e-06}),
|
|
],
|
|
[],
|
|
]
|
|
|
|
|
|
def test_model_group_info_reads_prices_written_as_text_the_way_billing_does(local_model_cost_map: None) -> None:
|
|
tier: Final = {"range": [0, 256000], "input_cost_per_token": "5e-08", "output_cost_per_token": "4e-07"}
|
|
model: Final = _cost_map_model(
|
|
"hub-text-priced-model", {"tiered_pricing": [tier], "cache_read_input_token_cost": "N/A"}
|
|
)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "tiered", "litellm_params": {"model": model}},
|
|
{
|
|
"model_name": "flat",
|
|
"litellm_params": {"model": model, "input_cost_per_token": "3e-7", "output_cost_per_token": "1.2e-6"},
|
|
"model_info": {"id": "hub-text-override"},
|
|
},
|
|
]
|
|
)
|
|
|
|
tiered: Final = router.registry().group("tiered")
|
|
flat: Final = router.registry().group("flat")
|
|
|
|
assert tiered is not None
|
|
assert flat is not None
|
|
assert [dict(t.costs) for t in tiered.deployment_pricing[0].tiered_pricing] == [
|
|
{"input_cost_per_token": 5e-08, "output_cost_per_token": 4e-07}
|
|
]
|
|
assert "cache_read_input_token_cost" not in tiered.deployment_pricing[0].costs
|
|
flat_costs: Final = flat.deployment_pricing[0].costs
|
|
shown: Final = ("input_cost_per_token", "output_cost_per_token")
|
|
assert {key: flat_costs[key] for key in shown} == pytest.approx(
|
|
{key: _billed_rate(model, "hub-text-override", *_usage_billed_at(key)) for key in shown}
|
|
)
|
|
|
|
|
|
def test_model_group_info_lists_custom_prices_of_an_unmapped_deployment_and_skips_an_unpriced_one() -> None:
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "self-hosted",
|
|
"litellm_params": {"model": "openai/hub-unpriced", "api_base": "http://localhost:8000/v1"},
|
|
"model_info": {"cost_tracking_exempt": True},
|
|
},
|
|
{
|
|
"model_name": "self-hosted",
|
|
"litellm_params": {
|
|
"model": "openai/hub-priced",
|
|
"api_base": "http://localhost:8001/v1",
|
|
"input_cost_per_token": 1e-06,
|
|
"output_cost_per_token": 2e-06,
|
|
},
|
|
"model_info": {"cache_read_input_token_cost": 1e-07},
|
|
},
|
|
]
|
|
)
|
|
|
|
group: Final = router.registry().group("self-hosted")
|
|
|
|
assert group is not None
|
|
assert [(p.provider, dict(p.costs)) for p in group.deployment_pricing] == [
|
|
(
|
|
"openai",
|
|
{"input_cost_per_token": 1e-06, "output_cost_per_token": 2e-06, "cache_read_input_token_cost": 1e-07},
|
|
)
|
|
]
|
|
|
|
|
|
def test_model_group_info_description_from_model_info():
|
|
"""
|
|
model_info.description set in the config should surface on ModelGroupInfo,
|
|
including when only a later deployment in the group carries it.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4",
|
|
"litellm_params": {"model": "gpt-4", "api_key": "fake"},
|
|
"model_info": {},
|
|
},
|
|
{
|
|
"model_name": "gpt-4",
|
|
"litellm_params": {"model": "gpt-4", "api_key": "fake2"},
|
|
"model_info": {"description": "State-of-the-art language model."},
|
|
},
|
|
]
|
|
)
|
|
|
|
result = router.get_model_group_info("gpt-4")
|
|
assert result is not None
|
|
assert result.description == "State-of-the-art language model."
|
|
|
|
|
|
@pytest.mark.parametrize("chat_deployment_first", [True, False])
|
|
def test_model_group_info_lists_batch_for_a_mixed_chat_and_batch_group(chat_deployment_first: bool):
|
|
chat: Final = _azure_sku_deployment(
|
|
"gpt-mixed", "unmapped-chat-sku", "chat", supported_endpoints=["/v1/chat/completions"]
|
|
)
|
|
batch: Final = _azure_sku_deployment("gpt-mixed", "unmapped-batch-sku", "batch")
|
|
router: Final = Router(model_list=[chat, batch] if chat_deployment_first else [batch, chat])
|
|
|
|
info: Final = router.get_model_group_info("gpt-mixed")
|
|
|
|
assert info is not None
|
|
assert sorted(info.supported_endpoints or ()) == ["/v1/batch", "/v1/chat/completions"]
|
|
|
|
|
|
def test_model_group_info_lists_no_batch_for_a_chat_only_group():
|
|
router: Final = Router(
|
|
model_list=[
|
|
_azure_sku_deployment(
|
|
"gpt-declared", "unmapped-chat-sku", "chat", supported_endpoints=["/v1/chat/completions"]
|
|
),
|
|
_azure_sku_deployment("gpt-undeclared", "unmapped-undeclared-sku", "chat"),
|
|
]
|
|
)
|
|
|
|
declared: Final = router.get_model_group_info("gpt-declared")
|
|
undeclared: Final = router.get_model_group_info("gpt-undeclared")
|
|
|
|
assert declared is not None and declared.supported_endpoints == ("/v1/chat/completions",)
|
|
assert undeclared is not None and undeclared.supported_endpoints is None
|
|
|
|
|
|
@pytest.mark.parametrize(("provider", "lists_batch"), [("azure", False), ("azure_ai", False), ("openai", True)])
|
|
def test_model_group_info_lists_cost_map_batch_only_for_providers_that_batch_on_any_deployment(
|
|
provider: str, lists_batch: bool
|
|
):
|
|
"""Azure runs batches only on a separate batch deployment, though the cost map lists /v1/batch for its chat
|
|
models, so an Azure chat group must not advertise it. OpenAI batches through the deployment that serves chat,
|
|
so its cost map listing stands."""
|
|
cost_map_name, cost_map_endpoints = next(
|
|
(name, tuple(info["supported_endpoints"]))
|
|
for name, info in litellm.model_cost.items()
|
|
if info.get("litellm_provider") == provider
|
|
and info.get("mode") == "chat"
|
|
and "/v1/batch" in (info.get("supported_endpoints") or ())
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-chat",
|
|
"litellm_params": {"model": f"{provider}/{cost_map_name.removeprefix(f'{provider}/')}"},
|
|
}
|
|
]
|
|
)
|
|
|
|
info: Final = router.get_model_group_info("gpt-chat")
|
|
|
|
assert info is not None
|
|
assert info.supported_endpoints == tuple(
|
|
endpoint for endpoint in cost_map_endpoints if lists_batch or endpoint != "/v1/batch"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"malformed_endpoints",
|
|
["/v1/chat/completions", [{"path": "/v1/chat/completions"}], [1], {"/v1/batch": True}],
|
|
)
|
|
def test_model_group_info_skips_a_malformed_endpoint_list_instead_of_failing(malformed_endpoints: object):
|
|
router: Final = Router(
|
|
model_list=[
|
|
_azure_sku_deployment("gpt-mixed", "chat-sku", "chat", supported_endpoints=["/v1/chat/completions"]),
|
|
_azure_sku_deployment("gpt-mixed", "odd-sku", "chat", supported_endpoints=malformed_endpoints),
|
|
]
|
|
)
|
|
|
|
info: Final = router.get_model_group_info("gpt-mixed")
|
|
|
|
assert info is not None and info.supported_endpoints == ("/v1/chat/completions",)
|
|
|
|
|
|
def test_model_group_info_ignores_a_stored_batch_endpoint_on_an_azure_chat_deployment():
|
|
"""Admin UI saves before v1.104.0 stored the cost map's endpoint list, /v1/batch included, on Azure chat rows,
|
|
so a stored list cannot mark a deployment as one that runs batches."""
|
|
router: Final = Router(
|
|
model_list=[
|
|
_azure_sku_deployment(
|
|
"gpt-stored-endpoints",
|
|
"unmapped-chat-sku",
|
|
"chat",
|
|
supported_endpoints=["/v1/chat/completions", "/v1/batch"],
|
|
)
|
|
]
|
|
)
|
|
|
|
info: Final = router.get_model_group_info("gpt-stored-endpoints")
|
|
|
|
assert info is not None and info.supported_endpoints == ("/v1/chat/completions",)
|
|
|
|
|
|
@pytest.mark.parametrize(("team_id", "lists_batch"), [(None, False), ("team-b", False), ("team-a", True)])
|
|
def test_model_group_info_lists_batch_only_to_callers_that_may_use_the_batch_deployment(
|
|
team_id: str | None, lists_batch: bool
|
|
):
|
|
router: Final = Router(
|
|
model_list=[
|
|
_azure_sku_deployment("gpt-mixed", "chat-sku", "chat", supported_endpoints=["/v1/chat/completions"]),
|
|
_azure_sku_deployment("gpt-mixed", "batch-sku", "batch", team_id="team-a"),
|
|
]
|
|
)
|
|
|
|
info: Final = router.get_model_group_info("gpt-mixed", team_id=team_id)
|
|
|
|
assert info is not None
|
|
assert ("/v1/batch" in (info.supported_endpoints or ())) is lists_batch
|
|
|
|
|
|
def test_model_group_info_lists_only_batch_for_an_azure_batch_deployment():
|
|
"""An Azure batch deployment answers chat, Responses, and Messages calls with 400 "The current operation is not
|
|
allowed in this deployment", so a group of them must advertise only /v1/batch whatever the endpoint list says."""
|
|
router: Final = Router(
|
|
model_list=[
|
|
_azure_sku_deployment(
|
|
"gpt-batch-only",
|
|
"batch-sku",
|
|
"batch",
|
|
supported_endpoints=["/v1/chat/completions", "/v1/responses", "/v1/batch"],
|
|
)
|
|
]
|
|
)
|
|
|
|
info: Final = router.get_model_group_info("gpt-batch-only")
|
|
|
|
assert info is not None and info.supported_endpoints == ("/v1/batch",)
|
|
|
|
|
|
def _alias_cost_router() -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "vllm-free",
|
|
"litellm_params": {
|
|
"model": "openai/my-vllm-free",
|
|
"api_key": "fake",
|
|
"api_base": "http://localhost:8000/v1",
|
|
"input_cost_per_token": 0,
|
|
"output_cost_per_token": 0,
|
|
},
|
|
},
|
|
{
|
|
"model_name": "gpt-priced",
|
|
"litellm_params": {"model": "gpt-4o", "api_key": "fake"},
|
|
},
|
|
],
|
|
model_group_alias={"hidden-free": {"model": "vllm-free", "hidden": True}, "visible": "vllm-free"},
|
|
)
|
|
|
|
|
|
def test_get_model_group_info_include_hidden_resolves_a_hidden_alias():
|
|
router = _alias_cost_router()
|
|
|
|
assert router.get_model_group_info(model_group="hidden-free") is None
|
|
|
|
hidden: Final = router.get_model_group_info(model_group="hidden-free", include_hidden=True)
|
|
assert hidden is not None
|
|
assert hidden.model_group == "hidden-free"
|
|
assert hidden.input_cost_per_token == 0
|
|
assert hidden.output_cost_per_token == 0
|
|
|
|
|
|
def test_update_settings_model_group_alias_drops_cached_group_info():
|
|
router = _alias_cost_router()
|
|
before: Final = router.cached_model_group_info("visible")
|
|
assert before is not None and before.input_cost_per_token == 0
|
|
|
|
router.update_settings(model_group_alias={"visible": "gpt-priced"})
|
|
|
|
after: Final = router.cached_model_group_info("visible")
|
|
assert after is not None
|
|
assert after.input_cost_per_token is not None and after.input_cost_per_token > 0
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"fallback_kind, mock_testing_param",
|
|
[
|
|
("fallbacks", "mock_testing_fallbacks"),
|
|
("context_window_fallbacks", "mock_testing_context_fallbacks"),
|
|
("content_policy_fallbacks", "mock_testing_content_policy_fallbacks"),
|
|
],
|
|
)
|
|
async def test_update_settings_fallback_rule_of_every_kind_is_used_on_its_error(
|
|
fallback_kind: str, mock_testing_param: str
|
|
):
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {"model": "gpt-5.4-mini", "api_key": "fake", "mock_response": "primary answered"},
|
|
},
|
|
{
|
|
"model_name": "backup",
|
|
"litellm_params": {"model": "gpt-5.4-nano", "api_key": "fake", "mock_response": "backup answered"},
|
|
},
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
router.update_settings(**{fallback_kind: [{"primary": ["backup"]}]})
|
|
|
|
response: Final = await router.acompletion(
|
|
model="primary", messages=[{"role": "user", "content": "hi"}], **{mock_testing_param: True}
|
|
)
|
|
assert response.choices[0].message.content == "backup answered"
|
|
|
|
|
|
_PAID_INPUT_COST_PER_TOKEN: Final = 3e-06
|
|
_PAID_OUTPUT_COST_PER_TOKEN: Final = 1.5e-05
|
|
|
|
|
|
def _free_ollama_deployment(model_name: str) -> dict:
|
|
return {
|
|
"model_name": model_name,
|
|
"litellm_params": {
|
|
"model": "ollama/qwen3:0.6b",
|
|
"api_base": "http://localhost:11434",
|
|
"input_cost_per_token": 0,
|
|
"output_cost_per_token": 0,
|
|
},
|
|
}
|
|
|
|
|
|
def _paid_openai_deployment(model_name: str, model: str) -> dict:
|
|
return {
|
|
"model_name": model_name,
|
|
"litellm_params": {
|
|
"model": model,
|
|
"api_key": "fake",
|
|
"input_cost_per_token": _PAID_INPUT_COST_PER_TOKEN,
|
|
"output_cost_per_token": _PAID_OUTPUT_COST_PER_TOKEN,
|
|
},
|
|
}
|
|
|
|
|
|
def _assert_priced(info: ModelGroupInfo | None, provider: str) -> None:
|
|
assert info is not None
|
|
assert info.providers == [provider]
|
|
assert info.input_cost_per_token == _PAID_INPUT_COST_PER_TOKEN
|
|
assert info.output_cost_per_token == _PAID_OUTPUT_COST_PER_TOKEN
|
|
|
|
|
|
def _assert_free(info: ModelGroupInfo | None, provider: str) -> None:
|
|
assert info is not None
|
|
assert info.providers == [provider]
|
|
assert info.input_cost_per_token == 0
|
|
assert info.output_cost_per_token == 0
|
|
|
|
|
|
def test_get_model_group_info_prices_an_alias_chain_from_the_group_it_routes_to():
|
|
"""chain-entry resolves one hop to local-free and is served by local-free's own
|
|
deployment, so its price is that deployment's; local-free's own alias to gpt-priced
|
|
is a hop the router takes only for a request to local-free by name."""
|
|
router = Router(
|
|
model_list=[
|
|
_free_ollama_deployment("local-free"),
|
|
_paid_openai_deployment("gpt-priced", "gpt-4o"),
|
|
],
|
|
model_group_alias={"chain-entry": "local-free", "local-free": "gpt-priced"},
|
|
)
|
|
|
|
_assert_free(router.get_model_group_info(model_group="chain-entry"), "ollama")
|
|
_assert_priced(router.get_model_group_info(model_group="local-free"), "openai")
|
|
|
|
|
|
def test_get_model_group_info_prices_an_alias_chain_from_the_wildcard_route_serving_it():
|
|
"""When the routed group has no deployment of its own, the wildcard route matching it
|
|
serves the request, so the price is the wildcard's and never the routed group's own alias
|
|
target's."""
|
|
router = Router(
|
|
model_list=[
|
|
_paid_openai_deployment("openai/*", "openai/*"),
|
|
_free_ollama_deployment("local-free"),
|
|
],
|
|
model_group_alias={"wildcard-entry": "openai/gpt-4o", "openai/gpt-4o": "local-free"},
|
|
)
|
|
|
|
_assert_priced(router.get_model_group_info(model_group="wildcard-entry"), "openai")
|
|
_assert_free(router.get_model_group_info(model_group="openai/gpt-4o"), "ollama")
|
|
|
|
|
|
def _served_models(deployments: list[DeploymentTypedDict] | None) -> list[str]:
|
|
return [deployment["litellm_params"]["model"] for deployment in deployments or ()]
|
|
|
|
|
|
def test_get_model_list_of_routed_group_reads_the_groups_own_deployments_only():
|
|
"""The router resolves an alias once, so a group reached as an alias target is served by
|
|
its own deployments. get_model_list composes the group's own alias target too, the hop a
|
|
request to that group by name takes."""
|
|
router = Router(
|
|
model_list=[
|
|
_free_ollama_deployment("local-free"),
|
|
_paid_openai_deployment("gpt-priced", "gpt-4o"),
|
|
],
|
|
model_group_alias={"local-free": "gpt-priced"},
|
|
)
|
|
|
|
assert _served_models(router.get_model_list_of_routed_group("local-free")) == ["ollama/qwen3:0.6b"]
|
|
assert _served_models(router.get_model_list(model_name="local-free")) == ["ollama/qwen3:0.6b", "gpt-4o"]
|
|
|
|
|
|
def test_get_model_list_of_routed_group_falls_back_to_the_wildcard_route_serving_it():
|
|
router = Router(
|
|
model_list=[
|
|
_paid_openai_deployment("openai/*", "openai/*"),
|
|
_free_ollama_deployment("local-free"),
|
|
],
|
|
model_group_alias={"openai/gpt-4o": "local-free"},
|
|
)
|
|
|
|
routed = router.get_model_list_of_routed_group("openai/gpt-4o")
|
|
|
|
assert [deployment["model_name"] for deployment in routed] == ["openai/gpt-4o"]
|
|
assert _served_models(routed) == ["openai/gpt-4o"]
|
|
assert _served_models(router.get_model_list(model_name="openai/gpt-4o")) == ["ollama/qwen3:0.6b"]
|
|
|
|
|
|
def test_get_model_access_groups_caching():
|
|
"""
|
|
Test that get_model_access_groups caches the no-args result
|
|
and invalidates on deployment changes.
|
|
"""
|
|
from litellm.types.router import Deployment, LiteLLM_Params
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4",
|
|
"litellm_params": {"model": "gpt-4"},
|
|
"model_info": {"access_groups": ["premium"]},
|
|
},
|
|
]
|
|
)
|
|
|
|
# First call computes and populates cache
|
|
result1 = router.get_model_access_groups()
|
|
assert "premium" in result1
|
|
|
|
# All subsequent calls should return the same cached object (including first)
|
|
result2 = router.get_model_access_groups()
|
|
assert result1 is result2
|
|
|
|
# Calls with args should bypass cache
|
|
result_with_args = router.get_model_access_groups(model_name="gpt-4")
|
|
assert result_with_args is not result2
|
|
|
|
# Add a deployment — cache should be invalidated
|
|
router.add_deployment(
|
|
Deployment(
|
|
model_name="gpt-3.5",
|
|
litellm_params=LiteLLM_Params(model="gpt-3.5-turbo"),
|
|
model_info={"access_groups": ["default"]},
|
|
)
|
|
)
|
|
result3 = router.get_model_access_groups()
|
|
assert result3 is not result2
|
|
assert "premium" in result3
|
|
assert "default" in result3
|
|
|
|
# Delete the deployment — cache should be invalidated again
|
|
deployment_id = None
|
|
for m in router.model_list:
|
|
if m.get("model_name") == "gpt-3.5":
|
|
deployment_id = m.get("model_info", {}).get("id")
|
|
break
|
|
assert deployment_id is not None
|
|
router.delete_deployment(id=deployment_id)
|
|
result4 = router.get_model_access_groups()
|
|
assert result4 is not result3
|
|
assert "default" not in result4
|
|
assert "premium" in result4
|
|
|
|
|
|
def test_get_model_access_groups_cache_invalidation_set_model_list():
|
|
"""
|
|
Test that set_model_list invalidates the access groups cache.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4",
|
|
"litellm_params": {"model": "gpt-4"},
|
|
"model_info": {"access_groups": ["premium"]},
|
|
},
|
|
]
|
|
)
|
|
|
|
# Populate cache
|
|
result1 = router.get_model_access_groups()
|
|
assert "premium" in result1
|
|
|
|
# set_model_list should invalidate cache
|
|
router.set_model_list(
|
|
[
|
|
{
|
|
"model_name": "claude-3",
|
|
"litellm_params": {"model": "anthropic/claude-3-opus-20240229"},
|
|
"model_info": {"access_groups": ["research"]},
|
|
},
|
|
]
|
|
)
|
|
result2 = router.get_model_access_groups()
|
|
assert result2 is not result1
|
|
assert "research" in result2
|
|
assert "premium" not in result2
|
|
|
|
|
|
def test_get_model_access_groups_cache_invalidation_upsert_deployment():
|
|
"""
|
|
Test that upsert_deployment invalidates the access groups cache.
|
|
"""
|
|
from litellm.types.router import Deployment, LiteLLM_Params
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4",
|
|
"litellm_params": {"model": "gpt-4"},
|
|
"model_info": {"access_groups": ["premium"]},
|
|
},
|
|
]
|
|
)
|
|
|
|
# Populate cache
|
|
result1 = router.get_model_access_groups()
|
|
assert "premium" in result1
|
|
|
|
# Get the existing deployment's ID
|
|
existing_id = router.model_list[0]["model_info"]["id"]
|
|
|
|
# Upsert with the same ID but different params — triggers pop + re-add
|
|
router.upsert_deployment(
|
|
Deployment(
|
|
model_name="gpt-4-updated",
|
|
litellm_params=LiteLLM_Params(model="gpt-4-turbo"),
|
|
model_info={"id": existing_id, "access_groups": ["updated-group"]},
|
|
)
|
|
)
|
|
result2 = router.get_model_access_groups()
|
|
assert result2 is not result1
|
|
assert "updated-group" in result2
|
|
|
|
|
|
class _CutSSE(httpx.AsyncByteStream):
|
|
def __init__(self, content: str) -> None:
|
|
self._content: Final = content
|
|
|
|
async def __aiter__(self) -> AsyncIterator[bytes]:
|
|
yield f"data: {json.dumps({'id': 'cut', 'object': 'chat.completion.chunk', 'created': 1700000000, 'model': 'm', 'choices': [{'index': 0, 'delta': {'role': 'assistant', 'content': self._content}, 'finish_reason': None}]})}\n\n".encode()
|
|
raise httpx.RemoteProtocolError("peer closed connection without sending complete message body")
|
|
|
|
|
|
_BACKUP_SSE: Final = (
|
|
"".join(
|
|
f"data: {json.dumps(chunk)}\n\n"
|
|
for chunk in (
|
|
{
|
|
"id": "b",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1700000000,
|
|
"model": "mistral-large-latest",
|
|
"choices": [
|
|
{"index": 0, "delta": {"role": "assistant", "content": " self-attention."}, "finish_reason": None}
|
|
],
|
|
},
|
|
{
|
|
"id": "b",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1700000000,
|
|
"model": "mistral-large-latest",
|
|
"choices": [{"index": 0, "delta": {"content": ""}, "finish_reason": "stop"}],
|
|
"usage": {"prompt_tokens": 91, "completion_tokens": 5, "total_tokens": 96},
|
|
},
|
|
)
|
|
)
|
|
) + "data: [DONE]\n\n"
|
|
|
|
|
|
class _SpendRecorder(CustomLogger):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.successes: Final[list[dict[str, object]]] = []
|
|
self.failures: Final[list[dict[str, object]]] = []
|
|
self.spend_rows: Final[list[dict[str, object]]] = []
|
|
self.succeeded: Final = asyncio.Event()
|
|
|
|
async def async_log_success_event(self, kwargs, response_obj, start_time, end_time) -> None:
|
|
from litellm.proxy.spend_tracking.spend_tracking_utils import get_logging_payload
|
|
|
|
self.successes.append(kwargs["standard_logging_object"])
|
|
self.spend_rows.append(get_logging_payload(kwargs, response_obj, start_time, end_time))
|
|
self.succeeded.set()
|
|
|
|
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time) -> None:
|
|
self.failures.append(kwargs["standard_logging_object"])
|
|
|
|
|
|
class _SlowFailureRecorder(_SpendRecorder):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.failed_deployment_ids: Final[list[str]] = []
|
|
|
|
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time) -> None:
|
|
await asyncio.sleep(0.05)
|
|
self.failed_deployment_ids.append(kwargs["litellm_params"]["metadata"]["model_info"]["id"])
|
|
await super().async_log_failure_event(kwargs, response_obj, start_time, end_time)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("cut_groups", [("primary",), ("primary", "middle")], ids=["one_break", "two_breaks"])
|
|
async def test_continued_stream_success_log_bills_every_cut_leg(
|
|
monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter, cut_groups: tuple[str, ...]
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
recorder: Final = _SpendRecorder()
|
|
monkeypatch.setattr(litellm, "callbacks", [recorder])
|
|
respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
200, headers={"content-type": "text/event-stream"}, stream=_CutSSE("Transformers use")
|
|
)
|
|
)
|
|
respx_mock.post(host="api.deepseek.com").mock(
|
|
return_value=httpx.Response(200, headers={"content-type": "text/event-stream"}, stream=_CutSSE(" learned"))
|
|
)
|
|
respx_mock.post(host="api.mistral.ai").mock(
|
|
return_value=httpx.Response(200, headers={"content-type": "text/event-stream"}, text=_BACKUP_SSE)
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o",
|
|
"api_key": "k1",
|
|
"input_cost_per_token": 0.001,
|
|
"output_cost_per_token": 0.002,
|
|
},
|
|
},
|
|
{
|
|
"model_name": "middle",
|
|
"litellm_params": {
|
|
"model": "deepseek/deepseek-chat",
|
|
"api_key": "k2",
|
|
"input_cost_per_token": 0.003,
|
|
"output_cost_per_token": 0.004,
|
|
},
|
|
},
|
|
{"model_name": "backup", "litellm_params": {"model": "mistral/mistral-large-latest", "api_key": "k3"}},
|
|
],
|
|
fallbacks=[{"primary": ["middle" if "middle" in cut_groups else "backup"]}, {"middle": ["backup"]}],
|
|
enable_mid_stream_fallback_continuation=True,
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "How do transformers work?"}],
|
|
stream=True,
|
|
stream_options={"include_usage": True},
|
|
)
|
|
chunks: Final = [chunk async for chunk in response]
|
|
await asyncio.wait_for(recorder.succeeded.wait(), timeout=10)
|
|
await asyncio.sleep(0.5)
|
|
|
|
backup_prompt_cost, backup_completion_cost = litellm.cost_per_token(
|
|
model="mistral/mistral-large-latest", prompt_tokens=91, completion_tokens=5
|
|
)
|
|
success: Final = recorder.successes[-1]
|
|
assert len(recorder.failures) == len(cut_groups)
|
|
assert [failure["response_cost"] for failure in recorder.failures] == pytest.approx(
|
|
[
|
|
failure["prompt_tokens"] * input_price + failure["completion_tokens"] * output_price
|
|
for failure, (input_price, output_price) in zip(recorder.failures, ((0.001, 0.002), (0.003, 0.004)))
|
|
]
|
|
)
|
|
assert success["response_cost"] == pytest.approx(
|
|
sum(failure["response_cost"] for failure in recorder.failures) + backup_prompt_cost + backup_completion_cost
|
|
)
|
|
assert success["prompt_tokens"] == sum(failure["prompt_tokens"] for failure in recorder.failures) + 91
|
|
assert success["completion_tokens"] == sum(failure["completion_tokens"] for failure in recorder.failures) + 5
|
|
assert success["cost_breakdown"]["total_cost"] == pytest.approx(success["response_cost"])
|
|
spend_row: Final = recorder.spend_rows[-1]
|
|
assert (spend_row["spend"], spend_row["prompt_tokens"], spend_row["completion_tokens"]) == (
|
|
success["response_cost"],
|
|
success["prompt_tokens"],
|
|
success["completion_tokens"],
|
|
)
|
|
client_usage: Final = next(chunk.usage for chunk in reversed(chunks) if getattr(chunk, "usage", None))
|
|
assert (client_usage.prompt_tokens, client_usage.completion_tokens) == (
|
|
success["prompt_tokens"],
|
|
success["completion_tokens"],
|
|
)
|
|
assert {chunk.id for chunk in chunks} == {spend_row["request_id"]}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_continued_stream_sharing_the_proxy_logging_object_bills_the_cut_to_its_own_deployment(
|
|
monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
recorder: Final = _SlowFailureRecorder()
|
|
monkeypatch.setattr(litellm, "callbacks", [recorder])
|
|
respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
200, headers={"content-type": "text/event-stream"}, stream=_CutSSE("Transformers use")
|
|
)
|
|
)
|
|
respx_mock.post(host="api.mistral.ai").mock(
|
|
return_value=httpx.Response(200, headers={"content-type": "text/event-stream"}, text=_BACKUP_SSE)
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o",
|
|
"api_key": "k1",
|
|
"input_cost_per_token": 0.001,
|
|
"output_cost_per_token": 0.002,
|
|
},
|
|
"model_info": {"id": "primary-deployment"},
|
|
},
|
|
{"model_name": "backup", "litellm_params": {"model": "mistral/mistral-large-latest", "api_key": "k3"}},
|
|
],
|
|
fallbacks=[{"primary": ["backup"]}],
|
|
enable_mid_stream_fallback_continuation=True,
|
|
)
|
|
messages: Final = [{"role": "user", "content": "How do transformers work?"}]
|
|
shared_logging, _ = litellm.utils.function_setup(
|
|
original_function="acompletion",
|
|
rules_obj=litellm.utils.Rules(),
|
|
start_time=datetime.now(),
|
|
model="primary",
|
|
messages=messages,
|
|
stream=True,
|
|
litellm_call_id="shared-call",
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="primary",
|
|
messages=messages,
|
|
stream=True,
|
|
stream_options={"include_usage": True},
|
|
litellm_call_id="shared-call",
|
|
litellm_logging_obj=shared_logging,
|
|
)
|
|
_ = [chunk async for chunk in response]
|
|
await asyncio.wait_for(recorder.succeeded.wait(), timeout=10)
|
|
await asyncio.sleep(0.5)
|
|
|
|
backup_prompt_cost, backup_completion_cost = litellm.cost_per_token(
|
|
model="mistral/mistral-large-latest", prompt_tokens=91, completion_tokens=5
|
|
)
|
|
(failure,) = recorder.failures
|
|
assert (failure["model_group"], failure["model_id"]) == ("primary", "primary-deployment")
|
|
assert recorder.failed_deployment_ids == ["primary-deployment"]
|
|
assert failure["response_cost"] == pytest.approx(
|
|
failure["prompt_tokens"] * 0.001 + failure["completion_tokens"] * 0.002
|
|
)
|
|
assert recorder.successes[-1]["response_cost"] == pytest.approx(
|
|
failure["response_cost"] + backup_prompt_cost + backup_completion_cost
|
|
)
|
|
|
|
|
|
_AGENTIC_FIRST_CALL_USAGE: Final = {"prompt_tokens": 50, "completion_tokens": 7, "total_tokens": 57}
|
|
_AGENTIC_FIRST_CALL_BODY: Final = {
|
|
"id": "chatcmpl-first",
|
|
"object": "chat.completion",
|
|
"created": 1700000000,
|
|
"model": "gpt-4o-mini",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"finish_reason": "tool_calls",
|
|
"message": {
|
|
"role": "assistant",
|
|
"content": None,
|
|
"tool_calls": [
|
|
{
|
|
"id": "call_abc",
|
|
"type": "function",
|
|
"function": {"name": "litellm_code_execution", "arguments": "{}"},
|
|
}
|
|
],
|
|
},
|
|
}
|
|
],
|
|
"usage": _AGENTIC_FIRST_CALL_USAGE,
|
|
}
|
|
|
|
|
|
_CRASH_THE_PLAN: Final = "crash the plan"
|
|
|
|
|
|
class _ScriptedAgenticFollowUps(CustomLogger):
|
|
def __init__(self, follow_ups: tuple[str, ...]) -> None:
|
|
super().__init__()
|
|
self._follow_ups: Final = follow_ups
|
|
self.plans_built = 0
|
|
self.shared_call_succeeded: Final = asyncio.Event()
|
|
|
|
async def async_should_run_agentic_loop(
|
|
self,
|
|
response: object,
|
|
model: str,
|
|
messages: Sequence[Mapping[str, object]],
|
|
tools: Sequence[Mapping[str, object]] | None,
|
|
stream: bool,
|
|
custom_llm_provider: str,
|
|
kwargs: Mapping[str, object],
|
|
) -> tuple[bool, dict[str, object]]:
|
|
assert isinstance(response, litellm.ModelResponse)
|
|
return bool(response.choices[0].message.tool_calls), {"tool_calls": [{"id": "call_abc"}]}
|
|
|
|
async def async_build_agentic_loop_plan(
|
|
self,
|
|
tools: Mapping[str, object],
|
|
model: str,
|
|
messages: Sequence[Mapping[str, object]],
|
|
response: object,
|
|
anthropic_messages_provider_config: object,
|
|
anthropic_messages_optional_request_params: Mapping[str, object],
|
|
logging_obj: object,
|
|
stream: bool,
|
|
kwargs: Mapping[str, object],
|
|
) -> AgenticLoopPlan:
|
|
follow_up: Final = self._follow_ups[min(self.plans_built, len(self._follow_ups) - 1)]
|
|
self.plans_built += 1
|
|
if follow_up == _CRASH_THE_PLAN:
|
|
raise ValueError("the agentic plan build crashed")
|
|
return AgenticLoopPlan(
|
|
run_agentic_loop=True,
|
|
request_patch=AgenticLoopRequestPatch(
|
|
messages=[{"role": "user", "content": "what is 6*7?"}, {"role": "assistant", "content": "42"}],
|
|
kwargs={"mock_response": follow_up},
|
|
),
|
|
)
|
|
|
|
async def async_log_success_event(
|
|
self, kwargs: Mapping[str, object], response_obj: object, start_time: datetime, end_time: datetime
|
|
) -> None:
|
|
if kwargs.get("litellm_call_id") == "shared-agentic-call":
|
|
self.shared_call_succeeded.set()
|
|
|
|
|
|
_AGENTIC_BACKUP_USAGE: Final = {"prompt_tokens": 20, "completion_tokens": 3, "total_tokens": 23}
|
|
_AGENTIC_BACKUP_BODY: Final = {
|
|
"id": "chatcmpl-backup",
|
|
"object": "chat.completion",
|
|
"created": 1700000000,
|
|
"model": "gpt-4o-mini",
|
|
"choices": [{"index": 0, "finish_reason": "stop", "message": {"role": "assistant", "content": "42"}}],
|
|
"usage": _AGENTIC_BACKUP_USAGE,
|
|
}
|
|
_AGENTIC_RATE_LIMITED: Final = {
|
|
"error": {"message": "Rate limit reached", "type": "requests", "code": "rate_limit_exceeded"}
|
|
}
|
|
|
|
|
|
async def _route_agentic_request(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
respx_mock: respx.MockRouter,
|
|
follow_ups: tuple[str, ...],
|
|
model: str = "m",
|
|
first_call_replies: tuple[httpx.Response, ...] = (),
|
|
fallbacks: tuple[str, ...] = (),
|
|
) -> tuple[object, LiteLLMLogging, respx.Route, _ScriptedAgenticFollowUps]:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
hook: Final = _ScriptedAgenticFollowUps(follow_ups)
|
|
monkeypatch.setattr(litellm, "callbacks", [hook])
|
|
first_calls: Final = (
|
|
respx_mock.post("https://api.openai.com/v1/chat/completions").mock(side_effect=list(first_call_replies))
|
|
if first_call_replies
|
|
else respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=httpx.Response(200, json=_AGENTIC_FIRST_CALL_BODY)
|
|
)
|
|
)
|
|
respx_mock.post("https://backup.test/v1/chat/completions").mock(
|
|
return_value=httpx.Response(200, json=_AGENTIC_BACKUP_BODY)
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{"model_name": "m", "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test"}},
|
|
{
|
|
"model_name": "backup",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "sk-test",
|
|
"api_base": "https://backup.test/v1",
|
|
},
|
|
},
|
|
],
|
|
num_retries=2,
|
|
retry_after=0,
|
|
fallbacks=[{"m": list(fallbacks)}] if fallbacks else [],
|
|
)
|
|
messages: Final = [{"role": "user", "content": "what is 6*7?"}]
|
|
shared_logging, _ = litellm.utils.function_setup(
|
|
original_function="acompletion",
|
|
rules_obj=litellm.utils.Rules(),
|
|
start_time=datetime.now(),
|
|
model=model,
|
|
messages=messages,
|
|
litellm_call_id="shared-agentic-call",
|
|
)
|
|
try:
|
|
outcome: object = await router.acompletion(
|
|
model=model, messages=messages, litellm_call_id="shared-agentic-call", litellm_logging_obj=shared_logging
|
|
)
|
|
except (litellm.RateLimitError, litellm.APIConnectionError) as error:
|
|
outcome = error
|
|
return outcome, shared_logging, first_calls, hook
|
|
|
|
|
|
def _gpt_4o_mini_cost(usage: Mapping[str, int]) -> float:
|
|
return sum(
|
|
litellm.cost_per_token(
|
|
model="gpt-4o-mini", prompt_tokens=usage["prompt_tokens"], completion_tokens=usage["completion_tokens"]
|
|
)
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_agentic_request_whose_follow_up_fails_is_not_retried_and_bills_its_one_first_call(
|
|
monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter
|
|
) -> None:
|
|
from litellm.proxy.utils import _failure_fields_to_lift
|
|
|
|
outcome, shared_logging, first_calls, _ = await _route_agentic_request(
|
|
monkeypatch, respx_mock, ("litellm.RateLimitError",)
|
|
)
|
|
|
|
lifted: Final = _failure_fields_to_lift({"litellm_logging_obj": shared_logging})
|
|
assert isinstance(outcome, litellm.RateLimitError)
|
|
assert outcome.status_code == 429
|
|
assert first_calls.call_count == 1
|
|
assert lifted["combined_usage_object"].prompt_tokens == _AGENTIC_FIRST_CALL_USAGE["prompt_tokens"]
|
|
assert lifted["combined_usage_object"].completion_tokens == _AGENTIC_FIRST_CALL_USAGE["completion_tokens"]
|
|
assert lifted["response_cost"] == pytest.approx(_gpt_4o_mini_cost(_AGENTIC_FIRST_CALL_USAGE))
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_agentic_request_whose_plan_build_crashes_after_the_first_call_is_not_retried(
|
|
monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter
|
|
) -> None:
|
|
outcome, _, first_calls, hook = await _route_agentic_request(monkeypatch, respx_mock, (_CRASH_THE_PLAN,))
|
|
|
|
assert isinstance(outcome, litellm.APIConnectionError)
|
|
assert first_calls.call_count == 1
|
|
assert hook.plans_built == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_agentic_request_whose_first_call_fails_is_still_retried(
|
|
monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter
|
|
) -> None:
|
|
outcome, _, first_calls, hook = await _route_agentic_request(
|
|
monkeypatch,
|
|
respx_mock,
|
|
("42",),
|
|
first_call_replies=(
|
|
httpx.Response(429, json=_AGENTIC_RATE_LIMITED),
|
|
httpx.Response(200, json=_AGENTIC_FIRST_CALL_BODY),
|
|
),
|
|
)
|
|
|
|
assert isinstance(outcome, litellm.ModelResponse)
|
|
assert first_calls.call_count == 2
|
|
assert hook.plans_built == 1
|
|
|
|
|
|
async def _successful_agentic_request_billing(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
respx_mock: respx.MockRouter,
|
|
follow_ups: tuple[str, ...],
|
|
model: str,
|
|
fallbacks: tuple[str, ...] = (),
|
|
) -> tuple[Mapping[str, object], Mapping[str, str | None]]:
|
|
from litellm.proxy.common_request_processing import cost_headers
|
|
|
|
outcome, shared_logging, _, hook = await _route_agentic_request(
|
|
monkeypatch, respx_mock, follow_ups, model=model, fallbacks=fallbacks
|
|
)
|
|
await asyncio.wait_for(hook.shared_call_succeeded.wait(), timeout=10)
|
|
assert isinstance(outcome, litellm.ModelResponse)
|
|
success: Final = shared_logging.model_call_details["standard_logging_object"]
|
|
return success, cost_headers(success["response_cost"], shared_logging)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_after_a_failed_agentic_follow_up_bills_the_primarys_first_call_on_the_success_row(
|
|
monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter
|
|
) -> None:
|
|
first_try, first_try_headers = await _successful_agentic_request_billing(
|
|
monkeypatch, respx_mock, ("42",), model="backup"
|
|
)
|
|
recovered, recovered_headers = await _successful_agentic_request_billing(
|
|
monkeypatch, respx_mock, ("litellm.RateLimitError",), model="m", fallbacks=("backup",)
|
|
)
|
|
|
|
first_call_cost: Final = _gpt_4o_mini_cost(_AGENTIC_FIRST_CALL_USAGE)
|
|
assert first_try["response_cost"] == pytest.approx(_gpt_4o_mini_cost(_AGENTIC_BACKUP_USAGE))
|
|
assert "failed_attempts_cost" not in first_try["cost_breakdown"].get("additional_costs", {})
|
|
assert recovered["response_cost"] == pytest.approx(first_try["response_cost"] + first_call_cost)
|
|
assert recovered["prompt_tokens"] == first_try["prompt_tokens"] + _AGENTIC_FIRST_CALL_USAGE["prompt_tokens"]
|
|
assert (
|
|
recovered["completion_tokens"]
|
|
== first_try["completion_tokens"] + _AGENTIC_FIRST_CALL_USAGE["completion_tokens"]
|
|
)
|
|
assert recovered["cost_breakdown"]["total_cost"] == pytest.approx(recovered["response_cost"])
|
|
assert recovered["cost_breakdown"]["additional_costs"]["failed_attempts_cost"] == pytest.approx(first_call_cost)
|
|
assert float(recovered_headers["x-litellm-response-cost"]) == pytest.approx(recovered["response_cost"])
|
|
assert {key: value for key, value in recovered_headers.items() if key != "x-litellm-response-cost"} == {
|
|
key: value for key, value in first_try_headers.items() if key != "x-litellm-response-cost"
|
|
}
|
|
|
|
|
|
def _responses_sse(*events: Mapping[str, object]) -> str:
|
|
return "".join(f"event: {event['type']}\ndata: {json.dumps(event)}\n\n" for event in events)
|
|
|
|
|
|
def _responses_envelope(response_id: str, model: str, status: str, **fields: object) -> dict[str, object]:
|
|
return {
|
|
"id": response_id,
|
|
"object": "response",
|
|
"created_at": 1700000000,
|
|
"status": status,
|
|
"model": model,
|
|
"output": [],
|
|
**fields,
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_responses_fallback_sharing_the_proxy_logging_object_logs_the_fallback_usage(
|
|
monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
recorder: Final = _SpendRecorder()
|
|
monkeypatch.setattr(litellm, "callbacks", [recorder])
|
|
respx_mock.post("https://primary.example/v1/responses").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
headers={"content-type": "text/event-stream"},
|
|
text=_responses_sse(
|
|
{"type": "response.created", "response": _responses_envelope("resp_primary", "gpt-4o", "in_progress")},
|
|
*(
|
|
{
|
|
"type": "response.output_text.delta",
|
|
"item_id": "msg_primary",
|
|
"output_index": 0,
|
|
"content_index": 0,
|
|
"delta": word,
|
|
}
|
|
for word in ("Once ", "upon ", "a ", "time ", "there")
|
|
),
|
|
{
|
|
"type": "response.failed",
|
|
"response": _responses_envelope(
|
|
"resp_primary",
|
|
"gpt-4o",
|
|
"failed",
|
|
error={"code": "server_error", "message": "The model had an internal error"},
|
|
),
|
|
},
|
|
),
|
|
)
|
|
)
|
|
respx_mock.post("https://backup.example/v1/responses").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
headers={"content-type": "text/event-stream"},
|
|
text=_responses_sse(
|
|
{
|
|
"type": "response.created",
|
|
"response": _responses_envelope("resp_backup", "gpt-4o-mini", "in_progress"),
|
|
},
|
|
{
|
|
"type": "response.completed",
|
|
"response": _responses_envelope(
|
|
"resp_backup",
|
|
"gpt-4o-mini",
|
|
"completed",
|
|
output=[
|
|
{
|
|
"type": "message",
|
|
"id": "msg_backup",
|
|
"status": "completed",
|
|
"role": "assistant",
|
|
"content": [{"type": "output_text", "text": "Once upon a time", "annotations": []}],
|
|
}
|
|
],
|
|
usage={"input_tokens": 1000, "output_tokens": 700, "total_tokens": 1700},
|
|
),
|
|
},
|
|
),
|
|
)
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "k1", "api_base": "https://primary.example/v1"},
|
|
},
|
|
{
|
|
"model_name": "backup",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "k2",
|
|
"api_base": "https://backup.example/v1",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"primary": ["backup"]}],
|
|
num_retries=0,
|
|
)
|
|
shared_logging, _ = litellm.utils.function_setup(
|
|
original_function="aresponses",
|
|
rules_obj=litellm.utils.Rules(),
|
|
start_time=datetime.now(),
|
|
model="primary",
|
|
input="Tell me a story",
|
|
stream=True,
|
|
litellm_call_id="shared-call",
|
|
)
|
|
|
|
response: Final = await router.aresponses(
|
|
model="primary",
|
|
input="Tell me a story",
|
|
stream=True,
|
|
litellm_call_id="shared-call",
|
|
litellm_logging_obj=shared_logging,
|
|
)
|
|
_ = [event async for event in response]
|
|
await asyncio.wait_for(recorder.succeeded.wait(), timeout=10)
|
|
|
|
success: Final = recorder.successes[-1]
|
|
assert (success["prompt_tokens"], success["completion_tokens"]) == (1000, 700)
|
|
spend_row: Final = recorder.spend_rows[-1]
|
|
assert (spend_row["prompt_tokens"], spend_row["completion_tokens"], spend_row["spend"]) == (
|
|
1000,
|
|
700,
|
|
success["response_cost"],
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"routing_strategy",
|
|
[
|
|
"simple-shuffle",
|
|
"usage-based-routing",
|
|
"usage-based-routing-v2",
|
|
"least-busy",
|
|
"latency-based-routing",
|
|
"cost-based-routing",
|
|
],
|
|
)
|
|
async def test_continuation_routes_only_to_deployments_that_can_continue_under_every_routing_strategy(
|
|
routing_strategy: str,
|
|
) -> None:
|
|
from litellm.router_utils.pre_call_checks.continuation_prefill_check import (
|
|
MID_STREAM_CONTINUATION_KWARG,
|
|
MID_STREAM_CONTINUATION_MARKER,
|
|
)
|
|
from litellm.types.router import RouterRateLimitError
|
|
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "backup",
|
|
"litellm_params": {"model": "gpt-4o", "api_key": "k1"},
|
|
"model_info": {"id": "no-prefill-deployment", "supports_assistant_prefill": False},
|
|
},
|
|
],
|
|
routing_strategy=routing_strategy,
|
|
enable_mid_stream_fallback_continuation=True,
|
|
)
|
|
messages: Final = [{"role": "user", "content": "Hi"}]
|
|
plain_pick: Final = await router.async_get_available_deployment(
|
|
model="backup", messages=messages, request_kwargs={}
|
|
)
|
|
assert plain_pick["model_info"]["id"] == "no-prefill-deployment"
|
|
with pytest.raises(RouterRateLimitError):
|
|
await router.async_get_available_deployment(
|
|
model="backup",
|
|
messages=messages,
|
|
request_kwargs={MID_STREAM_CONTINUATION_KWARG: MID_STREAM_CONTINUATION_MARKER},
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("response_kind", ["object", "dict", "async-generator"])
|
|
async def test_set_response_headers_exposes_complexity_decision_on_every_response_shape(
|
|
response_kind: Literal["object", "dict", "async-generator"],
|
|
) -> None:
|
|
class HeaderResponse:
|
|
def __init__(self) -> None:
|
|
self._hidden_params: dict[str, object] = {}
|
|
|
|
response: object
|
|
if response_kind == "object":
|
|
response = HeaderResponse()
|
|
elif response_kind == "dict":
|
|
response = {}
|
|
else:
|
|
response = _AsyncList()
|
|
|
|
router = Router(model_list=[])
|
|
result = await router.set_response_headers(
|
|
response=response,
|
|
request_kwargs={
|
|
"metadata": {
|
|
"routing_decision": {
|
|
"router_type": "complexity",
|
|
"tier": "SIMPLE",
|
|
"cause": "heuristic_scorer",
|
|
"score": 0.25,
|
|
"tier_litellm_params": {"reasoning_effort": "low"},
|
|
}
|
|
}
|
|
},
|
|
)
|
|
hidden_params = result["_hidden_params"] if isinstance(result, dict) else result._hidden_params
|
|
additional_headers = hidden_params["additional_headers"]
|
|
|
|
assert additional_headers == {
|
|
"x-litellm-model-group": None,
|
|
"x-litellm-complexity-router-tier": "SIMPLE",
|
|
"x-litellm-complexity-router-cause": "heuristic_scorer",
|
|
"x-litellm-complexity-router-score": "0.25",
|
|
"x-litellm-complexity-router-reasoning-effort": "low",
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_set_response_headers_is_the_only_complexity_header_source_for_proxy_headers() -> None:
|
|
from litellm.proxy._types import UserAPIKeyAuth
|
|
from litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing
|
|
|
|
router = Router(model_list=[])
|
|
response = await router.set_response_headers(response={}, request_kwargs={})
|
|
additional_headers = response["_hidden_params"]["additional_headers"]
|
|
proxy_headers = ProxyBaseLLMRequestProcessing.get_custom_headers(
|
|
user_api_key_dict=UserAPIKeyAuth(),
|
|
request_data={
|
|
"metadata": {
|
|
"routing_decision": {
|
|
"router_type": "complexity",
|
|
"tier": "REASONING",
|
|
"cause": "heuristic_scorer",
|
|
"tier_litellm_params": {"reasoning_effort": "xhigh"},
|
|
}
|
|
}
|
|
},
|
|
**additional_headers,
|
|
)
|
|
|
|
assert not {key for key in proxy_headers if key.startswith("x-litellm-complexity-router-")}
|
|
|
|
|
|
def test_completion_streaming_fallback_resumes_chain_without_retrying_primary():
|
|
class FailingStream(CustomStreamWrapper):
|
|
def __init__(self, model: str):
|
|
super().__init__(
|
|
completion_stream=object(), model=model, custom_llm_provider="openai", logging_obj=MagicMock()
|
|
)
|
|
|
|
def __iter__(self):
|
|
return self
|
|
|
|
def __next__(self):
|
|
raise MidStreamFallbackError(
|
|
message=f"provider 500 from {self.model}",
|
|
model=self.model,
|
|
llm_provider="openai",
|
|
generated_content="",
|
|
is_pre_first_chunk=True,
|
|
original_exception=litellm.InternalServerError(
|
|
message=f"provider 500 from {self.model}", model=self.model, llm_provider="openai"
|
|
),
|
|
)
|
|
|
|
class OkStream(FailingStream):
|
|
def __init__(self, model: str):
|
|
super().__init__(model)
|
|
self._chunks = iter(
|
|
[litellm.ModelResponseStream(choices=[{"index": 0, "delta": {"content": f"ok-from-{model}"}}])]
|
|
)
|
|
|
|
def __next__(self):
|
|
return next(self._chunks)
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "primary", "litellm_params": {"model": "openai/primary-model", "api_key": "fake-key"}},
|
|
{"model_name": "backup", "litellm_params": {"model": "openai/backup-model", "api_key": "fake-key"}},
|
|
],
|
|
fallbacks=[{"primary": ["backup"]}],
|
|
num_retries=0,
|
|
)
|
|
primary_calls: Final = iter(range(2))
|
|
|
|
def fake_completion(**kwargs):
|
|
model_group: Final = kwargs["metadata"]["model_group"]
|
|
if model_group == "backup":
|
|
return OkStream(kwargs["model"])
|
|
if next(primary_calls) > 0:
|
|
raise RuntimeError("primary group retried")
|
|
return FailingStream(kwargs["model"])
|
|
|
|
with patch("litellm.completion", side_effect=fake_completion) as provider_calls:
|
|
response: Final = router.completion(model="primary", messages=[{"role": "user", "content": "hi"}], stream=True)
|
|
content: Final = "".join(chunk.choices[0].delta.content or "" for chunk in response)
|
|
|
|
assert content == "ok-from-openai/backup-model"
|
|
assert [call.kwargs["metadata"]["model_group"] for call in provider_calls.call_args_list] == [
|
|
"primary",
|
|
"backup",
|
|
]
|
|
|
|
|
|
def test_completion_mid_stream_fallback_walks_every_entry_of_the_configured_list():
|
|
class FailingStream(CustomStreamWrapper):
|
|
def __init__(self, model: str):
|
|
super().__init__(
|
|
completion_stream=object(), model=model, custom_llm_provider="openai", logging_obj=MagicMock()
|
|
)
|
|
|
|
def __iter__(self):
|
|
return self
|
|
|
|
def __next__(self):
|
|
raise MidStreamFallbackError(
|
|
message=f"provider 500 from {self.model}",
|
|
model=self.model,
|
|
llm_provider="openai",
|
|
generated_content="",
|
|
is_pre_first_chunk=True,
|
|
original_exception=litellm.InternalServerError(
|
|
message=f"provider 500 from {self.model}", model=self.model, llm_provider="openai"
|
|
),
|
|
)
|
|
|
|
class OkStream(FailingStream):
|
|
def __init__(self, model: str):
|
|
super().__init__(model)
|
|
self._chunks = iter(
|
|
[litellm.ModelResponseStream(choices=[{"index": 0, "delta": {"content": f"ok-from-{model}"}}])]
|
|
)
|
|
|
|
def __next__(self):
|
|
return next(self._chunks)
|
|
|
|
def fake_completion(**kwargs):
|
|
if "fb2" in kwargs["model"]:
|
|
return OkStream(kwargs["model"])
|
|
return FailingStream(kwargs["model"])
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "primary", "litellm_params": {"model": "openai/primary-model", "api_key": "fake-key"}},
|
|
{"model_name": "fb1", "litellm_params": {"model": "openai/fb1-model", "api_key": "fake-key"}},
|
|
{"model_name": "fb2", "litellm_params": {"model": "openai/fb2-model", "api_key": "fake-key"}},
|
|
],
|
|
fallbacks=[{"primary": ["fb1", "fb2"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
with patch("litellm.completion", side_effect=fake_completion) as provider_calls:
|
|
response: Final = router.completion(model="primary", messages=[{"role": "user", "content": "hi"}], stream=True)
|
|
content: Final = "".join(chunk.choices[0].delta.content or "" for chunk in response if chunk is not None)
|
|
|
|
assert content == "ok-from-openai/fb2-model"
|
|
assert [call.kwargs["metadata"]["model_group"] for call in provider_calls.call_args_list] == [
|
|
"primary",
|
|
"fb1",
|
|
"fb2",
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_mid_stream_fallback_walks_every_entry_of_the_configured_list():
|
|
"""LIT-7400: fallbacks=[{primary: [fb1, fb2]}] must reach fb2 when fb1 dies before its first chunk.
|
|
|
|
run_async_fallback returns as soon as fb1's stream wrapper exists, so fb1's failure surfaces
|
|
inside the streaming iterator, where the lookup is keyed by fb1. That key has no chain of its
|
|
own, so the iterator has to resume the chain of the group the request was originally for.
|
|
"""
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from litellm.exceptions import MidStreamFallbackError
|
|
from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper
|
|
|
|
class FailingStream(CustomStreamWrapper):
|
|
def __init__(self, model: str):
|
|
super().__init__(
|
|
completion_stream=object(), model=model, custom_llm_provider="openai", logging_obj=MagicMock()
|
|
)
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
raise MidStreamFallbackError(
|
|
message=f"provider 500 from {self.model}",
|
|
model=self.model,
|
|
llm_provider="openai",
|
|
generated_content="",
|
|
is_pre_first_chunk=True,
|
|
original_exception=litellm.InternalServerError(
|
|
message=f"provider 500 from {self.model}", model=self.model, llm_provider="openai"
|
|
),
|
|
)
|
|
|
|
class OkStream(FailingStream):
|
|
def __init__(self, model: str):
|
|
super().__init__(model)
|
|
self._chunks = iter(
|
|
[litellm.ModelResponseStream(choices=[{"index": 0, "delta": {"content": f"ok-from-{model}"}}])]
|
|
)
|
|
|
|
async def __anext__(self):
|
|
try:
|
|
return next(self._chunks)
|
|
except StopIteration:
|
|
raise StopAsyncIteration from None
|
|
|
|
async def fake_acompletion(**kwargs):
|
|
if "fb2" in kwargs["model"]:
|
|
return OkStream(kwargs["model"])
|
|
return FailingStream(kwargs["model"])
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "primary", "litellm_params": {"model": "openai/primary-model", "api_key": "fake-key"}},
|
|
{"model_name": "fb1", "litellm_params": {"model": "openai/fb1-model", "api_key": "fake-key"}},
|
|
{"model_name": "fb2", "litellm_params": {"model": "openai/fb2-model", "api_key": "fake-key"}},
|
|
],
|
|
fallbacks=[{"primary": ["fb1", "fb2"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
with patch("litellm.acompletion", side_effect=fake_acompletion) as mock_acompletion:
|
|
response = await router.acompletion(model="primary", messages=[{"role": "user", "content": "hi"}], stream=True)
|
|
content: Final = "".join(
|
|
[chunk.choices[0].delta.content or "" async for chunk in response if chunk is not None]
|
|
)
|
|
|
|
assert content == "ok-from-openai/fb2-model"
|
|
assert [c.kwargs["metadata"]["model_group"] for c in mock_acompletion.call_args_list] == [
|
|
"primary",
|
|
"fb1",
|
|
"fb2",
|
|
]
|
|
|
|
|
|
class _DiesBeforeFirstChunk(CustomStreamWrapper):
|
|
def __init__(self, model: str):
|
|
super().__init__(completion_stream=object(), model=model, custom_llm_provider="openai", logging_obj=MagicMock())
|
|
|
|
def _mid_stream_error(self) -> MidStreamFallbackError:
|
|
return MidStreamFallbackError(
|
|
message=f"provider 500 from {self.model}",
|
|
model=self.model,
|
|
llm_provider="openai",
|
|
generated_content="",
|
|
is_pre_first_chunk=True,
|
|
original_exception=litellm.InternalServerError(
|
|
message=f"provider 500 from {self.model}", model=self.model, llm_provider="openai"
|
|
),
|
|
)
|
|
|
|
def __iter__(self):
|
|
return self
|
|
|
|
def __next__(self):
|
|
raise self._mid_stream_error()
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
raise self._mid_stream_error()
|
|
|
|
|
|
class _Answers(_DiesBeforeFirstChunk):
|
|
def __init__(self, model: str):
|
|
super().__init__(model)
|
|
self._chunks = iter(
|
|
[litellm.ModelResponseStream(choices=[{"index": 0, "delta": {"content": f"ok-from-{model}"}}])]
|
|
)
|
|
|
|
def __next__(self):
|
|
return next(self._chunks)
|
|
|
|
async def __anext__(self):
|
|
try:
|
|
return next(self._chunks)
|
|
except StopIteration:
|
|
raise StopAsyncIteration from None
|
|
|
|
|
|
def _primary_and_backup_router(**settings: object) -> litellm.Router:
|
|
return litellm.Router(
|
|
model_list=[
|
|
{"model_name": "primary", "litellm_params": {"model": "openai/primary-model", "api_key": "fake-key"}},
|
|
{"model_name": "backup", "litellm_params": {"model": "openai/backup-model", "api_key": "fake-key"}},
|
|
],
|
|
num_retries=0,
|
|
**settings,
|
|
)
|
|
|
|
|
|
def _stream_for(**kwargs: object) -> CustomStreamWrapper:
|
|
model: Final = str(kwargs["model"])
|
|
return _Answers(model) if "backup" in model else _DiesBeforeFirstChunk(model)
|
|
|
|
|
|
def _groups_called(provider_calls: MagicMock) -> list[str]:
|
|
return [call.kwargs["metadata"]["model_group"] for call in provider_calls.call_args_list]
|
|
|
|
|
|
def _router_internals_reached_the_provider(provider_calls: MagicMock) -> bool:
|
|
leaked: Final = frozenset(
|
|
("fallbacks", "context_window_fallbacks", "content_policy_fallbacks", MID_STREAM_FALLBACK_CONTROLS_KEY)
|
|
)
|
|
return any(leaked & call.kwargs.keys() for call in provider_calls.call_args_list)
|
|
|
|
|
|
def test_completion_mid_stream_fallback_honors_the_per_request_list():
|
|
router: Final = _primary_and_backup_router()
|
|
|
|
with patch("litellm.completion", side_effect=_stream_for) as provider_calls:
|
|
response: Final = router.completion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
stream=True,
|
|
fallbacks=[{"primary": ["backup"]}],
|
|
)
|
|
content: Final = "".join(chunk.choices[0].delta.content or "" for chunk in response if chunk is not None)
|
|
|
|
assert content == "ok-from-openai/backup-model"
|
|
assert _groups_called(provider_calls) == ["primary", "backup"]
|
|
assert not _router_internals_reached_the_provider(provider_calls)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_mid_stream_fallback_honors_the_per_request_list():
|
|
router: Final = _primary_and_backup_router()
|
|
|
|
async def fake_acompletion(**kwargs):
|
|
return _stream_for(**kwargs)
|
|
|
|
with patch("litellm.acompletion", side_effect=fake_acompletion) as provider_calls:
|
|
response: Final = await router.acompletion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
stream=True,
|
|
fallbacks=[{"primary": ["backup"]}],
|
|
)
|
|
content: Final = "".join(
|
|
[chunk.choices[0].delta.content or "" async for chunk in response if chunk is not None]
|
|
)
|
|
|
|
assert content == "ok-from-openai/backup-model"
|
|
assert _groups_called(provider_calls) == ["primary", "backup"]
|
|
assert not _router_internals_reached_the_provider(provider_calls)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_mid_stream_fallback_honors_a_per_request_fallbacks_none():
|
|
router: Final = _primary_and_backup_router(fallbacks=[{"primary": ["backup"]}])
|
|
|
|
async def fake_acompletion(**kwargs):
|
|
return _stream_for(**kwargs)
|
|
|
|
with patch("litellm.acompletion", side_effect=fake_acompletion) as provider_calls:
|
|
with pytest.raises(litellm.InternalServerError, match="provider 500 from openai/primary-model"):
|
|
await router.acompletion(
|
|
model="primary", messages=[{"role": "user", "content": "hi"}], stream=True, fallbacks=None
|
|
)
|
|
|
|
assert _groups_called(provider_calls) == ["primary"]
|
|
|
|
|
|
class _AsyncList:
|
|
"""Generic async iterator over a list — used as the fallback response."""
|
|
|
|
def __init__(self, items=()):
|
|
self._items = list(items)
|
|
self._idx = 0
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self):
|
|
if self._idx >= len(self._items):
|
|
raise StopAsyncIteration
|
|
item = self._items[self._idx]
|
|
self._idx += 1
|
|
return item
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_aresponses_mid_stream_order_fallback_hop_drops_the_encrypted_reasoning_the_next_provider_cannot_decrypt(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
"""A Codex-style multi-turn history replays the order-1 provider's encrypted reasoning. When that
|
|
provider's stream breaks before its first output chunk, the order-2 hop must not replay reasoning
|
|
the next provider cannot decrypt; the readable summary stays."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
|
|
def history() -> list:
|
|
return [
|
|
{"type": "message", "role": "user", "content": "What is 17*23?"},
|
|
{
|
|
"type": "reasoning",
|
|
"id": "rs_order1",
|
|
"encrypted_content": "gAAAAA-minted-by-order-1",
|
|
"summary": [{"type": "summary_text", "text": "multiply 17 by 23"}],
|
|
},
|
|
{"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "391"}]},
|
|
{"type": "message", "role": "user", "content": "And 19*21?"},
|
|
]
|
|
|
|
def response_body(response_id: str, model: str, status: str, output: list) -> dict:
|
|
return {
|
|
"id": response_id,
|
|
"object": "response",
|
|
"created_at": 0,
|
|
"status": status,
|
|
"model": model,
|
|
"output": output,
|
|
"usage": {"input_tokens": 1, "output_tokens": 1, "total_tokens": 2} if status == "completed" else None,
|
|
}
|
|
|
|
def sse(events: list) -> httpx.Response:
|
|
body: Final = "".join(f"data: {json.dumps(event)}\n\n" for event in events)
|
|
return httpx.Response(200, content=body, headers={"content-type": "text/event-stream"})
|
|
|
|
openai_opened: Final = response_body("resp_openai", "gpt-6-astra", "in_progress", [])
|
|
openai_route: Final = respx_mock.post("https://api.openai.com/v1/responses").mock(
|
|
return_value=sse(
|
|
[
|
|
{"type": "response.created", "sequence_number": 0, "response": openai_opened},
|
|
{"type": "response.in_progress", "sequence_number": 1, "response": openai_opened},
|
|
{
|
|
"type": "error",
|
|
"sequence_number": 2,
|
|
"error": {
|
|
"type": "server_error",
|
|
"code": "server_error",
|
|
"message": "The server had an error while processing your request",
|
|
"param": None,
|
|
},
|
|
},
|
|
]
|
|
)
|
|
)
|
|
mantle_answer: Final = [
|
|
{
|
|
"id": "msg_mantle",
|
|
"type": "message",
|
|
"status": "completed",
|
|
"role": "assistant",
|
|
"content": [{"type": "output_text", "text": "399", "annotations": []}],
|
|
}
|
|
]
|
|
mantle_route: Final = respx_mock.post("https://bedrock-mantle.us-east-1.api.aws/openai/v1/responses").mock(
|
|
return_value=sse(
|
|
[
|
|
{
|
|
"type": "response.created",
|
|
"sequence_number": 0,
|
|
"response": response_body("resp_mantle", "openai.gpt-6-astra", "in_progress", []),
|
|
},
|
|
{
|
|
"type": "response.completed",
|
|
"sequence_number": 1,
|
|
"response": response_body("resp_mantle", "openai.gpt-6-astra", "completed", mantle_answer),
|
|
},
|
|
]
|
|
)
|
|
)
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra", "api_key": "openai-key", "order": 1},
|
|
"model_info": {"id": "openai-order-1"},
|
|
},
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {
|
|
"model": "bedrock_mantle/openai.gpt-6-astra",
|
|
"api_key": "mantle-bearer-token",
|
|
"aws_region_name": "us-east-1",
|
|
"order": 2,
|
|
},
|
|
"model_info": {"id": "mantle-order-2"},
|
|
},
|
|
],
|
|
num_retries=0,
|
|
)
|
|
stream = await router.aresponses(model="gpt-6-astra", input=history(), store=False, stream=True)
|
|
collected = [event async for event in stream]
|
|
|
|
assert [event.type for event in collected] == ["response.created", "response.completed"]
|
|
assert json.loads(openai_route.calls.last.request.read())["input"] == history()
|
|
assert json.loads(mantle_route.calls.last.request.read())["input"] == [
|
|
{"type": "message", "role": "user", "content": "What is 17*23?"},
|
|
{"type": "reasoning", "summary": [{"type": "summary_text", "text": "multiply 17 by 23"}]},
|
|
{"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "391"}]},
|
|
{"type": "message", "role": "user", "content": "And 19*21?"},
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_aresponses_mid_stream_order_fallback_hop_keeps_the_encrypted_reasoning_the_same_boundary_can_decrypt(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
"""The mid-stream hop re-enters the chain on a snapshot taken before routing, so the snapshot has to
|
|
carry the deployment that streamed and failed: a same-boundary order-2 deployment can decrypt that
|
|
deployment's unmarked reasoning and must receive it unchanged."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
|
|
def history() -> list:
|
|
return [
|
|
{"type": "message", "role": "user", "content": "What is 17*23?"},
|
|
{
|
|
"type": "reasoning",
|
|
"id": "rs_order1",
|
|
"encrypted_content": "gAAAAA-minted-by-order-1",
|
|
"summary": [{"type": "summary_text", "text": "multiply 17 by 23"}],
|
|
},
|
|
{"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "391"}]},
|
|
{"type": "message", "role": "user", "content": "And 19*21?"},
|
|
]
|
|
|
|
def response_body(response_id: str, model: str, status: str, output: list) -> dict:
|
|
return {
|
|
"id": response_id,
|
|
"object": "response",
|
|
"created_at": 0,
|
|
"status": status,
|
|
"model": model,
|
|
"output": output,
|
|
"usage": {"input_tokens": 1, "output_tokens": 1, "total_tokens": 2} if status == "completed" else None,
|
|
}
|
|
|
|
def sse(events: list) -> httpx.Response:
|
|
body: Final = "".join(f"data: {json.dumps(event)}\n\n" for event in events)
|
|
return httpx.Response(200, content=body, headers={"content-type": "text/event-stream"})
|
|
|
|
order_1_opened: Final = response_body("resp_order1", "gpt-6-astra", "in_progress", [])
|
|
order_2_answer: Final = [
|
|
{
|
|
"id": "msg_order2",
|
|
"type": "message",
|
|
"status": "completed",
|
|
"role": "assistant",
|
|
"content": [{"type": "output_text", "text": "399", "annotations": []}],
|
|
}
|
|
]
|
|
openai_route: Final = respx_mock.post("https://api.openai.com/v1/responses").mock(
|
|
side_effect=[
|
|
sse(
|
|
[
|
|
{"type": "response.created", "sequence_number": 0, "response": order_1_opened},
|
|
{"type": "response.in_progress", "sequence_number": 1, "response": order_1_opened},
|
|
{
|
|
"type": "error",
|
|
"sequence_number": 2,
|
|
"error": {
|
|
"type": "server_error",
|
|
"code": "server_error",
|
|
"message": "The server had an error while processing your request",
|
|
"param": None,
|
|
},
|
|
},
|
|
]
|
|
),
|
|
sse(
|
|
[
|
|
{
|
|
"type": "response.created",
|
|
"sequence_number": 0,
|
|
"response": response_body("resp_order2", "gpt-6-astra-mini", "in_progress", []),
|
|
},
|
|
{
|
|
"type": "response.completed",
|
|
"sequence_number": 1,
|
|
"response": response_body("resp_order2", "gpt-6-astra-mini", "completed", order_2_answer),
|
|
},
|
|
]
|
|
),
|
|
]
|
|
)
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-6-astra",
|
|
"api_base": "https://api.openai.com/v1",
|
|
"api_key": "openai-key",
|
|
"order": 1,
|
|
},
|
|
"model_info": {"id": "openai-order-1"},
|
|
},
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-6-astra-mini",
|
|
"api_base": "https://api.openai.com/v1",
|
|
"api_key": "openai-key",
|
|
"order": 2,
|
|
},
|
|
"model_info": {"id": "openai-order-2"},
|
|
},
|
|
],
|
|
num_retries=0,
|
|
)
|
|
stream = await router.aresponses(model="gpt-6-astra", input=history(), store=False, stream=True)
|
|
collected = [event async for event in stream]
|
|
|
|
assert [event.type for event in collected] == ["response.created", "response.completed"]
|
|
assert [json.loads(call.request.read())["input"] for call in openai_route.calls] == [history(), history()]
|
|
|
|
|
|
async def _events_until_error(stream: AsyncIterable[object]) -> AsyncIterator[object]:
|
|
try:
|
|
async for chunk in stream:
|
|
yield chunk
|
|
except Exception as error:
|
|
yield error
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("event_type", "held_event_count", "expected"),
|
|
[
|
|
("response.created", 0, True),
|
|
("response.in_progress", 1, True),
|
|
("response.queued", MAX_HELD_PRE_OUTPUT_RESPONSES_EVENTS - 1, True),
|
|
("response.in_progress", MAX_HELD_PRE_OUTPUT_RESPONSES_EVENTS, False),
|
|
("response.output_item.added", 0, False),
|
|
("response.output_text.delta", 0, False),
|
|
("response.completed", 0, False),
|
|
],
|
|
)
|
|
def test_responses_stream_holds_event_holds_only_pre_output_lifecycle_events_under_the_cap(
|
|
event_type: str, held_event_count: int, expected: bool
|
|
):
|
|
assert _responses_stream_holds_event(MagicMock(type=event_type), held_event_count) is expected
|
|
|
|
|
|
_BACKGROUND_PRIMARY_URL: Final = "https://api.openai.com/v1/responses"
|
|
_BACKGROUND_BACKUP_URL: Final = "https://backup.openai.test/v1/responses"
|
|
|
|
|
|
def _background_sse_frame(event: Mapping[str, object]) -> bytes:
|
|
return f"data: {json.dumps(event)}\n\n".encode()
|
|
|
|
|
|
def _background_response_body(status: str, background: bool) -> dict[str, object]:
|
|
return {
|
|
"id": "resp_upstream_run",
|
|
"object": "response",
|
|
"created_at": 0,
|
|
"status": status,
|
|
"model": "gpt-6.1-terra",
|
|
"background": background,
|
|
"output": [],
|
|
"usage": {"input_tokens": 1, "output_tokens": 1, "total_tokens": 2} if status == "completed" else None,
|
|
}
|
|
|
|
|
|
def _paused_background_stream(
|
|
output_may_start: asyncio.Event,
|
|
upstream_paused: asyncio.Event,
|
|
background: bool,
|
|
after_pause: tuple[dict[str, object], ...],
|
|
) -> httpx.Response:
|
|
async def body() -> AsyncIterator[bytes]:
|
|
yield _background_sse_frame(
|
|
{
|
|
"type": "response.created",
|
|
"sequence_number": 0,
|
|
"response": _background_response_body("queued", background),
|
|
}
|
|
)
|
|
yield _background_sse_frame(
|
|
{
|
|
"type": "response.queued",
|
|
"sequence_number": 1,
|
|
"response": _background_response_body("queued", background),
|
|
}
|
|
)
|
|
upstream_paused.set()
|
|
await output_may_start.wait()
|
|
for event in after_pause:
|
|
yield _background_sse_frame(event)
|
|
|
|
return httpx.Response(200, content=body(), headers={"content-type": "text/event-stream"})
|
|
|
|
|
|
def _background_output_then_completed(background: bool) -> tuple[dict[str, object], ...]:
|
|
return (
|
|
{"type": "response.output_text.delta", "sequence_number": 2, "item_id": "msg_1", "delta": "41"},
|
|
{
|
|
"type": "response.completed",
|
|
"sequence_number": 3,
|
|
"response": _background_response_body("completed", background),
|
|
},
|
|
)
|
|
|
|
|
|
def _background_router(
|
|
model_name: str,
|
|
litellm_params: dict[str, object],
|
|
fallbacks: list[dict[str, list[str]]] | None,
|
|
content_policy_fallbacks: list[dict[str, list[str]]] | None = None,
|
|
) -> Router:
|
|
return litellm.Router(
|
|
model_list=[
|
|
{"model_name": model_name, "litellm_params": litellm_params},
|
|
{
|
|
"model_name": "backup",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-6.1-terra",
|
|
"api_key": "backup-key",
|
|
"api_base": "https://backup.openai.test/v1",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=fallbacks,
|
|
content_policy_fallbacks=content_policy_fallbacks,
|
|
num_retries=0,
|
|
)
|
|
|
|
|
|
async def _reached_client_before_upstream_paused(
|
|
first: "asyncio.Future[object]", upstream_paused: asyncio.Event
|
|
) -> bool:
|
|
paused: Final = asyncio.ensure_future(upstream_paused.wait())
|
|
done, _ = await asyncio.wait({first, paused}, return_when=asyncio.FIRST_COMPLETED)
|
|
paused.cancel()
|
|
return first in done
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("fallbacks", "request_kwargs"),
|
|
[
|
|
pytest.param(None, {}, id="no-fallback-configured"),
|
|
pytest.param([{"gpt-bg": ["backup"]}], {"disable_fallbacks": True}, id="fallbacks-disabled-for-the-request"),
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_aresponses_background_stream_hands_the_response_id_to_the_client_before_output(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
fallbacks: list[dict[str, list[str]]] | None,
|
|
request_kwargs: dict[str, bool],
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
output_may_start: Final = asyncio.Event()
|
|
upstream_paused: Final = asyncio.Event()
|
|
respx_mock.post(_BACKGROUND_PRIMARY_URL).mock(
|
|
return_value=_paused_background_stream(
|
|
output_may_start, upstream_paused, True, _background_output_then_completed(True)
|
|
)
|
|
)
|
|
router: Final = _background_router("gpt-bg", {"model": "openai/gpt-6.1-terra", "api_key": "k"}, fallbacks)
|
|
|
|
stream: Final = await router.aresponses(model="gpt-bg", input="hi", background=True, stream=True, **request_kwargs)
|
|
first: Final = asyncio.ensure_future(anext(stream))
|
|
arrived_before_output: Final = await _reached_client_before_upstream_paused(first, upstream_paused)
|
|
output_may_start.set()
|
|
created: Final = await first
|
|
rest: Final = [event async for event in stream]
|
|
|
|
assert arrived_before_output is True
|
|
assert created.type == "response.created"
|
|
assert [event.type for event in rest] == [
|
|
"response.queued",
|
|
"response.output_text.delta",
|
|
"response.completed",
|
|
]
|
|
assert rest[-1].response.id == created.response.id
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("litellm_params", "background", "fallbacks"),
|
|
[
|
|
pytest.param({"model": "openai/gpt-6.1-terra", "api_key": "k"}, False, None, id="foreground-request"),
|
|
pytest.param(
|
|
{"model": "openai/gpt-6.1-terra", "api_key": "k"}, True, [{"gpt-bg": ["backup"]}], id="fallback-configured"
|
|
),
|
|
pytest.param(
|
|
{
|
|
"model": "azure/gpt-6.1-terra",
|
|
"api_key": "k",
|
|
"api_base": "https://bg-resource.openai.azure.com",
|
|
"api_version": "preview",
|
|
},
|
|
True,
|
|
None,
|
|
id="azure-deployment",
|
|
),
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_aresponses_stream_keeps_holding_lifecycle_events_until_output_off_the_eligible_background_route(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
litellm_params: dict[str, object],
|
|
background: bool,
|
|
fallbacks: list[dict[str, list[str]]] | None,
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
output_may_start: Final = asyncio.Event()
|
|
upstream_paused: Final = asyncio.Event()
|
|
respx_mock.route(method="POST", path__regex=r"/responses$").mock(
|
|
return_value=_paused_background_stream(
|
|
output_may_start, upstream_paused, background, _background_output_then_completed(background)
|
|
)
|
|
)
|
|
router: Final = _background_router("gpt-bg", litellm_params, fallbacks)
|
|
|
|
stream: Final = await router.aresponses(model="gpt-bg", input="hi", background=background, stream=True)
|
|
first: Final = asyncio.ensure_future(anext(stream))
|
|
arrived_before_output: Final = await _reached_client_before_upstream_paused(first, upstream_paused)
|
|
output_may_start.set()
|
|
events: Final = [await first, *[event async for event in stream]]
|
|
|
|
assert arrived_before_output is False
|
|
assert upstream_paused.is_set()
|
|
assert [event.type for event in events] == [
|
|
"response.created",
|
|
"response.queued",
|
|
"response.output_text.delta",
|
|
"response.completed",
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_aresponses_background_stream_surfaces_a_later_error_instead_of_a_fallback_once_its_id_is_out(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
output_may_start: Final = asyncio.Event()
|
|
upstream_paused: Final = asyncio.Event()
|
|
backup_output_may_start: Final = asyncio.Event()
|
|
backup_output_may_start.set()
|
|
server_error: Final = {
|
|
"type": "error",
|
|
"sequence_number": 2,
|
|
"error": {"type": "server_error", "code": "server_error", "message": "upstream died", "param": None},
|
|
}
|
|
primary_route: Final = respx_mock.post(_BACKGROUND_PRIMARY_URL).mock(
|
|
return_value=_paused_background_stream(output_may_start, upstream_paused, True, (server_error,))
|
|
)
|
|
backup_route: Final = respx_mock.post(_BACKGROUND_BACKUP_URL).mock(
|
|
return_value=_paused_background_stream(
|
|
backup_output_may_start, asyncio.Event(), True, _background_output_then_completed(True)
|
|
)
|
|
)
|
|
router: Final = _background_router("gpt-bg", {"model": "openai/gpt-6.1-terra", "api_key": "k"}, None)
|
|
|
|
stream: Final = await router.aresponses(model="gpt-bg", input="hi", background=True, stream=True)
|
|
first: Final = asyncio.ensure_future(anext(stream))
|
|
arrived_before_output: Final = await _reached_client_before_upstream_paused(first, upstream_paused)
|
|
router.fallbacks = [{"gpt-bg": ["backup"]}]
|
|
output_may_start.set()
|
|
created: Final = await first
|
|
rest: Final = [item async for item in _events_until_error(stream)]
|
|
|
|
assert arrived_before_output is True
|
|
assert created.type == "response.created"
|
|
assert [item.type for item in rest[:-1]] == ["response.queued"]
|
|
assert isinstance(rest[-1], litellm.InternalServerError)
|
|
assert "upstream died" in str(rest[-1])
|
|
assert primary_route.call_count == 1
|
|
assert backup_route.call_count == 0
|
|
|
|
|
|
_BACKGROUND_TEXT_DELTA: Final = {
|
|
"type": "response.output_text.delta",
|
|
"sequence_number": 2,
|
|
"item_id": "msg_1",
|
|
"delta": "41",
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("fallbacks", "content_policy_fallbacks", "output_before_refusal", "router_note_expected"),
|
|
[
|
|
pytest.param(None, None, (), True, id="no-fallback-configured"),
|
|
pytest.param([{"other-group": ["backup"]}], None, (), True, id="fallbacks-for-another-group"),
|
|
pytest.param(None, [{"other-group": ["backup"]}], (), True, id="content-policy-fallbacks-for-another-group"),
|
|
pytest.param(None, None, (_BACKGROUND_TEXT_DELTA,), False, id="refused-after-output-text"),
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_aresponses_background_stream_content_policy_error_reads_the_same_once_its_id_is_out(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
fallbacks: list[dict[str, list[str]]] | None,
|
|
content_policy_fallbacks: list[dict[str, list[str]]] | None,
|
|
output_before_refusal: tuple[dict[str, object], ...],
|
|
router_note_expected: bool,
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
released_output_may_start: Final = asyncio.Event()
|
|
released_upstream_paused: Final = asyncio.Event()
|
|
held_output_may_start: Final = asyncio.Event()
|
|
held_output_may_start.set()
|
|
refusal: Final = {
|
|
"type": "error",
|
|
"sequence_number": 2 + len(output_before_refusal),
|
|
"error": {
|
|
"type": "invalid_request_error",
|
|
"code": "content_policy_violation",
|
|
"message": "Your request was rejected as a result of our safety system.",
|
|
"param": None,
|
|
},
|
|
}
|
|
after_pause: Final = (*output_before_refusal, refusal)
|
|
primary_route: Final = respx_mock.post(_BACKGROUND_PRIMARY_URL).mock(
|
|
side_effect=[
|
|
_paused_background_stream(released_output_may_start, released_upstream_paused, True, after_pause),
|
|
_paused_background_stream(held_output_may_start, asyncio.Event(), False, after_pause),
|
|
]
|
|
)
|
|
backup_route: Final = respx_mock.post(_BACKGROUND_BACKUP_URL)
|
|
router: Final = _background_router(
|
|
"gpt-bg", {"model": "openai/gpt-6.1-terra", "api_key": "k"}, fallbacks, content_policy_fallbacks
|
|
)
|
|
|
|
released: Final = await router.aresponses(model="gpt-bg", input="hi", background=True, stream=True)
|
|
first: Final = asyncio.ensure_future(anext(released))
|
|
arrived_before_output: Final = await _reached_client_before_upstream_paused(first, released_upstream_paused)
|
|
released_output_may_start.set()
|
|
await first
|
|
released_error: Final = [item async for item in _events_until_error(released)][-1]
|
|
held: Final = await router.aresponses(model="gpt-bg", input="hi", stream=True)
|
|
held_error: Final = [item async for item in _events_until_error(held)][-1]
|
|
|
|
assert arrived_before_output is True
|
|
assert isinstance(released_error, litellm.ContentPolicyViolationError)
|
|
assert isinstance(held_error, litellm.ContentPolicyViolationError)
|
|
assert ("LiteLLM: model group 'gpt-bg' failed with the error above" in held_error.message) is router_note_expected
|
|
assert released_error.message == held_error.message
|
|
assert primary_route.call_count == 2
|
|
assert backup_route.call_count == 0
|
|
|
|
|
|
def test_should_include_deployment():
|
|
"""Test that Router._core_should_include_deployment returns the correct response"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "model_name_a28a12f9-3e44-4861-bd4f-325f2d309ce8_cd5dc6fb-b046-4e05-ae1d-32ba4d936266",
|
|
"litellm_params": {"model": "openai/*"},
|
|
"model_info": {
|
|
"team_id": "a28a12f9-3e44-4861-bd4f-325f2d309ce8",
|
|
"team_public_model_name": "openai/*",
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
model = {
|
|
"model_name": "model_name_a28a12f9-3e44-4861-bd4f-325f2d309ce8_cd5dc6fb-b046-4e05-ae1d-32ba4d936266",
|
|
"litellm_params": {
|
|
"api_key": "sk-proj-1234567890",
|
|
"custom_llm_provider": "openai",
|
|
"use_in_pass_through": False,
|
|
"use_litellm_proxy": False,
|
|
"merge_reasoning_content_in_choices": False,
|
|
"model": "openai/*",
|
|
},
|
|
"model_info": {
|
|
"id": "95f58039-d54a-4d1c-b700-5e32e99a1120",
|
|
"db_model": True,
|
|
"updated_by": "64a2f787-0863-4d76-9516-2dc49c1598e8",
|
|
"created_by": "64a2f787-0863-4d76-9516-2dc49c1598e8",
|
|
"team_id": "a28a12f9-3e44-4861-bd4f-325f2d309ce8",
|
|
"team_public_model_name": "openai/*",
|
|
"mode": "completion",
|
|
"access_groups": ["restricted-models-openai"],
|
|
},
|
|
}
|
|
model_name = "openai/o4-mini-deep-research"
|
|
team_id = "a28a12f9-3e44-4861-bd4f-325f2d309ce8"
|
|
assert router.get_model_list(
|
|
model_name=model_name,
|
|
team_id=team_id,
|
|
)
|
|
|
|
|
|
_MULTILINGUAL_PROMPT: Final = [{"role": "user", "content": "日本語のテキストとहिन्दी पाठ और русский текст. " * 20}]
|
|
|
|
|
|
def _context_limited_router(*served: str) -> litellm.Router:
|
|
limit: Final = (
|
|
litellm.token_counter(model="openai/gpt-6.1-sol", messages=_MULTILINGUAL_PROMPT)
|
|
+ litellm.token_counter(model="prod", messages=_MULTILINGUAL_PROMPT)
|
|
) // 2
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "prod",
|
|
"litellm_params": {"model": model, "api_key": "sk-test"},
|
|
"model_info": {"max_input_tokens": limit},
|
|
}
|
|
for model in served
|
|
],
|
|
enable_pre_call_checks=True,
|
|
)
|
|
|
|
|
|
def test_pre_call_checks_count_a_group_of_one_openai_model_with_that_models_tokenizer():
|
|
router: Final = _context_limited_router("openai/gpt-6.1-sol")
|
|
|
|
kept: Final = router._pre_call_checks(
|
|
model="prod", healthy_deployments=router.get_model_list(model_name="prod"), messages=_MULTILINGUAL_PROMPT
|
|
)
|
|
|
|
assert len(kept) == 1
|
|
|
|
|
|
def test_pre_call_checks_count_a_group_mixing_in_another_vendor_by_its_own_name():
|
|
router: Final = _context_limited_router("openai/gpt-6.1-sol", "anthropic/claude-sonnet-5-5")
|
|
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
router._pre_call_checks(
|
|
model="prod", healthy_deployments=router.get_model_list(model_name="prod"), messages=_MULTILINGUAL_PROMPT
|
|
)
|
|
|
|
|
|
_EMBEDDING_CONTEXT_WINDOW: Final = 8_192
|
|
_EMBEDDING_IMAGE_INPUT: Final = f"data:image/png;base64,{base64.b64encode(bytes(range(256)) * 1024).decode()}"
|
|
|
|
|
|
def _embedding_context_router(backend: str, window: int = _EMBEDDING_CONTEXT_WINDOW) -> litellm.Router:
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "embedder",
|
|
"litellm_params": {"model": backend, "api_key": "sk-test"},
|
|
"model_info": {"max_input_tokens": window},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("backend", "shape"),
|
|
[
|
|
("vertex_ai/gemini-embedding-2", "pdf"),
|
|
("vertex_ai/gemini-embedding-2", "pdf_file_block"),
|
|
("vertex_ai/gemini-embedding-2", "two_pdfs"),
|
|
("bedrock/amazon.titan-embed-image-v1", "image"),
|
|
("bedrock/amazon.titan-embed-image-v1", "two_images"),
|
|
],
|
|
)
|
|
def test_context_window_pre_call_check_leaves_embedding_media_to_the_backend(backend: str, shape: str):
|
|
window: Final = litellm.get_model_info(backend)["max_input_tokens"]
|
|
assert window is not None
|
|
router: Final = _embedding_context_router(backend, window)
|
|
pdf: Final = f"data:application/pdf;base64,{base64.b64encode(bytes(window * 8)).decode()}"
|
|
inputs: Final = {
|
|
"pdf": pdf,
|
|
"pdf_file_block": [{"type": "file", "file": {"file_data": pdf}}],
|
|
"two_pdfs": [pdf, pdf],
|
|
"image": _EMBEDDING_IMAGE_INPUT,
|
|
"two_images": [_EMBEDDING_IMAGE_INPUT, _EMBEDDING_IMAGE_INPUT],
|
|
}
|
|
|
|
kept: Final = router._pre_call_checks(
|
|
model="embedder", healthy_deployments=router.registry().deployments("embedder"), input=inputs[shape]
|
|
)
|
|
|
|
assert len(kept) == 1
|
|
|
|
|
|
def test_context_window_pre_call_check_counts_an_embedding_image_as_text_on_a_text_only_backend():
|
|
router: Final = _embedding_context_router("text-embedding-3-small")
|
|
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
router._pre_call_checks(
|
|
model="embedder",
|
|
healthy_deployments=router.registry().deployments("embedder"),
|
|
input=_EMBEDDING_IMAGE_INPUT,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("backend", "fits"),
|
|
[
|
|
pytest.param("vertex_ai/gemini-embedding-2", True, id="image-backend-without-an-openai-tokenizer"),
|
|
pytest.param("text-embedding-3-small", False, id="text-only-backend-counts-the-image"),
|
|
],
|
|
)
|
|
def test_context_window_pre_call_check_on_a_caption_next_to_an_embedding_image(backend: str, fits: bool, caplog):
|
|
caption: Final = "word " * 40
|
|
router: Final = _embedding_context_router(backend, window=20)
|
|
deployments: Final = router.registry().deployments("embedder")
|
|
|
|
with caplog.at_level(logging.ERROR, logger="LiteLLM Router"):
|
|
if fits:
|
|
kept: Final = router._pre_call_checks(
|
|
model="embedder", healthy_deployments=deployments, input=[caption, _EMBEDDING_IMAGE_INPUT]
|
|
)
|
|
assert len(kept) == 1
|
|
else:
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
router._pre_call_checks(
|
|
model="embedder", healthy_deployments=deployments, input=[caption, _EMBEDDING_IMAGE_INPUT]
|
|
)
|
|
|
|
assert "failed to count tokens" not in caplog.text
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_an_async_pre_call_check_reads_embedding_media_off_the_event_loop():
|
|
from tests.unit.litellm_core_utils.event_loop_lag import assert_loop_stayed_free, timed_with_loop_lags
|
|
|
|
router: Final = _embedding_context_router("vertex_ai/gemini-embedding-2", window=20)
|
|
caption_and_images: Final = ["a red square", *[_EMBEDDING_IMAGE_INPUT] * 400]
|
|
|
|
deployment, took, lags = await timed_with_loop_lags(
|
|
lambda: router.select("embedder", {}, input=caption_and_images)
|
|
)
|
|
|
|
assert deployment["litellm_params"]["model"] == "vertex_ai/gemini-embedding-2"
|
|
assert_loop_stayed_free(took, lags)
|
|
|
|
|
|
async def test_a_wildcard_route_checks_the_context_window_with_the_requested_openai_models_tokenizer():
|
|
limit: Final = (
|
|
litellm.token_counter(model="openai/gpt-6.1-sol", messages=_MULTILINGUAL_PROMPT)
|
|
+ litellm.token_counter(model="my-model", messages=_MULTILINGUAL_PROMPT)
|
|
) // 2
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {"model": "openai/*", "api_key": "sk-test"},
|
|
"model_info": {"max_input_tokens": limit},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
)
|
|
|
|
sync_response: Final = router.completion(
|
|
model="openai/gpt-6.1-sol", messages=_MULTILINGUAL_PROMPT, mock_response="ok"
|
|
)
|
|
async_response: Final = await router.acompletion(
|
|
model="openai/gpt-6.1-sol", messages=_MULTILINGUAL_PROMPT, mock_response="ok"
|
|
)
|
|
|
|
assert (sync_response.choices[0].message.content, async_response.choices[0].message.content) == ("ok", "ok")
|
|
|
|
|
|
_OVERSIZED_TOOL_DESCRIPTION = "look up the answer in the knowledge base. " * 40
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_aanthropic_messages_enforces_context_window_with_system_and_tools():
|
|
"""
|
|
End-to-end router regression for /v1/messages: a request whose only oversized
|
|
content lives in the top-level `system` field or in `tools` must trip the pre-call
|
|
context-window check instead of being dispatched (the deployment uses mock_response,
|
|
so reaching the provider handler would return a response rather than raise).
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "small-ctx",
|
|
"litellm_params": {"model": "anthropic/claude-3-5-haiku-20241022", "mock_response": "hi"},
|
|
"model_info": {"max_input_tokens": 20},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
)
|
|
messages = [{"role": "user", "content": "hi"}]
|
|
|
|
response = await router.aanthropic_messages(model="small-ctx", messages=messages, max_tokens=5)
|
|
assert response is not None
|
|
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
await router.aanthropic_messages(
|
|
model="small-ctx",
|
|
messages=messages,
|
|
max_tokens=5,
|
|
system="You are a meticulous assistant. " * 40,
|
|
)
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
await router.aanthropic_messages(
|
|
model="small-ctx",
|
|
messages=messages,
|
|
max_tokens=5,
|
|
tools=[
|
|
{
|
|
"name": "lookup",
|
|
"description": _OVERSIZED_TOOL_DESCRIPTION,
|
|
"input_schema": {"type": "object", "properties": {"q": {"type": "string"}}},
|
|
}
|
|
],
|
|
)
|
|
|
|
|
|
def test_count_pre_call_check_tokens_across_api_surfaces():
|
|
"""
|
|
_count_pre_call_check_tokens must count tokens from chat `messages`, a Responses
|
|
API string `input`, and a Responses API list `input`, and raise when given neither.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "m", "litellm_params": {"model": "gpt-3.5-turbo"}},
|
|
],
|
|
)
|
|
|
|
messages_tokens = router._count_pre_call_check_tokens(
|
|
messages=[{"role": "user", "content": "hello world"}], input=None
|
|
)
|
|
string_input_tokens = router._count_pre_call_check_tokens(messages=None, input="hello world")
|
|
list_input_tokens = router._count_pre_call_check_tokens(
|
|
messages=None, input=[{"role": "user", "content": "hello world"}]
|
|
)
|
|
|
|
assert messages_tokens > 0
|
|
assert string_input_tokens > 0
|
|
assert list_input_tokens > 0
|
|
|
|
with pytest.raises(ValueError, match="Either messages or input must be provided to count tokens"):
|
|
router._count_pre_call_check_tokens(messages=None, input=None)
|
|
|
|
|
|
def test_count_pre_call_check_tokens_skips_the_history_a_compaction_item_replaces():
|
|
router = litellm.Router(model_list=[{"model_name": "m", "litellm_params": {"model": "gpt-3.5-turbo"}}])
|
|
summary: Final = "The user planned a Tokyo trip."
|
|
compaction_item: Final = {
|
|
"type": "compaction",
|
|
"id": "cmp_1",
|
|
"encrypted_content": json.dumps({"type": "compaction", "content": summary}),
|
|
}
|
|
history: Final = [
|
|
{"role": "user", "content": "Plan a Tokyo trip. " * 200},
|
|
{"role": "assistant", "content": "Day one, Asakusa. " * 200},
|
|
]
|
|
next_turn: Final = {"role": "user", "content": "And Osaka?"}
|
|
|
|
compacted_tokens: Final = router._count_pre_call_check_tokens(
|
|
messages=None, input=[*history, compaction_item, next_turn]
|
|
)
|
|
|
|
assert compacted_tokens == router._count_pre_call_check_tokens(
|
|
messages=None, input=[{"role": "user", "content": summary}, next_turn]
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("local_model_cost_map", "isolate_router_model_cost_state")
|
|
async def test_aresponses_enforces_context_window_pre_call_check():
|
|
"""
|
|
End-to-end router regression: a Responses API call whose `input` exceeds the
|
|
deployment's max_input_tokens must be filtered by the pre-call check, raising
|
|
ContextWindowExceededError instead of being silently routed. This guards the
|
|
wiring that forwards `input` from the generic-call path into deployment selection
|
|
(the deployment uses mock_response, so the check must trip before any real call).
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "small-ctx",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "mock_response": "hi"},
|
|
"model_info": {"max_input_tokens": 5},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
)
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
await router.aresponses(
|
|
model="small-ctx",
|
|
input="this responses input is definitely much longer than five tokens for sure",
|
|
)
|
|
|
|
|
|
def _embedding_router(max_input_tokens: int) -> litellm.Router:
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "embed",
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-3-small",
|
|
"api_key": "sk-test",
|
|
"mock_response": [0.1, 0.2],
|
|
},
|
|
"model_info": {"max_input_tokens": max_input_tokens},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"embedding_input",
|
|
[
|
|
pytest.param(["one two three four five six"] * 3, id="texts"),
|
|
pytest.param([list(range(6))] * 3, id="token-id-lists"),
|
|
pytest.param(list(range(10)), id="token-ids-at-the-limit"),
|
|
],
|
|
)
|
|
async def test_aembedding_accepts_a_batch_whose_inputs_each_fit_the_context_window(embedding_input, caplog):
|
|
router: Final = _embedding_router(max_input_tokens=10)
|
|
|
|
with caplog.at_level(logging.ERROR, logger="LiteLLM Router"):
|
|
response: Final = await router.aembedding(model="embed", input=embedding_input)
|
|
|
|
assert response.data
|
|
assert "failed to count tokens" not in caplog.text
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"embedding_input",
|
|
[
|
|
pytest.param(["one", " ".join(["word"] * 20)], id="texts"),
|
|
pytest.param(list(range(11)), id="token-ids"),
|
|
pytest.param([[1], list(range(11))], id="token-id-lists"),
|
|
],
|
|
)
|
|
async def test_aembedding_rejects_a_batch_with_one_input_over_the_context_window(embedding_input):
|
|
router: Final = _embedding_router(max_input_tokens=10)
|
|
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
await router.aembedding(model="embed", input=embedding_input)
|
|
|
|
|
|
def test_embedding_rejects_a_batch_with_one_input_over_the_context_window():
|
|
router: Final = _embedding_router(max_input_tokens=10)
|
|
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
router.embedding(model="embed", input=["one", " ".join(["word"] * 20)])
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"embedding_input",
|
|
[
|
|
pytest.param(["one", " ".join(["word"] * 20)], id="texts"),
|
|
pytest.param(list(range(11)), id="token-ids"),
|
|
pytest.param([[1], list(range(11))], id="token-id-lists"),
|
|
],
|
|
)
|
|
async def test_aembedding_skips_the_context_window_check_when_token_counting_is_disabled(embedding_input, monkeypatch):
|
|
monkeypatch.setattr(litellm, "disable_token_counter", True)
|
|
router: Final = _embedding_router(max_input_tokens=10)
|
|
|
|
response: Final = await router.aembedding(model="embed", input=embedding_input)
|
|
|
|
assert response.data
|
|
|
|
|
|
async def _until_every_count_slot_is_taken() -> None:
|
|
from litellm.constants import TOKEN_COUNTER_MAX_CONCURRENT_COUNTS
|
|
from litellm.litellm_core_utils.token_counter import _count_offload_limiter_for_this_loop
|
|
|
|
while _count_offload_limiter_for_this_loop().borrowed_tokens < TOKEN_COUNTER_MAX_CONCURRENT_COUNTS:
|
|
await asyncio.sleep(0)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_chat_pre_call_count_does_not_wait_behind_long_embedding_batches():
|
|
from litellm.constants import TOKEN_COUNTER_MAX_CONCURRENT_COUNTS
|
|
|
|
window: Final = {"max_input_tokens": 10000}
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "embed",
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-3-small",
|
|
"api_key": "sk-test",
|
|
"mock_response": [0.1],
|
|
},
|
|
"model_info": window,
|
|
},
|
|
{
|
|
"model_name": "chat",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test", "mock_response": "hi"},
|
|
"model_info": window,
|
|
},
|
|
],
|
|
enable_pre_call_checks=True,
|
|
)
|
|
batch: Final = ["word " * 2100] * 76
|
|
batches: Final = tuple(
|
|
asyncio.create_task(router.aembedding(model="embed", input=batch))
|
|
for _ in range(TOKEN_COUNTER_MAX_CONCURRENT_COUNTS)
|
|
)
|
|
await asyncio.wait_for(_until_every_count_slot_is_taken(), timeout=30)
|
|
chat: Final = asyncio.create_task(router.acompletion(model="chat", messages=[{"role": "user", "content": "hi"}]))
|
|
|
|
first_done, _ = await asyncio.wait((chat, *batches), return_when=asyncio.FIRST_COMPLETED)
|
|
await asyncio.gather(chat, *batches)
|
|
|
|
assert first_done == {chat}
|
|
|
|
|
|
def test_a_routing_plugin_counts_an_embedding_text_batch_by_its_longest_text():
|
|
long_text: Final = "word " * 4000
|
|
context: Final = RouterContext(_embedding_router(max_input_tokens=10))
|
|
|
|
assert context.count_pre_call_check_tokens(None, ["one", long_text]) == litellm.token_counter(text=long_text)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("local_model_cost_map", "isolate_router_model_cost_state")
|
|
@pytest.mark.parametrize(
|
|
"prompt_kwargs",
|
|
[
|
|
pytest.param({"instructions": "you are a helpful assistant. " * 20}, id="instructions"),
|
|
pytest.param(
|
|
{
|
|
"tools": [
|
|
{
|
|
"type": "function",
|
|
"name": "lookup",
|
|
"description": "look up a record by its identifier. " * 20,
|
|
"parameters": {"type": "object", "properties": {"q": {"type": "string"}}},
|
|
}
|
|
]
|
|
},
|
|
id="tools",
|
|
),
|
|
],
|
|
)
|
|
async def test_aresponses_with_empty_input_still_counts_its_other_prompt_content(prompt_kwargs):
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "small-ctx",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "mock_response": "hi"},
|
|
"model_info": {"max_input_tokens": 50},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
)
|
|
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
await router.aresponses(model="small-ctx", input=[], **prompt_kwargs)
|
|
|
|
|
|
def test_get_deployment_model_info_base_model_flow():
|
|
"""Test that get_deployment_model_info correctly handles the base model flow"""
|
|
from unittest.mock import patch
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "test-model",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
}
|
|
],
|
|
)
|
|
|
|
# Mock data for the test
|
|
mock_custom_model_info = {
|
|
"base_model": "gpt-3.5-turbo",
|
|
"input_cost_per_token": 0.001,
|
|
"output_cost_per_token": 0.002,
|
|
"custom_field": "custom_value",
|
|
}
|
|
|
|
mock_base_model_info = {
|
|
"key": "gpt-3.5-turbo",
|
|
"max_tokens": 4096,
|
|
"max_input_tokens": 4096,
|
|
"max_output_tokens": 4096,
|
|
"input_cost_per_token": 0.0015, # This should be overridden by custom model info
|
|
"output_cost_per_token": 0.002,
|
|
"litellm_provider": "openai",
|
|
"mode": "chat",
|
|
"supported_openai_params": ["temperature", "max_tokens"],
|
|
}
|
|
|
|
mock_litellm_model_name_info = {
|
|
"key": "test-model",
|
|
"max_tokens": 2048,
|
|
"max_input_tokens": 2048,
|
|
"max_output_tokens": 2048,
|
|
"input_cost_per_token": 0.0005,
|
|
"output_cost_per_token": 0.001,
|
|
"litellm_provider": "test_provider",
|
|
"mode": "completion",
|
|
"supported_openai_params": ["temperature"],
|
|
}
|
|
|
|
# Test Case 1: Base model flow with custom model info that has base_model
|
|
with patch.object(litellm, "model_cost", {"test-custom-model": mock_custom_model_info}):
|
|
with patch.object(litellm, "get_model_info") as mock_get_model_info:
|
|
# Configure mock returns
|
|
mock_get_model_info.side_effect = lambda model: {
|
|
"gpt-3.5-turbo": mock_base_model_info,
|
|
"test-model": mock_litellm_model_name_info,
|
|
}.get(model)
|
|
|
|
result = router.get_deployment_model_info(model_id="test-custom-model", model_name="test-model")
|
|
|
|
# Verify that get_model_info was called for both base model and model name
|
|
assert mock_get_model_info.call_count == 2
|
|
mock_get_model_info.assert_any_call(model="gpt-3.5-turbo") # base model call
|
|
mock_get_model_info.assert_any_call(model="test-model") # model name call
|
|
|
|
# Verify the result contains merged information
|
|
assert result is not None
|
|
|
|
# Test the correct merging behavior after fix:
|
|
# 1. base_model_info provides defaults, custom_model_info overrides (correct priority)
|
|
# 2. The result of step 1 gets merged into litellm_model_name_info (custom+base override litellm)
|
|
|
|
# Fields from custom model (should override base model values)
|
|
assert result["input_cost_per_token"] == 0.001 # From custom model (overrides base 0.0015)
|
|
assert result["output_cost_per_token"] == 0.002 # From custom model (same as base)
|
|
assert result["custom_field"] == "custom_value" # From custom model
|
|
|
|
# Fields from base model that weren't overridden by custom
|
|
assert result["max_tokens"] == 4096 # From base model
|
|
assert result["litellm_provider"] == "openai" # From base model
|
|
assert result["mode"] == "chat" # From base model (overrides litellm "completion")
|
|
|
|
# The key field comes from base model since both base and litellm have it
|
|
# and base model info overrides litellm model name info in final merge
|
|
assert result["key"] == "gpt-3.5-turbo" # From base model (overrides litellm key)
|
|
|
|
# Test Case 2: Custom model info without base_model
|
|
mock_custom_model_info_no_base = {
|
|
"input_cost_per_token": 0.001,
|
|
"output_cost_per_token": 0.002,
|
|
"custom_field": "custom_value",
|
|
}
|
|
|
|
with patch.object(
|
|
litellm,
|
|
"model_cost",
|
|
{"test-custom-model-no-base": mock_custom_model_info_no_base},
|
|
):
|
|
with patch.object(litellm, "get_model_info") as mock_get_model_info:
|
|
mock_get_model_info.side_effect = lambda model: {
|
|
"test-model": mock_litellm_model_name_info,
|
|
}.get(model)
|
|
|
|
result = router.get_deployment_model_info(model_id="test-custom-model-no-base", model_name="test-model")
|
|
|
|
# Should only call get_model_info once for model name (no base model)
|
|
assert mock_get_model_info.call_count == 1
|
|
mock_get_model_info.assert_called_with(model="test-model")
|
|
|
|
# Verify the result contains merged information
|
|
assert result is not None
|
|
assert result["input_cost_per_token"] == 0.001 # From custom model
|
|
assert result["max_tokens"] == 2048 # From litellm model name info
|
|
assert result["custom_field"] == "custom_value" # From custom model
|
|
assert result["mode"] == "completion" # From litellm model name info
|
|
|
|
# Test Case 3: No custom model info, only litellm model name info
|
|
with patch.object(litellm, "model_cost", {}): # Empty model cost
|
|
with patch.object(litellm, "get_model_info") as mock_get_model_info:
|
|
mock_get_model_info.side_effect = lambda model: {
|
|
"test-model": mock_litellm_model_name_info,
|
|
}.get(model)
|
|
|
|
result = router.get_deployment_model_info(model_id="non-existent-model", model_name="test-model")
|
|
|
|
# Should only call get_model_info once for model name
|
|
assert mock_get_model_info.call_count == 1
|
|
mock_get_model_info.assert_called_with(model="test-model")
|
|
|
|
# Result should be just the litellm model name info
|
|
assert result is not None
|
|
assert result == mock_litellm_model_name_info
|
|
|
|
# Test Case 4: Base model info retrieval fails (exception handling)
|
|
mock_custom_model_info_invalid_base = {
|
|
"base_model": "invalid-base-model",
|
|
"input_cost_per_token": 0.001,
|
|
"output_cost_per_token": 0.002,
|
|
}
|
|
|
|
with patch.object(
|
|
litellm,
|
|
"model_cost",
|
|
{"test-custom-model-invalid": mock_custom_model_info_invalid_base},
|
|
):
|
|
with patch.object(litellm, "get_model_info") as mock_get_model_info:
|
|
# Mock get_model_info to raise exception for invalid base model
|
|
def mock_get_model_info_side_effect(model):
|
|
if model == "invalid-base-model":
|
|
raise Exception("Model not found")
|
|
elif model == "test-model":
|
|
return mock_litellm_model_name_info
|
|
return None
|
|
|
|
mock_get_model_info.side_effect = mock_get_model_info_side_effect
|
|
|
|
result = router.get_deployment_model_info(model_id="test-custom-model-invalid", model_name="test-model")
|
|
|
|
# Should handle exception gracefully and still return merged result
|
|
assert result is not None
|
|
assert result["input_cost_per_token"] == 0.001 # From custom model
|
|
assert result["mode"] == "completion" # From litellm model name info
|
|
|
|
# Test Case 5: Both model_cost.get() and get_model_info() return None
|
|
with patch.object(litellm, "model_cost", {}):
|
|
with patch.object(litellm, "get_model_info", side_effect=Exception("Not found")):
|
|
result = router.get_deployment_model_info(model_id="non-existent", model_name="non-existent")
|
|
|
|
# Should return None when no model info is found
|
|
assert result is None
|
|
|
|
# Test Case 6: custom_model_info present but litellm_model_name_model_info is None
|
|
# (model has custom pricing in config but is not in built-in model_prices_and_context_window.json)
|
|
mock_custom_pricing_only = {
|
|
"input_cost_per_token": 1.74e-06,
|
|
"output_cost_per_token": 3.48e-06,
|
|
"cache_read_input_token_cost": 1.45e-08,
|
|
"mode": "chat",
|
|
}
|
|
|
|
with patch.object(
|
|
litellm,
|
|
"model_cost",
|
|
{"custom-model-id": mock_custom_pricing_only},
|
|
):
|
|
with patch.object(litellm, "get_model_info") as mock_get_model_info:
|
|
# Model NOT in built-in cost map — raise exception
|
|
mock_get_model_info.side_effect = Exception("Model not in cost map")
|
|
|
|
result = router.get_deployment_model_info(model_id="custom-model-id", model_name="unknown-model")
|
|
|
|
# Should return custom_model_info even when litellm_model_name_model_info is None
|
|
assert result is not None
|
|
assert result["input_cost_per_token"] == 1.74e-06
|
|
assert result["output_cost_per_token"] == 3.48e-06
|
|
assert result["cache_read_input_token_cost"] == 1.45e-08
|
|
assert result["mode"] == "chat"
|
|
|
|
# Test Case 7: custom_model_info with base_model but litellm_model_name_model_info None
|
|
mock_custom_with_base = {
|
|
"base_model": "some-base-model",
|
|
"input_cost_per_token": 0.01,
|
|
"output_cost_per_token": 0.02,
|
|
}
|
|
mock_base_info = {
|
|
"key": "some-base-model",
|
|
"max_tokens": 8192,
|
|
"mode": "chat",
|
|
"litellm_provider": "openai",
|
|
}
|
|
|
|
with patch.object(
|
|
litellm,
|
|
"model_cost",
|
|
{"custom-with-base": mock_custom_with_base},
|
|
):
|
|
with patch.object(litellm, "get_model_info") as mock_get_model_info:
|
|
|
|
def get_info_side_effect(model):
|
|
if model == "some-base-model":
|
|
return mock_base_info
|
|
raise Exception("Model not in cost map")
|
|
|
|
mock_get_model_info.side_effect = get_info_side_effect
|
|
|
|
result = router.get_deployment_model_info(model_id="custom-with-base", model_name="unknown-model")
|
|
|
|
# Should return custom_model_info merged with base model info
|
|
assert result is not None
|
|
assert result["input_cost_per_token"] == 0.01 # From custom (overrides base)
|
|
assert result["max_tokens"] == 8192 # From base model
|
|
assert result["litellm_provider"] == "openai" # From base model
|
|
|
|
print("✓ All base model flow test cases passed!")
|
|
|
|
|
|
@patch("litellm.model_cost", {})
|
|
def test_get_deployment_model_info_base_model_merge_priority():
|
|
"""Test that base model info merging respects the correct priority order"""
|
|
from unittest.mock import patch
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "test-model",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
}
|
|
],
|
|
)
|
|
|
|
# Test data with overlapping fields to test merge priority
|
|
mock_custom_model_info = {
|
|
"base_model": "gpt-4",
|
|
"input_cost_per_token": 0.01, # Should override base model value
|
|
"max_tokens": 8000, # Should override base model value
|
|
"custom_only_field": "custom_value",
|
|
}
|
|
|
|
mock_base_model_info = {
|
|
"key": "gpt-4",
|
|
"max_tokens": 4096, # Should be overridden by custom model
|
|
"input_cost_per_token": 0.03, # Should be overridden by custom model
|
|
"output_cost_per_token": 0.06, # Should be preserved (not in custom)
|
|
"litellm_provider": "openai",
|
|
"base_only_field": "base_value",
|
|
}
|
|
|
|
mock_litellm_model_name_info = {
|
|
"key": "test-model",
|
|
"max_tokens": 2048, # Should be overridden by final custom model info
|
|
"input_cost_per_token": 0.005, # Should be overridden by final custom model info
|
|
"output_cost_per_token": 0.01, # Should be overridden by final custom model info
|
|
"mode": "completion",
|
|
"litellm_only_field": "litellm_value",
|
|
}
|
|
|
|
with patch.object(litellm, "model_cost", {"custom-model-id": mock_custom_model_info}):
|
|
with patch.object(litellm, "get_model_info") as mock_get_model_info:
|
|
mock_get_model_info.side_effect = lambda model: {
|
|
"gpt-4": mock_base_model_info,
|
|
"test-model": mock_litellm_model_name_info,
|
|
}.get(model)
|
|
|
|
result = router.get_deployment_model_info(model_id="custom-model-id", model_name="test-model")
|
|
|
|
assert result is not None
|
|
|
|
# Test correct merge priority after fix:
|
|
# 1. base_model_info provides defaults
|
|
# 2. custom_model_info overrides base_model_info
|
|
# 3. Result from steps 1-2 overrides litellm_model_name_info
|
|
|
|
# Fields that should come from custom model info (highest priority)
|
|
assert result["input_cost_per_token"] == 0.01 # From custom model (overrides base 0.03)
|
|
assert result["max_tokens"] == 8000 # From custom model (overrides base 4096)
|
|
assert result["custom_only_field"] == "custom_value" # From custom model
|
|
|
|
# Fields that should come from base model (not overridden by custom)
|
|
assert result["output_cost_per_token"] == 0.06 # From base model (not in custom)
|
|
assert result["litellm_provider"] == "openai" # From base model (not in custom)
|
|
assert result["base_only_field"] == "base_value" # From base model (not in custom)
|
|
|
|
# Fields that should come from litellm model name info (not overridden by custom+base)
|
|
assert result["mode"] == "completion" # From litellm model name info (not in custom or base)
|
|
assert (
|
|
result["litellm_only_field"] == "litellm_value"
|
|
) # From litellm model name info (not in custom or base)
|
|
|
|
# Key comes from base model since both base and litellm have key fields
|
|
# and the merged custom+base overrides litellm in the final merge
|
|
assert result["key"] == "gpt-4"
|
|
|
|
print("✓ Base model merge priority test passed!")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_acompletion_with_unknown_model_and_default_fallback():
|
|
"""
|
|
Test that the router successfully uses a default fallback when a completely
|
|
unknown model is requested. It should not raise a BadRequestError.
|
|
This test verifies the fix for issue #15114.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "gpt-4o", # This is the fallback model
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4o-real", # The actual underlying model name
|
|
"api_key": "fake-key",
|
|
"api_base": "https://fake-endpoint.openai.azure.com/",
|
|
"mock_response": "this is the fallback response", # Mocked response to prevent real API calls
|
|
},
|
|
}
|
|
]
|
|
|
|
# Initialize the router with a default fallback
|
|
router = litellm.Router(model_list=model_list, default_fallbacks=["gpt-4o"])
|
|
|
|
messages = [{"role": "user", "content": "This call should succeed by falling back."}]
|
|
|
|
# Call completion with a model name that is NOT in the model_list
|
|
response = await router.acompletion(model="completely-unknown-model", messages=messages)
|
|
|
|
# Check that the call did not fail and we received a valid response object.
|
|
assert response is not None
|
|
|
|
# Check that the content of the response is from the MOCKED fallback model.
|
|
assert response.choices[0].message.content == "this is the fallback response"
|
|
|
|
# Check that the response object reports the model that was *actually* called.
|
|
assert response.model == "gpt-4o-real"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_acompletion_with_unknown_model_and_no_fallback():
|
|
"""
|
|
Test that the router still raises a BadRequestError for an unknown model
|
|
when no default fallbacks are configured. This ensures we don't break
|
|
the original behavior.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4o-real",
|
|
"api_key": "fake-key",
|
|
"mock_response": "this should not be called",
|
|
},
|
|
}
|
|
]
|
|
|
|
# Initialize the router WITHOUT any default fallbacks
|
|
router = litellm.Router(model_list=model_list)
|
|
|
|
messages = [{"role": "user", "content": "This call should fail."}]
|
|
|
|
# Use pytest.raises to assert that a BadRequestError is thrown.
|
|
with pytest.raises(litellm.BadRequestError) as excinfo:
|
|
await router.acompletion(model="completely-unknown-model", messages=messages)
|
|
|
|
# Check that the error message is correct.
|
|
# The router returns 'no healthy deployments' because get_model_list returns [] not None.
|
|
assert "no healthy deployments for this model" in str(excinfo.value)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_unknown_model_error_message_renders_model_name_literally():
|
|
"""
|
|
The unknown-model error message renders the caller-supplied model name
|
|
verbatim. A name containing Python format-field syntax must be treated as
|
|
literal text, not re-interpreted as a format template, which would distort
|
|
the message and balloon its length.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {"model": "azure/gpt-4o-real", "api_key": "fake-key"},
|
|
}
|
|
]
|
|
)
|
|
|
|
weird_model = "ghost{:>200}model"
|
|
messages = [{"role": "user", "content": "hi"}]
|
|
|
|
with pytest.raises(litellm.BadRequestError) as excinfo:
|
|
await router.acompletion(model=weird_model, messages=messages)
|
|
|
|
message = str(excinfo.value)
|
|
assert weird_model in message
|
|
assert " " not in message # no padding run from an expanded format field
|
|
|
|
|
|
def test_get_credential_deployment_is_the_deployment_credentials_resolve_to():
|
|
"""Regression: a batch retrieved with credentials resolved by model name was priced
|
|
without its deployment id, so per-deployment pricing never applied. The deployment
|
|
behind the credentials must be reachable by name and by id, carrying its model_info."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "mistral-ocr",
|
|
"litellm_params": {"model": "mistral/mistral-ocr-latest", "api_key": "sk-ocr"},
|
|
"model_info": {"id": "ocr-dep", "ocr_cost_per_page_batches": 0.0123},
|
|
}
|
|
]
|
|
)
|
|
|
|
by_name = router.get_credential_deployment(model_id="mistral-ocr")
|
|
by_id = router.get_credential_deployment(model_id="ocr-dep")
|
|
|
|
assert by_name is not None and by_id is not None
|
|
assert by_name.model_info.id == by_id.model_info.id == "ocr-dep"
|
|
assert by_name.model_info.model_dump()["ocr_cost_per_page_batches"] == 0.0123
|
|
assert router.get_deployment_credentials_with_provider(model_id="mistral-ocr")["api_key"] == "sk-ocr"
|
|
assert router.get_credential_deployment(model_id="no-such-model") is None
|
|
|
|
|
|
def test_get_credential_deployment_skips_a_paused_deployment():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "paused-ocr",
|
|
"litellm_params": {"model": "mistral/mistral-ocr-latest", "api_key": "sk-ocr"},
|
|
"model_info": {"id": "paused-dep", "blocked": True},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_credential_deployment(model_id="paused-ocr") is None
|
|
assert router.get_credential_deployment(model_id="paused-dep") is None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("team_id", "expected_id"),
|
|
[
|
|
pytest.param("team-a", "team-a-byok", id="team-key-uses-its-own-byok-wildcard"),
|
|
pytest.param("team-b", "shared-openrouter", id="team-without-byok-uses-the-shared-wildcard"),
|
|
pytest.param(None, "shared-openrouter", id="key-without-team-uses-the-shared-wildcard"),
|
|
],
|
|
)
|
|
def test_team_byok_wildcard_is_served_before_a_proxy_wide_wildcard_of_the_same_name(
|
|
team_id: str | None, expected_id: str
|
|
):
|
|
"""Regression: a proxy-wide openrouter/* shadowed a team's own openrouter/* BYOK deployment for that team's keys."""
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "model_name_team-a_internal",
|
|
"litellm_params": {"model": "openrouter/*", "api_key": "sk-team-a"},
|
|
"model_info": {"id": "team-a-byok", "team_id": "team-a", "team_public_model_name": "openrouter/*"},
|
|
},
|
|
{
|
|
"model_name": "openrouter/*",
|
|
"litellm_params": {"model": "openrouter/*", "api_key": "sk-shared"},
|
|
"model_info": {"id": "shared-openrouter"},
|
|
},
|
|
]
|
|
)
|
|
requested: Final = "openrouter/openai/gpt-5-nano"
|
|
|
|
served: Final = router.wildcard_route_deployments(requested, team_id)
|
|
|
|
assert tuple(Deployment.model_validate(d).model_info.id for d in served) == (expected_id,)
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_aws_bedrock_runtime_endpoint():
|
|
"""
|
|
Test that get_deployment_credentials_with_provider correctly copies
|
|
aws_bedrock_runtime_endpoint from deployment litellm_params to credentials.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-claude-model",
|
|
"litellm_params": {
|
|
"model": "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
"aws_access_key_id": "test-access-key",
|
|
"aws_secret_access_key": "test-secret-key",
|
|
"aws_region_name": "us-east-1",
|
|
"aws_bedrock_runtime_endpoint": "https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="bedrock-claude-model")
|
|
|
|
assert credentials is not None
|
|
assert credentials["aws_bedrock_runtime_endpoint"] == "https://bedrock-runtime.us-east-1.amazonaws.com"
|
|
assert credentials["aws_access_key_id"] == "test-access-key"
|
|
assert credentials["aws_secret_access_key"] == "test-secret-key"
|
|
assert credentials["aws_region_name"] == "us-east-1"
|
|
assert credentials["custom_llm_provider"] == "bedrock"
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_includes_bucket_name():
|
|
"""
|
|
Regression: bucket_name must survive the CredentialLiteLLMParams filter so
|
|
managed-files batch retrieval can resolve the GCS/S3 bucket. Previously it was
|
|
dropped, causing "GCS bucket_name is required" when fetching batch output files.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "vertex-gemini",
|
|
"litellm_params": {
|
|
"model": "vertex_ai/gemini-3.5-flash",
|
|
"vertex_project": "my-project",
|
|
"vertex_location": "global",
|
|
"gcs_bucket_name": "my-batch-bucket",
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="vertex-gemini")
|
|
|
|
assert credentials is not None
|
|
assert credentials["gcs_bucket_name"] == "my-batch-bucket"
|
|
assert credentials["vertex_project"] == "my-project"
|
|
assert credentials["custom_llm_provider"] == "vertex_ai"
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_keeps_legacy_bucket_name():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "vertex-gemini",
|
|
"litellm_params": {
|
|
"model": "vertex_ai/gemini-3.5-flash",
|
|
"vertex_project": "my-project",
|
|
"vertex_location": "global",
|
|
"bucket_name": "my-legacy-bucket",
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="vertex-gemini")
|
|
|
|
assert credentials is not None
|
|
assert credentials["bucket_name"] == "my-legacy-bucket"
|
|
assert "gcs_bucket_name" not in credentials
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_keeps_both_bucket_keys():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "vertex-gemini",
|
|
"litellm_params": {
|
|
"model": "vertex_ai/gemini-3.5-flash",
|
|
"vertex_project": "my-project",
|
|
"vertex_location": "global",
|
|
"gcs_bucket_name": "new-bucket",
|
|
"bucket_name": "legacy-bucket",
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="vertex-gemini")
|
|
|
|
assert credentials is not None
|
|
assert credentials["gcs_bucket_name"] == "new-bucket"
|
|
assert credentials["bucket_name"] == "legacy-bucket"
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_resolves_credential_name():
|
|
"""
|
|
Test that get_deployment_credentials_with_provider correctly resolves
|
|
litellm_credential_name to actual credential values (for UI-created models).
|
|
"""
|
|
from litellm.types.utils import CredentialItem
|
|
|
|
# Setup credential list with a test credential
|
|
litellm.credential_list = [
|
|
CredentialItem(
|
|
credential_name="test-azure-cred",
|
|
credential_info={"custom_llm_provider": "azure"},
|
|
credential_values={
|
|
"api_key": "resolved-api-key",
|
|
"api_base": "https://resolved.openai.azure.com",
|
|
"api_version": "2024-02-01",
|
|
},
|
|
)
|
|
]
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "azure-gpt-4",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4",
|
|
"litellm_credential_name": "test-azure-cred",
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="azure-gpt-4")
|
|
|
|
assert credentials is not None
|
|
assert credentials["api_key"] == "resolved-api-key"
|
|
assert credentials["api_base"] == "https://resolved.openai.azure.com"
|
|
assert credentials["api_version"] == "2024-02-01"
|
|
assert credentials["custom_llm_provider"] == "azure"
|
|
# Ensure credential name is removed after resolution
|
|
assert "litellm_credential_name" not in credentials
|
|
|
|
# Cleanup
|
|
litellm.credential_list = []
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_bedrock_batch_fields():
|
|
"""
|
|
Test that get_deployment_credentials_with_provider returns the deployment's
|
|
model and the Bedrock batch/S3 fields (s3_region_name, s3_encryption_key_id,
|
|
aws_batch_role_arn) instead of silently dropping them (#25104).
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-batch-model",
|
|
"litellm_params": {
|
|
"model": "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
"aws_region_name": "us-west-2",
|
|
"s3_bucket_name": "my-batch-bucket",
|
|
"s3_region_name": "us-east-1",
|
|
"s3_encryption_key_id": "arn:aws:kms:us-west-2:123:key/abc",
|
|
"s3_bucket_owner": "111111111111",
|
|
"aws_batch_role_arn": "arn:aws:iam::123:role/batch-role",
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="bedrock-batch-model")
|
|
|
|
assert credentials is not None
|
|
assert credentials["custom_llm_provider"] == "bedrock"
|
|
assert credentials["model"] == "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0"
|
|
assert credentials["aws_region_name"] == "us-west-2"
|
|
assert credentials["s3_bucket_name"] == "my-batch-bucket"
|
|
assert credentials["s3_region_name"] == "us-east-1"
|
|
assert credentials["s3_encryption_key_id"] == "arn:aws:kms:us-west-2:123:key/abc"
|
|
assert credentials["s3_bucket_owner"] == "111111111111"
|
|
assert credentials["aws_batch_role_arn"] == "arn:aws:iam::123:role/batch-role"
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_preserves_aws_auth_params():
|
|
"""
|
|
Test that get_deployment_credentials_with_provider preserves every AWS auth
|
|
selector (session token, assume-role, web identity, profile) so bedrock
|
|
files/batches deployments using temporary or role-based credentials do not
|
|
silently fall back to the server's ambient identity (#36155).
|
|
"""
|
|
aws_auth_params = {
|
|
"aws_access_key_id": "deployment-access-key",
|
|
"aws_secret_access_key": "deployment-secret",
|
|
"aws_session_token": "deployment-session-token",
|
|
"aws_region_name": "us-west-2",
|
|
"aws_session_name": "deployment-session",
|
|
"aws_profile_name": "deployment-profile",
|
|
"aws_role_name": "arn:aws:iam::123:role/deployment-role",
|
|
"aws_web_identity_token": "deployment-web-identity",
|
|
"aws_sts_endpoint": "https://sts.us-west-2.amazonaws.com",
|
|
"aws_external_id": "deployment-external-id",
|
|
}
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-batch-model",
|
|
"litellm_params": {
|
|
"model": "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",
|
|
**aws_auth_params,
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="bedrock-batch-model")
|
|
|
|
assert credentials is not None
|
|
for key, value in aws_auth_params.items():
|
|
assert credentials.get(key) == value, key
|
|
|
|
|
|
def test_get_deployment_credentials_preserves_azure_entra_id_params():
|
|
entra_params = {
|
|
"tenant_id": "deployment-tenant",
|
|
"client_id": "deployment-client",
|
|
"client_secret": "deployment-client-secret",
|
|
"azure_scope": "https://cognitiveservices.azure.us/.default",
|
|
"azure_username": "deployment-user",
|
|
"azure_password": "deployment-password",
|
|
}
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "azure-entra-model",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-5.4",
|
|
"api_base": "https://example.openai.azure.com/",
|
|
"api_version": "2024-10-21",
|
|
**entra_params,
|
|
},
|
|
"model_info": {"id": "azure-entra-model-id"},
|
|
}
|
|
],
|
|
)
|
|
|
|
credentials = router.get_deployment_credentials(model_id="azure-entra-model-id")
|
|
credentials_with_provider = router.get_deployment_credentials_with_provider(model_id="azure-entra-model-id")
|
|
|
|
assert credentials is not None
|
|
assert credentials_with_provider is not None
|
|
assert "api_key" not in credentials
|
|
for key, value in entra_params.items():
|
|
assert credentials.get(key) == value, key
|
|
assert credentials_with_provider.get(key) == value, key
|
|
|
|
|
|
def _team_wildcard_model(api_key: str, model_id: str = "team-wildcard-id") -> dict:
|
|
return {
|
|
"model_name": f"model_name_team-1_{model_id}",
|
|
"litellm_params": {"model": "openai/*", "api_key": api_key},
|
|
"model_info": {
|
|
"id": model_id,
|
|
"team_id": "team-1",
|
|
"team_public_model_name": "openai/*",
|
|
},
|
|
}
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_team_wildcard_priority():
|
|
"""
|
|
Regression: a global wildcard pattern (e.g. "openai/*") must not shadow a
|
|
team's own wildcard entry. When team_id is provided, the team wildcard
|
|
deployment's credentials win; without team_id the global one is used.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {"model": "openai/*", "api_key": "global-key"},
|
|
},
|
|
_team_wildcard_model(api_key="team-key"),
|
|
],
|
|
)
|
|
|
|
team_credentials = router.get_deployment_credentials_with_provider(model_id="openai/gpt-5.2", team_id="team-1")
|
|
assert team_credentials is not None
|
|
assert team_credentials["api_key"] == "team-key"
|
|
|
|
global_credentials = router.get_deployment_credentials_with_provider(model_id="openai/gpt-5.2")
|
|
assert global_credentials is not None
|
|
assert global_credentials["api_key"] == "global-key"
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_skips_other_team_deployment():
|
|
"""
|
|
Regression: a team-scoped deployment sharing a model_name with a global
|
|
deployment must never resolve for another team's (or an unscoped) caller,
|
|
even when it is indexed first; the shared global deployment wins instead.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini-2.5-pro",
|
|
"litellm_params": {
|
|
"model": "vertex_ai/gemini-2.5-pro",
|
|
"vertex_project": "team-b-project",
|
|
},
|
|
"model_info": {
|
|
"id": "team-b-vertex",
|
|
"team_id": "team-b",
|
|
"team_public_model_name": "gemini-2.5-pro",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "gemini-2.5-pro",
|
|
"litellm_params": {
|
|
"model": "vertex_ai/gemini-2.5-pro",
|
|
"vertex_project": "shared-project",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
other_team_credentials = router.get_deployment_credentials_with_provider(
|
|
model_id="gemini-2.5-pro", team_id="team-a"
|
|
)
|
|
assert other_team_credentials is not None
|
|
assert other_team_credentials["vertex_project"] == "shared-project"
|
|
|
|
unscoped_credentials = router.get_deployment_credentials_with_provider(model_id="gemini-2.5-pro")
|
|
assert unscoped_credentials is not None
|
|
assert unscoped_credentials["vertex_project"] == "shared-project"
|
|
|
|
owner_credentials = router.get_deployment_credentials_with_provider(model_id="gemini-2.5-pro", team_id="team-b")
|
|
assert owner_credentials is not None
|
|
assert owner_credentials["vertex_project"] == "team-b-project"
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_no_fallback_to_other_team_only_name():
|
|
"""
|
|
When the only deployments under a model name belong to another team, other
|
|
callers must get None (env fallback) instead of that team's credentials.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini-2.5-pro",
|
|
"litellm_params": {
|
|
"model": "vertex_ai/gemini-2.5-pro",
|
|
"vertex_project": "team-b-project",
|
|
},
|
|
"model_info": {
|
|
"id": "team-b-vertex",
|
|
"team_id": "team-b",
|
|
"team_public_model_name": "gemini-2.5-pro",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
assert router.get_deployment_credentials_with_provider(model_id="gemini-2.5-pro", team_id="team-a") is None
|
|
assert router.get_deployment_credentials_with_provider(model_id="gemini-2.5-pro") is None
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_skips_other_team_wildcard():
|
|
"""
|
|
Global wildcard resolution must skip a team-scoped wildcard deployment for
|
|
callers outside that team, falling through to the shared wildcard entry.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {"model": "openai/*", "api_key": "team-b-key"},
|
|
"model_info": {
|
|
"id": "team-b-wildcard",
|
|
"team_id": "team-b",
|
|
"team_public_model_name": "openai/*",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {"model": "openai/*", "api_key": "global-key"},
|
|
},
|
|
],
|
|
)
|
|
|
|
other_team_credentials = router.get_deployment_credentials_with_provider(
|
|
model_id="openai/gpt-5.2", team_id="team-a"
|
|
)
|
|
assert other_team_credentials is not None
|
|
assert other_team_credentials["api_key"] == "global-key"
|
|
|
|
owner_credentials = router.get_deployment_credentials_with_provider(model_id="openai/gpt-5.2", team_id="team-b")
|
|
assert owner_credentials is not None
|
|
assert owner_credentials["api_key"] == "team-b-key"
|
|
|
|
|
|
def test_team_wildcard_credentials_not_usable_after_delete_deployment():
|
|
"""
|
|
Regression: team_pattern_routers retained deleted deployments, so a team
|
|
user could keep resolving credentials of a deleted wildcard deployment.
|
|
"""
|
|
router = litellm.Router(model_list=[_team_wildcard_model(api_key="old-key")])
|
|
|
|
assert router.get_deployment_credentials_with_provider(model_id="openai/gpt-5.2", team_id="team-1") is not None
|
|
|
|
router.delete_deployment(id="team-wildcard-id")
|
|
|
|
assert router.get_deployment_credentials_with_provider(model_id="openai/gpt-5.2", team_id="team-1") is None
|
|
|
|
|
|
def test_global_wildcard_pattern_router_evicts_stale_entry_on_upsert_and_delete():
|
|
"""
|
|
Regression for #29064: upsert_deployment removed the old deployment from
|
|
model_list but left it in the global pattern_router, so wildcard requests
|
|
round-robined between the stale and the corrected deployment.
|
|
"""
|
|
from litellm.types.router import Deployment, LiteLLM_Params
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {"model": "openai/openai/*", "api_key": "sk-old"},
|
|
"model_info": {"id": "global-wildcard"},
|
|
}
|
|
]
|
|
)
|
|
|
|
router.upsert_deployment(
|
|
Deployment(
|
|
model_name="openai/*",
|
|
litellm_params=LiteLLM_Params(model="openai/*", api_key="sk-new"),
|
|
model_info={"id": "global-wildcard"},
|
|
)
|
|
)
|
|
|
|
matches = router.pattern_router.route("openai/gpt-5.2")
|
|
assert matches is not None
|
|
assert [m["litellm_params"]["api_key"] for m in matches] == ["sk-new"]
|
|
|
|
router.delete_deployment(id="global-wildcard")
|
|
assert router.pattern_router.patterns == {}
|
|
|
|
|
|
def test_pattern_match_router_remove_deployment():
|
|
"""
|
|
remove_deployment must drop only the deployment with the given model id and
|
|
delete patterns whose deployment list becomes empty.
|
|
"""
|
|
from litellm.litellm_core_utils.pattern_match_deployments import PatternMatchRouter
|
|
|
|
pattern_router = PatternMatchRouter()
|
|
pattern_router.add_pattern(
|
|
"openai/*",
|
|
{"litellm_params": {"model": "openai/*", "api_key": "key-a"}, "model_info": {"id": "dep-a"}},
|
|
)
|
|
pattern_router.add_pattern(
|
|
"openai/*",
|
|
{"litellm_params": {"model": "openai/*", "api_key": "key-b"}, "model_info": {"id": "dep-b"}},
|
|
)
|
|
|
|
pattern_router.remove_deployment(model_id="dep-a")
|
|
matches = pattern_router.route("openai/gpt-5.2")
|
|
assert matches is not None
|
|
assert [m["model_info"]["id"] for m in matches] == ["dep-b"]
|
|
|
|
pattern_router.remove_deployment(model_id="dep-b")
|
|
assert pattern_router.patterns == {}
|
|
assert pattern_router.route("openai/gpt-5.2") is None
|
|
|
|
|
|
def test_team_wildcard_credentials_refreshed_on_upsert_and_set_model_list():
|
|
"""
|
|
Regression: replacing a team wildcard deployment (upsert or model list
|
|
reload) must serve the new credentials, not the stale cached ones.
|
|
"""
|
|
from litellm.types.router import Deployment
|
|
|
|
router = litellm.Router(model_list=[_team_wildcard_model(api_key="old-key")])
|
|
|
|
router.upsert_deployment(deployment=Deployment(**_team_wildcard_model(api_key="new-key")))
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="openai/gpt-5.2", team_id="team-1")
|
|
assert credentials is not None
|
|
assert credentials["api_key"] == "new-key"
|
|
|
|
router.set_model_list(model_list=[])
|
|
assert router.get_deployment_credentials_with_provider(model_id="openai/gpt-5.2", team_id="team-1") is None
|
|
|
|
|
|
def test_get_available_guardrail_single_deployment():
|
|
"""
|
|
Test get_available_guardrail returns the single guardrail when only one exists.
|
|
"""
|
|
guardrail_config = {
|
|
"guardrail_name": "content-filter",
|
|
"litellm_params": {"guardrail": "custom", "mode": "pre_call"},
|
|
"id": "guardrail-1",
|
|
}
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
}
|
|
],
|
|
guardrail_list=[guardrail_config],
|
|
)
|
|
|
|
result = router.get_available_guardrail(guardrail_name="content-filter")
|
|
assert result == guardrail_config
|
|
|
|
|
|
def test_get_available_guardrail_multiple_deployments():
|
|
"""
|
|
Test get_available_guardrail load balances across multiple guardrails.
|
|
"""
|
|
guardrail_1 = {
|
|
"guardrail_name": "content-filter",
|
|
"litellm_params": {"guardrail": "custom", "mode": "pre_call"},
|
|
"id": "guardrail-1",
|
|
}
|
|
guardrail_2 = {
|
|
"guardrail_name": "content-filter",
|
|
"litellm_params": {"guardrail": "custom", "mode": "pre_call"},
|
|
"id": "guardrail-2",
|
|
}
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
}
|
|
],
|
|
guardrail_list=[guardrail_1, guardrail_2],
|
|
)
|
|
|
|
# Call multiple times to verify load balancing
|
|
results = set()
|
|
for _ in range(20):
|
|
result = router.get_available_guardrail(guardrail_name="content-filter")
|
|
results.add(result["id"])
|
|
|
|
# Both guardrails should be selected at least once
|
|
assert "guardrail-1" in results or "guardrail-2" in results
|
|
|
|
|
|
def test_get_available_guardrail_not_found():
|
|
"""
|
|
Test get_available_guardrail raises ValueError when guardrail not found.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
}
|
|
],
|
|
guardrail_list=[],
|
|
)
|
|
|
|
with pytest.raises(ValueError, match="No guardrail found with name"):
|
|
router.get_available_guardrail(guardrail_name="non-existent")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_aguardrail():
|
|
"""
|
|
Test aguardrail executes a guardrail with load balancing and fallbacks.
|
|
"""
|
|
guardrail_config = {
|
|
"guardrail_name": "content-filter",
|
|
"litellm_params": {"guardrail": "custom", "mode": "pre_call"},
|
|
"id": "guardrail-1",
|
|
}
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
}
|
|
],
|
|
guardrail_list=[guardrail_config],
|
|
)
|
|
|
|
# Mock the original function
|
|
async def mock_original_function(**kwargs):
|
|
return {
|
|
"result": "success",
|
|
"selected_guardrail": kwargs.get("selected_guardrail"),
|
|
}
|
|
|
|
result = await router.aguardrail(
|
|
guardrail_name="content-filter",
|
|
original_function=mock_original_function,
|
|
)
|
|
|
|
assert result["result"] == "success"
|
|
assert result["selected_guardrail"]["id"] == "guardrail-1"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_anthropic_messages_call_type_is_cached():
|
|
"""
|
|
Regression test: Verify that anthropic_messages call type is allowed
|
|
in PromptCachingDeploymentCheck.async_log_success_event.
|
|
"""
|
|
import asyncio
|
|
|
|
from litellm.caching.dual_cache import DualCache
|
|
from litellm.router_utils.pre_call_checks.prompt_caching_deployment_check import (
|
|
PromptCachingDeploymentCheck,
|
|
)
|
|
from litellm.types.utils import (
|
|
StandardLoggingHiddenParams,
|
|
StandardLoggingMetadata,
|
|
StandardLoggingModelInformation,
|
|
StandardLoggingPayload,
|
|
)
|
|
|
|
# Create mock standard logging payload inline
|
|
def create_standard_logging_payload() -> StandardLoggingPayload:
|
|
return StandardLoggingPayload(
|
|
id="test_id",
|
|
call_type="completion",
|
|
response_cost=0.1,
|
|
response_cost_failure_debug_info=None,
|
|
status="success",
|
|
total_tokens=30,
|
|
prompt_tokens=20,
|
|
completion_tokens=10,
|
|
startTime=1234567890.0,
|
|
endTime=1234567891.0,
|
|
completionStartTime=1234567890.5,
|
|
model_map_information=StandardLoggingModelInformation(model_map_key="gpt-3.5-turbo", model_map_value=None),
|
|
model="gpt-3.5-turbo",
|
|
custom_llm_provider="anthropic",
|
|
model_id="model-123",
|
|
model_group="openai-gpt",
|
|
api_base="https://api.openai.com",
|
|
metadata=StandardLoggingMetadata(
|
|
user_api_key_hash="test_hash",
|
|
user_api_key_org_id=None,
|
|
user_api_key_alias="test_alias",
|
|
user_api_key_team_id="test_team",
|
|
user_api_key_user_id="test_user",
|
|
user_api_key_team_alias="test_team_alias",
|
|
spend_logs_metadata=None,
|
|
requester_ip_address="127.0.0.1",
|
|
requester_metadata=None,
|
|
),
|
|
cache_hit=False,
|
|
cache_key=None,
|
|
saved_cache_cost=0.0,
|
|
request_tags=[],
|
|
end_user=None,
|
|
requester_ip_address="127.0.0.1",
|
|
messages=[{"role": "user", "content": "Hello, world!"}],
|
|
response={"choices": [{"message": {"content": "Hi there!"}}]},
|
|
error_str=None,
|
|
model_parameters={"stream": True},
|
|
hidden_params=StandardLoggingHiddenParams(
|
|
model_id="model-123",
|
|
cache_key=None,
|
|
api_base="https://api.openai.com",
|
|
response_cost="0.1",
|
|
additional_headers=None,
|
|
),
|
|
)
|
|
|
|
cache = DualCache()
|
|
deployment_check = PromptCachingDeploymentCheck(cache=cache)
|
|
prompt_cache = PromptCachingCache(cache=cache)
|
|
|
|
# Create messages with enough tokens to pass the caching threshold
|
|
test_messages = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{
|
|
"type": "text",
|
|
"text": "test long message here" * 1024,
|
|
"cache_control": {"type": "ephemeral", "ttl": "5m"},
|
|
}
|
|
],
|
|
}
|
|
]
|
|
test_model_id = "test-model-id-123"
|
|
|
|
# Create a payload with anthropic_messages call type
|
|
payload = create_standard_logging_payload()
|
|
payload["call_type"] = CallTypes.anthropic_messages.value
|
|
payload["messages"] = test_messages
|
|
payload["model"] = "anthropic/claude-3-5-sonnet-20240620"
|
|
payload["model_id"] = test_model_id
|
|
|
|
# Log the success event (should cache the model_id)
|
|
await deployment_check.async_log_success_event(
|
|
kwargs={"standard_logging_object": payload},
|
|
response_obj={},
|
|
start_time=1234567890.0,
|
|
end_time=1234567891.0,
|
|
)
|
|
|
|
# Small delay to ensure cache write completes
|
|
await asyncio.sleep(0.1)
|
|
|
|
# Verify that the model_id was actually cached
|
|
cached_result = await prompt_cache.async_get_model_id(
|
|
messages=test_messages,
|
|
tools=None,
|
|
)
|
|
|
|
# This assertion will FAIL if anthropic_messages is filtered out
|
|
assert cached_result is not None, "Model ID should be cached for anthropic_messages call type"
|
|
assert cached_result["model_id"] == test_model_id, f"Expected {test_model_id}, got {cached_result['model_id']}"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_retry_does_not_narrow_tag_filtered_group_to_failed_deployments_tags():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "tagged-group",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.5",
|
|
"api_key": "fake-key",
|
|
"tags": ["free"],
|
|
"input_cost_per_token": 0.000001,
|
|
"output_cost_per_token": 0.000001,
|
|
"mock_response": "litellm.ContextWindowExceededError",
|
|
},
|
|
"model_info": {"id": "tagged-failing"},
|
|
},
|
|
{
|
|
"model_name": "tagged-group",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.5",
|
|
"api_key": "fake-key",
|
|
"input_cost_per_token": 0.001,
|
|
"output_cost_per_token": 0.001,
|
|
"mock_response": "ok",
|
|
},
|
|
"model_info": {"id": "untagged-healthy"},
|
|
},
|
|
],
|
|
routing_strategy="cost-based-routing",
|
|
enable_tag_filtering=True,
|
|
num_retries=2,
|
|
retry_after=0,
|
|
retry_policy=RetryPolicy(BadRequestErrorRetries=2),
|
|
)
|
|
metadata: Final[dict[str, object]] = {}
|
|
|
|
response = await router.acompletion(
|
|
model="tagged-group", messages=[{"role": "user", "content": "hi"}], metadata=metadata
|
|
)
|
|
|
|
assert response._hidden_params["model_id"] == "untagged-healthy"
|
|
assert metadata["tags"] == ["free"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_team_scoped_model_fallback():
|
|
"""
|
|
Test that fallback works correctly for team-scoped models.
|
|
|
|
When a team-scoped model fails and the fallback model is also team-scoped,
|
|
the router should find the fallback deployment by matching team_public_model_name.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "team-a-primary-internal",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "api_key": "fake"},
|
|
"model_info": {
|
|
"team_id": "team-a",
|
|
"team_public_model_name": "primary-model",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "team-a-fallback-internal",
|
|
"litellm_params": {
|
|
"model": "gpt-4",
|
|
"api_key": "fake",
|
|
"mock_response": "fallback success from team-a",
|
|
},
|
|
"model_info": {
|
|
"team_id": "team-a",
|
|
"team_public_model_name": "fallback-model",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"primary-model": ["fallback-model"]}],
|
|
)
|
|
|
|
response = await router.acompletion(
|
|
model="primary-model",
|
|
messages=[{"role": "user", "content": "Hello"}],
|
|
metadata={"user_api_key_team_id": "team-a"},
|
|
mock_testing_fallbacks=True,
|
|
)
|
|
assert response is not None
|
|
assert response.choices[0].message.content == "fallback success from team-a"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_team_scoped_model_fallback_to_global():
|
|
"""
|
|
Test that a team-scoped model can fall back to a global (non-team) model.
|
|
|
|
Global models (no team_id on deployment) should be accessible as fallback
|
|
targets for team-scoped requests.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "team-a-primary-internal",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "api_key": "fake"},
|
|
"model_info": {
|
|
"team_id": "team-a",
|
|
"team_public_model_name": "primary-model",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "global-fallback",
|
|
"litellm_params": {
|
|
"model": "gpt-4",
|
|
"api_key": "fake",
|
|
"mock_response": "global fallback success",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"primary-model": ["global-fallback"]}],
|
|
)
|
|
|
|
response = await router.acompletion(
|
|
model="primary-model",
|
|
messages=[{"role": "user", "content": "Hello"}],
|
|
metadata={"user_api_key_team_id": "team-a"},
|
|
mock_testing_fallbacks=True,
|
|
)
|
|
assert response is not None
|
|
assert response.choices[0].message.content == "global fallback success"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_team_scoped_model_fallback_cross_team_blocked():
|
|
"""
|
|
Test that cross-team fallback is correctly blocked.
|
|
|
|
When team-a's model fails and the fallback target is scoped to team-b,
|
|
the router should NOT use it (team isolation).
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "team-a-primary-internal",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "api_key": "fake"},
|
|
"model_info": {
|
|
"team_id": "team-a",
|
|
"team_public_model_name": "primary-model",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "team-b-fallback-internal",
|
|
"litellm_params": {
|
|
"model": "gpt-4",
|
|
"api_key": "fake",
|
|
"mock_response": "team-b response - should not reach here",
|
|
},
|
|
"model_info": {
|
|
"team_id": "team-b",
|
|
"team_public_model_name": "fallback-model",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"primary-model": ["fallback-model"]}],
|
|
)
|
|
|
|
with pytest.raises(litellm.InternalServerError):
|
|
await router.acompletion(
|
|
model="primary-model",
|
|
messages=[{"role": "user", "content": "Hello"}],
|
|
metadata={"user_api_key_team_id": "team-a"},
|
|
mock_testing_fallbacks=True,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
def _shared_ptu_deployment(model_name: str, deployment_id: str, **litellm_params: str) -> dict[str, object]:
|
|
"""A one-PTU Azure deployment whose whole reservation is team-a's share."""
|
|
return {
|
|
"model_name": model_name,
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1",
|
|
"api_key": "fake",
|
|
"api_base": "https://ptu.example",
|
|
**litellm_params,
|
|
},
|
|
"model_info": {
|
|
"id": deployment_id,
|
|
"base_model": "azure/gpt-4.1",
|
|
"ptu_count": 1,
|
|
"cost_per_ptu_per_hour": 1.0,
|
|
"ptu_effective_from": "2026-01-01T00:00:00Z",
|
|
"ptu_shares": {"team-a": 1},
|
|
},
|
|
}
|
|
|
|
|
|
async def test_a_ptu_share_holders_failed_call_is_retried_on_its_shared_deployment(monkeypatch):
|
|
"""A shared PTU deployment is only servable to the teams holding a share, so the retry loop
|
|
has to look it up as the calling team; looked up with no team it finds no healthy deployment
|
|
and gives up after the first failure."""
|
|
monkeypatch.setenv("LITELLM_ENABLE_PTU_COST_ATTRIBUTION", "true")
|
|
router = litellm.Router(
|
|
model_list=[_shared_ptu_deployment("gpt-4.1-ptu", "shared-ptu", mock_response="litellm.InternalServerError")],
|
|
num_retries=2,
|
|
retry_after=0,
|
|
)
|
|
|
|
with pytest.raises(litellm.InternalServerError) as failed:
|
|
await router.acompletion(
|
|
model="gpt-4.1-ptu",
|
|
messages=[{"role": "user", "content": "Hello"}],
|
|
metadata={"user_api_key_team_id": "team-a"},
|
|
)
|
|
|
|
assert failed.value.num_retries == 2
|
|
|
|
|
|
def test_get_all_deployments_with_team_id():
|
|
"""
|
|
Test that _get_all_deployments with team_id can find deployments
|
|
by team_public_model_name when the model_name is not in the index.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "internal-team-deployment",
|
|
"litellm_params": {"model": "gpt-4", "api_key": "fake"},
|
|
"model_info": {
|
|
"team_id": "team-x",
|
|
"team_public_model_name": "gpt-4",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
# Without team_id: "gpt-4" is not in the model_name index (internal name is different)
|
|
deployments = router._get_all_deployments(model_name="gpt-4")
|
|
assert len(deployments) == 0
|
|
|
|
# With correct team_id: should find via O(n) scan matching team_public_model_name
|
|
deployments = router._get_all_deployments(model_name="gpt-4", team_id="team-x")
|
|
assert len(deployments) == 1
|
|
assert deployments[0]["model_name"] == "internal-team-deployment"
|
|
|
|
# With wrong team_id: should find nothing
|
|
deployments = router._get_all_deployments(model_name="gpt-4", team_id="team-y")
|
|
assert len(deployments) == 0
|
|
|
|
|
|
def test_multiregion_team_deployments_unique_model_names():
|
|
"""
|
|
Simulates athenahealth's exact setup: unique model_names per deployment,
|
|
same team_public_model_name, multiple regions.
|
|
|
|
Verifies that _get_all_deployments returns ALL regional deployments
|
|
for a team when queried by team_public_model_name.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "metis-claude-us-east-1",
|
|
"litellm_params": {
|
|
"model": "bedrock/anthropic.claude-3-sonnet",
|
|
"aws_region_name": "us-east-1",
|
|
"api_key": "fake",
|
|
},
|
|
"model_info": {
|
|
"team_id": "metis-team",
|
|
"team_public_model_name": "claude-sonnet",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "metis-claude-us-west-2",
|
|
"litellm_params": {
|
|
"model": "bedrock/anthropic.claude-3-sonnet",
|
|
"aws_region_name": "us-west-2",
|
|
"api_key": "fake",
|
|
},
|
|
"model_info": {
|
|
"team_id": "metis-team",
|
|
"team_public_model_name": "claude-sonnet",
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
# "claude-sonnet" is NOT in the model_name index
|
|
assert "claude-sonnet" not in router.model_names
|
|
|
|
# Without team_id: returns nothing (no model_name="claude-sonnet" in index, no O(n) scan)
|
|
deployments = router._get_all_deployments(model_name="claude-sonnet")
|
|
assert len(deployments) == 0
|
|
|
|
# With team_id: O(n) scan finds BOTH regional deployments
|
|
deployments = router._get_all_deployments(model_name="claude-sonnet", team_id="metis-team")
|
|
assert len(deployments) == 2
|
|
deployment_names = {d["model_name"] for d in deployments}
|
|
assert deployment_names == {"metis-claude-us-east-1", "metis-claude-us-west-2"}
|
|
|
|
# Each deployment has a unique ID (critical for cooldown/retry to work)
|
|
deployment_ids = {d["model_info"]["id"] for d in deployments}
|
|
assert len(deployment_ids) == 2, "Each deployment must have a unique ID for cooldown tracking"
|
|
|
|
# Wrong team: returns nothing
|
|
deployments = router._get_all_deployments(model_name="claude-sonnet", team_id="other-team")
|
|
assert len(deployments) == 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_multiregion_team_failover_between_regions():
|
|
"""
|
|
Simulates athenahealth's multiregion failover scenario:
|
|
- Two Bedrock deployments (us-east-1 and us-west-2) with unique model_names
|
|
- Same team_public_model_name ("claude-sonnet")
|
|
- Primary region fails → router should failover to second region
|
|
|
|
This is the exact scenario Sean Glover from athenahealth will demonstrate.
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "metis-claude-us-east-1",
|
|
"litellm_params": {
|
|
"model": "bedrock/anthropic.claude-3-sonnet",
|
|
"api_key": "fake",
|
|
"mock_response": "response from us-east-1",
|
|
},
|
|
"model_info": {
|
|
"team_id": "metis-team",
|
|
"team_public_model_name": "claude-sonnet",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "metis-claude-us-west-2",
|
|
"litellm_params": {
|
|
"model": "bedrock/anthropic.claude-3-sonnet",
|
|
"api_key": "fake",
|
|
"mock_response": "response from us-west-2",
|
|
},
|
|
"model_info": {
|
|
"team_id": "metis-team",
|
|
"team_public_model_name": "claude-sonnet",
|
|
},
|
|
},
|
|
],
|
|
num_retries=1,
|
|
)
|
|
|
|
# Verify the router finds both deployments for the team
|
|
deployments = router._get_all_deployments(model_name="claude-sonnet", team_id="metis-team")
|
|
assert len(deployments) == 2, "Router must find both regional deployments by team_public_model_name"
|
|
|
|
# Make a normal request — should succeed from one of the regions
|
|
response = await router.acompletion(
|
|
model="claude-sonnet",
|
|
messages=[{"role": "user", "content": "Hello"}],
|
|
metadata={"user_api_key_team_id": "metis-team"},
|
|
)
|
|
assert response is not None
|
|
assert response.choices[0].message.content in [
|
|
"response from us-east-1",
|
|
"response from us-west-2",
|
|
]
|
|
|
|
|
|
def _router_with_two_deployments(blocked_flags):
|
|
import litellm
|
|
|
|
model_list = []
|
|
for idx, blocked in enumerate(blocked_flags):
|
|
model_list.append(
|
|
{
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {"model": f"openai/gpt-4o-{idx}"},
|
|
"model_info": {"id": f"dep-{idx}", "blocked": blocked},
|
|
}
|
|
)
|
|
return litellm.Router(model_list=model_list)
|
|
|
|
|
|
def test_get_fully_blocked_model_names_marks_name_when_all_deployments_blocked():
|
|
router = _router_with_two_deployments([True, True])
|
|
assert router.get_fully_blocked_model_names() == {"gpt-4o"}
|
|
|
|
|
|
def test_get_fully_blocked_model_names_keeps_name_when_partial_blocked():
|
|
router = _router_with_two_deployments([True, False])
|
|
assert router.get_fully_blocked_model_names() == set()
|
|
|
|
|
|
def test_get_fully_blocked_model_names_treats_missing_key_as_unblocked():
|
|
import litellm
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {"model": "openai/gpt-4o"},
|
|
"model_info": {"id": "dep-0"},
|
|
}
|
|
]
|
|
)
|
|
assert router.get_fully_blocked_model_names() == set()
|
|
|
|
|
|
def _seed_unhealthy_states(router, unhealthy_ids, timestamp=None):
|
|
import time
|
|
|
|
ts = timestamp if timestamp is not None else time.time()
|
|
router.health_state_cache.set_deployment_health_states(
|
|
{uid: {"is_healthy": False, "timestamp": ts, "reason": "test_unhealthy"} for uid in unhealthy_ids}
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_get_fully_unhealthy_model_names_marks_name_when_all_unhealthy():
|
|
router = _router_with_two_deployments([False, False])
|
|
_seed_unhealthy_states(router, {"dep-0", "dep-1"})
|
|
assert await router.async_get_fully_unhealthy_model_names() == {"gpt-4o"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("health_check_probe", [False, True])
|
|
@pytest.mark.parametrize(
|
|
"state, health_routing, fails_policy, scoped, strict_ids",
|
|
[
|
|
("absent", True, False, False, ("dep-0", "dep-1")),
|
|
("partial", True, False, False, ("dep-1",)),
|
|
("all", True, False, False, ()),
|
|
("stale", True, False, False, ("dep-0", "dep-1")),
|
|
("all", False, False, False, ("dep-0", "dep-1")),
|
|
("all", True, True, False, ("dep-0", "dep-1")),
|
|
("all", True, True, True, ()),
|
|
],
|
|
)
|
|
async def test_health_probe_preserves_normal_caller_policy(
|
|
health_check_probe: bool,
|
|
state: str,
|
|
health_routing: bool,
|
|
fails_policy: bool,
|
|
scoped: bool,
|
|
strict_ids: tuple[str, ...],
|
|
) -> None:
|
|
import time
|
|
from litellm.types.router import AllowedFailsPolicy, RouterRateLimitError
|
|
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "health-group",
|
|
"litellm_params": {"model": "openai/gpt-5.6", "api_key": "test-only"},
|
|
"model_info": {"id": model_id},
|
|
}
|
|
for model_id in ("dep-0", "dep-1")
|
|
],
|
|
enable_health_check_routing=health_routing,
|
|
allowed_fails_policy=AllowedFailsPolicy(ServiceUnavailableErrorAllowedFails=2) if fails_policy else None,
|
|
background_health_check_model_groups=["health-group"] if scoped else None,
|
|
)
|
|
if state != "absent":
|
|
_seed_unhealthy_states(
|
|
router,
|
|
("dep-0",) if state == "partial" else ("dep-0", "dep-1"),
|
|
time.time() - router.health_state_cache.staleness_threshold - 10 if state == "stale" else None,
|
|
)
|
|
expected: Final = strict_ids if strict_ids or health_check_probe else ("dep-0", "dep-1")
|
|
if not expected:
|
|
with pytest.raises(RouterRateLimitError, match="No deployments available"):
|
|
await router.async_get_healthy_deployments(model="health-group", request_kwargs={}, health_check_probe=True)
|
|
else:
|
|
deployments: Final = await router.async_get_healthy_deployments(
|
|
model="health-group", request_kwargs={}, health_check_probe=health_check_probe
|
|
)
|
|
assert {d["model_info"]["id"] for d in deployments} == set(expected)
|
|
assert await router.cooldown_cache.async_get_active_cooldowns(["dep-0", "dep-1"], parent_otel_span=None) == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_get_fully_unhealthy_model_names_keeps_name_when_partial():
|
|
router = _router_with_two_deployments([False, False])
|
|
_seed_unhealthy_states(router, {"dep-0"})
|
|
assert await router.async_get_fully_unhealthy_model_names() == set()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_get_fully_unhealthy_model_names_empty_without_health_state():
|
|
router = _router_with_two_deployments([False, False])
|
|
assert await router.async_get_fully_unhealthy_model_names() == set()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_get_fully_unhealthy_model_names_ignores_stale_state():
|
|
import time
|
|
|
|
router = _router_with_two_deployments([False, False])
|
|
stale_ts = time.time() - (router.health_state_cache.staleness_threshold + 10)
|
|
_seed_unhealthy_states(router, {"dep-0", "dep-1"}, timestamp=stale_ts)
|
|
assert await router.async_get_fully_unhealthy_model_names() == set()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_get_fully_unhealthy_model_names_includes_team_alias():
|
|
import litellm
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {"model": "openai/gpt-4o"},
|
|
"model_info": {
|
|
"id": "dep-0",
|
|
"team_id": "team-1",
|
|
"team_public_model_name": "team-gpt",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
_seed_unhealthy_states(router, {"dep-0"})
|
|
assert await router.async_get_fully_unhealthy_model_names() == {
|
|
"gpt-4o",
|
|
"team-gpt",
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_get_fully_unhealthy_model_names_noop_with_allowed_fails_policy():
|
|
from litellm.types.router import AllowedFailsPolicy
|
|
|
|
router = _router_with_two_deployments([False, False])
|
|
router.allowed_fails_policy = AllowedFailsPolicy(BadRequestErrorAllowedFails=1)
|
|
_seed_unhealthy_states(router, {"dep-0", "dep-1"})
|
|
assert await router.async_get_fully_unhealthy_model_names() == set()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_public_async_get_healthy_deployments_skips_blocked_on_primary_path():
|
|
router = _router_with_two_deployments([True, False])
|
|
deployments = await router.async_get_healthy_deployments(model="gpt-4o", request_kwargs={})
|
|
assert isinstance(deployments, list)
|
|
ids = [d["model_info"]["id"] for d in deployments]
|
|
assert "dep-0" not in ids
|
|
assert "dep-1" in ids
|
|
|
|
|
|
def test_public_get_available_deployment_skips_blocked_on_primary_path():
|
|
router = _router_with_two_deployments([True, False])
|
|
deployment = router.get_available_deployment(model="gpt-4o", request_kwargs={})
|
|
assert deployment["model_info"]["id"] == "dep-1"
|
|
|
|
|
|
def test_get_available_deployment_raises_when_addressed_dict_is_blocked():
|
|
import litellm
|
|
|
|
router = _router_with_two_deployments([True, True])
|
|
with pytest.raises(litellm.ServiceUnavailableError):
|
|
router.get_available_deployment(model="dep-0", request_kwargs={})
|
|
|
|
|
|
def _cool_down(router: Router, *deployment_ids: str) -> None:
|
|
for deployment_id in deployment_ids:
|
|
router.cooldown_cache.add_deployment_to_cooldown(
|
|
model_id=deployment_id,
|
|
original_exception=litellm.RateLimitError(message="upstream 429", llm_provider="openai", model="gpt-4o"),
|
|
exception_status=429,
|
|
cooldown_time=60,
|
|
)
|
|
|
|
|
|
async def _select_deployment(router: Router, use_async: bool) -> None:
|
|
if use_async:
|
|
await router.async_get_available_deployment(model="gpt-4o", request_kwargs={})
|
|
return
|
|
router.get_available_deployment(model="gpt-4o", request_kwargs={})
|
|
|
|
|
|
@pytest.mark.parametrize("use_async", [False, True], ids=["sync", "async"])
|
|
@pytest.mark.asyncio
|
|
async def test_get_available_deployment_names_cooldown_when_every_deployment_is_cooled_down(use_async: bool):
|
|
from litellm.types.router import RouterErrors, RouterRateLimitError
|
|
|
|
router: Final = _router_with_two_deployments([False, False])
|
|
_cool_down(router, "dep-0", "dep-1")
|
|
with pytest.raises(RouterRateLimitError) as exc_info:
|
|
await _select_deployment(router, use_async)
|
|
assert exc_info.value.all_deployments_in_cooldown is True
|
|
assert exc_info.value.type == "all_deployments_in_cooldown"
|
|
assert RouterErrors.all_deployments_in_cooldown.value in str(exc_info.value)
|
|
assert str(exc_info.value).startswith("No deployments available for selected model, Try again in ")
|
|
|
|
|
|
@pytest.mark.parametrize("use_async", [False, True], ids=["sync", "async"])
|
|
@pytest.mark.asyncio
|
|
async def test_get_available_deployment_keeps_generic_error_when_cooldown_is_partial(use_async: bool):
|
|
from litellm.types.router import RouterErrors, RouterRateLimitError
|
|
|
|
router: Final = _router_with_two_deployments([False, True])
|
|
_cool_down(router, "dep-0")
|
|
with pytest.raises(RouterRateLimitError) as exc_info:
|
|
await _select_deployment(router, use_async)
|
|
assert exc_info.value.all_deployments_in_cooldown is False
|
|
assert exc_info.value.type == "rate_limit_error"
|
|
assert RouterErrors.all_deployments_in_cooldown.value not in str(exc_info.value)
|
|
|
|
|
|
def _router_with_two_pass_through_deployments(blocked_flags):
|
|
import litellm
|
|
|
|
model_list = []
|
|
for idx, blocked in enumerate(blocked_flags):
|
|
model_list.append(
|
|
{
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {
|
|
"model": f"openai/gpt-4o-{idx}",
|
|
"api_key": "sk-fake-for-tests",
|
|
"use_in_pass_through": True,
|
|
},
|
|
"model_info": {"id": f"pt-{idx}", "blocked": blocked},
|
|
}
|
|
)
|
|
return litellm.Router(model_list=model_list)
|
|
|
|
|
|
def test_get_available_deployment_for_pass_through_skips_blocked():
|
|
router = _router_with_two_pass_through_deployments([True, False])
|
|
deployment = router.get_available_deployment_for_pass_through(model="gpt-4o", request_kwargs={})
|
|
assert deployment["model_info"]["id"] == "pt-1"
|
|
|
|
|
|
def test_get_available_deployment_for_pass_through_raises_when_dict_blocked():
|
|
import litellm
|
|
|
|
router = _router_with_two_pass_through_deployments([True, True])
|
|
with pytest.raises(litellm.ServiceUnavailableError):
|
|
router.get_available_deployment_for_pass_through(model="pt-0", request_kwargs={})
|
|
|
|
|
|
def test_get_available_deployment_for_pass_through_names_cooldown_despite_healthy_non_pass_through():
|
|
from litellm.types.router import RouterRateLimitError
|
|
|
|
router: Final = _router_with_two_pass_through_deployments([False, False])
|
|
router.add_deployment(
|
|
Deployment(
|
|
model_name="gpt-4o",
|
|
litellm_params=LiteLLM_Params(model="openai/gpt-4o-plain", api_key="sk-fake-for-tests"),
|
|
model_info=ModelInfo(id="plain-0"),
|
|
)
|
|
)
|
|
_cool_down(router, "pt-0", "pt-1")
|
|
with pytest.raises(RouterRateLimitError) as exc_info:
|
|
router.get_available_deployment_for_pass_through(model="gpt-4o", request_kwargs={})
|
|
assert exc_info.value.all_deployments_in_cooldown is True
|
|
assert exc_info.value.type == "all_deployments_in_cooldown"
|
|
|
|
|
|
def test_initialize_deployment_for_pass_through_keeps_bedrock_iam_deployment():
|
|
"""
|
|
Bedrock deployments using IAM/OIDC auth have no api_key; pass-through
|
|
init must not raise and drop them from routing (#27728).
|
|
"""
|
|
import litellm
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-claude",
|
|
"litellm_params": {
|
|
"model": "bedrock/anthropic.claude-3-5-sonnet-20241022-v2:0",
|
|
"aws_role_name": "arn:aws:iam::123456789012:role/my-role",
|
|
"aws_session_name": "my-session",
|
|
"use_in_pass_through": True,
|
|
},
|
|
"model_info": {"id": "bedrock-iam-pt"},
|
|
}
|
|
]
|
|
)
|
|
assert [m["model_info"]["id"] for m in router.get_model_list()] == ["bedrock-iam-pt"]
|
|
|
|
|
|
def test_pass_through_deployment_api_key_resolves_via_get_credentials():
|
|
from litellm.proxy.pass_through_endpoints.passthrough_endpoint_router import (
|
|
PassthroughEndpointRouter,
|
|
)
|
|
|
|
router = _router_with_two_pass_through_deployments([False, False])
|
|
passthrough_router = PassthroughEndpointRouter(llm_router_getter=lambda: router)
|
|
assert len(router.get_model_list()) == 2
|
|
assert passthrough_router.get_credentials(custom_llm_provider="openai", region_name=None) == "sk-fake-for-tests"
|
|
|
|
|
|
def test_get_deployment_credentials_returns_none_for_blocked_deployment():
|
|
router = _router_with_two_deployments([True, False])
|
|
assert router.get_deployment_credentials(model_id="dep-0") is None
|
|
assert router.get_deployment_credentials(model_id="dep-1") is not None
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_returns_none_for_blocked_deployment():
|
|
router = _router_with_two_deployments([True, False])
|
|
assert router.get_deployment_credentials_with_provider(model_id="dep-0") is None
|
|
assert router.get_deployment_credentials_with_provider(model_id="dep-1") is not None
|
|
|
|
|
|
def test_synchronous_text_completion_rejects_a_restricted_model_before_the_provider() -> None:
|
|
model: Final = "text-completion-openai/unit-text-model"
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": model,
|
|
"litellm_params": {
|
|
"model": model,
|
|
"api_base": "https://sync-router.invalid/v1",
|
|
"api_key": "unit-provider-key",
|
|
},
|
|
"model_info": {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
with respx.mock(assert_all_called=False) as upstream:
|
|
provider: Final = upstream.post("https://sync-router.invalid/v1/completions").respond(
|
|
200,
|
|
json={
|
|
"id": "cmpl-unit",
|
|
"object": "text_completion",
|
|
"created": 1,
|
|
"model": "unit-text-model",
|
|
"choices": [{"text": "provider called", "index": 0, "finish_reason": "stop", "logprobs": None}],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
},
|
|
)
|
|
with pytest.raises(litellm.BadRequestError, match="/v1/completions is not supported"):
|
|
router.text_completion(model=model, prompt="test")
|
|
assert provider.call_count == 0
|
|
|
|
|
|
def test_routable_deployments_leave_out_models_that_refuse_the_call_type() -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "available",
|
|
"litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "unit-provider-key"},
|
|
},
|
|
{
|
|
"model_name": "embed-only",
|
|
"litellm_params": {"model": "openai/text-embedding-3-small", "api_key": "unit-provider-key"},
|
|
"model_info": {"enforce_supported_endpoints": True, "supported_endpoints": ["/v1/embeddings"]},
|
|
},
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
def routable(model: str, call_type: str | None) -> tuple[str, ...]:
|
|
return tuple(
|
|
deployment["model_name"] for deployment in router.get_routable_deployments(model, {}, call_type=call_type)
|
|
)
|
|
|
|
assert routable("unregistered-fallback", "acompletion") == ()
|
|
assert routable("available", "acompletion") == ("available",)
|
|
assert routable("embed-only", "acompletion") == ()
|
|
assert routable("embed-only", "aembedding") == ("embed-only",)
|
|
assert routable("embed-only", None) == ("embed-only",)
|
|
|
|
|
|
def test_routing_groups_legacy_property_forwards_to_public_attribute():
|
|
router = _router_with_two_deployments([False, False])
|
|
|
|
assert router._routing_groups is router.routing_groups
|
|
|
|
replacement = dict(router.routing_groups)
|
|
router._routing_groups = replacement
|
|
|
|
assert router.routing_groups is replacement
|
|
|
|
|
|
class TestRouterRequestTimeoutPropagation:
|
|
"""litellm_settings.request_timeout must act as an independent per-attempt timeout.
|
|
|
|
Regression for LIT-2369: request_timeout was shadowed by router_settings.timeout,
|
|
so Bedrock (and other provider) calls fell back to the hardcoded 600s httpx
|
|
default instead of the configured value.
|
|
"""
|
|
|
|
def _make_router(self, timeout=None, stream_timeout=None):
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "test-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4",
|
|
"api_key": "sk-test",
|
|
},
|
|
}
|
|
],
|
|
timeout=timeout,
|
|
stream_timeout=stream_timeout,
|
|
)
|
|
|
|
@pytest.fixture
|
|
def explicit_request_timeout(self):
|
|
original_value = litellm.request_timeout
|
|
original_flag = litellm.request_timeout_explicitly_set
|
|
litellm.request_timeout = 300
|
|
litellm.request_timeout_explicitly_set = True
|
|
try:
|
|
yield 300
|
|
finally:
|
|
litellm.request_timeout = original_value
|
|
litellm.request_timeout_explicitly_set = original_flag
|
|
|
|
def test_request_timeout_stored_independently_when_both_set(self, explicit_request_timeout):
|
|
router = self._make_router(timeout=330)
|
|
assert router.timeout == 330
|
|
assert router.request_timeout == 300
|
|
|
|
def test_request_timeout_none_when_not_explicitly_configured(self):
|
|
original_value = litellm.request_timeout
|
|
original_flag = litellm.request_timeout_explicitly_set
|
|
litellm.request_timeout = litellm.constants.DEFAULT_REQUEST_TIMEOUT_SECONDS
|
|
litellm.request_timeout_explicitly_set = False
|
|
try:
|
|
router = self._make_router(timeout=330)
|
|
assert router.timeout == 330
|
|
assert router.request_timeout is None
|
|
finally:
|
|
litellm.request_timeout = original_value
|
|
litellm.request_timeout_explicitly_set = original_flag
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Deferred-stream eager-fetch tests
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestAdvisorSubCallCooldown:
|
|
"""Regression for LIT-4565: an advisor orchestration failure must not cool
|
|
down the selected (healthy) deployment, which would reject unrelated
|
|
callers to the same model group."""
|
|
|
|
def _router(self):
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude-sonnet-5",
|
|
"litellm_params": {"model": "bedrock/us.anthropic.claude-opus-4-8"},
|
|
"model_info": {"id": "dep-1"},
|
|
}
|
|
],
|
|
)
|
|
|
|
def _kwargs(self, exception):
|
|
return {
|
|
"exception": exception,
|
|
"litellm_params": {"model_info": {"id": "dep-1"}, "metadata": {}},
|
|
}
|
|
|
|
def _auth_error(self):
|
|
return litellm.AuthenticationError(
|
|
message="x-api-key header is required",
|
|
llm_provider="anthropic",
|
|
model="claude-opus-4-8",
|
|
)
|
|
|
|
def _cooled_down_ids(self, router):
|
|
active = router.cooldown_cache.get_active_cooldowns(model_ids=["dep-1"], parent_otel_span=None)
|
|
return [entry[0] for entry in active]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_untagged_auth_error_cools_down_deployment(self):
|
|
from datetime import datetime
|
|
|
|
router = self._router()
|
|
now = datetime.now()
|
|
assert router.deployment_callback_on_failure(self._kwargs(self._auth_error()), None, now, now) is True
|
|
assert "dep-1" in self._cooled_down_ids(router)
|
|
|
|
def test_advisor_orchestration_failure_does_not_cool_down_deployment(self):
|
|
from datetime import datetime
|
|
|
|
from litellm.router_utils.cooldown_handlers import (
|
|
mark_advisor_orchestration_failure,
|
|
)
|
|
|
|
router = self._router()
|
|
exception = self._auth_error()
|
|
mark_advisor_orchestration_failure(exception)
|
|
|
|
now = datetime.now()
|
|
assert router.deployment_callback_on_failure(self._kwargs(exception), None, now, now) is False
|
|
assert "dep-1" not in self._cooled_down_ids(router)
|
|
|
|
|
|
class TestBackgroundResponseCostPollCooldown:
|
|
def _router(self):
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4.1",
|
|
"litellm_params": {"model": "openai/gpt-4.1"},
|
|
"model_info": {"id": "dep-1"},
|
|
},
|
|
{
|
|
"model_name": "gpt-4.1",
|
|
"litellm_params": {"model": "openai/gpt-4.1"},
|
|
"model_info": {"id": "dep-2"},
|
|
},
|
|
],
|
|
allowed_fails=0,
|
|
)
|
|
|
|
def _cooled_down_ids(self, router):
|
|
active = router.cooldown_cache.get_active_cooldowns(model_ids=["dep-1"], parent_otel_span=None)
|
|
return [entry[0] for entry in active]
|
|
|
|
def _not_found(self):
|
|
return litellm.NotFoundError(
|
|
message="Response with id 'resp_gone' not found.", llm_provider="openai", model="gpt-4.1"
|
|
)
|
|
|
|
def _deployment_callback_on_failure(self, router, kwargs):
|
|
from datetime import datetime
|
|
|
|
now = datetime.now()
|
|
return router.deployment_callback_on_failure(kwargs, None, now, now)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_untagged_not_found_cools_down_deployment(self):
|
|
router = self._router()
|
|
assert (
|
|
self._deployment_callback_on_failure(
|
|
router,
|
|
{
|
|
"exception": self._not_found(),
|
|
"litellm_params": {"model_info": {"id": "dep-1"}, "metadata": {}},
|
|
},
|
|
)
|
|
is True
|
|
)
|
|
assert "dep-1" in self._cooled_down_ids(router)
|
|
|
|
@pytest.mark.parametrize("poll_origin", ["background_response_cost_poll", "background_response_status_poll"])
|
|
def test_background_response_poll_not_found_does_not_cool_down_deployment(self, poll_origin: str):
|
|
"""A client can delete its background response before the poll reads it, so a 404 on either
|
|
poll read says nothing about the deployment's health."""
|
|
from datetime import datetime
|
|
|
|
from litellm.constants import INTERNAL_CALL_ORIGIN_METADATA_KEY
|
|
from litellm.router_utils.router_callbacks.track_deployment_metrics import (
|
|
get_deployment_failures_for_current_minute,
|
|
)
|
|
|
|
router = self._router()
|
|
now = datetime.now()
|
|
assert (
|
|
router.deployment_callback_on_failure(
|
|
{
|
|
"exception": self._not_found(),
|
|
"litellm_params": {
|
|
"model_info": {"id": "dep-1"},
|
|
"litellm_metadata": {INTERNAL_CALL_ORIGIN_METADATA_KEY: poll_origin},
|
|
},
|
|
},
|
|
None,
|
|
now,
|
|
now,
|
|
)
|
|
is False
|
|
)
|
|
assert self._cooled_down_ids(router) == []
|
|
value = get_deployment_failures_for_current_minute(ctx=router._core_ctx, deployment_id="dep-1")
|
|
assert not value
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cost_poll_non_404_still_cools_down_deployment(self):
|
|
from litellm.constants import INTERNAL_CALL_ORIGIN_METADATA_KEY
|
|
from litellm.types.utils import BACKGROUND_RESPONSE_COST_POLL_CALL_ORIGIN
|
|
|
|
router = self._router()
|
|
assert (
|
|
self._deployment_callback_on_failure(
|
|
router,
|
|
{
|
|
"exception": litellm.InternalServerError(
|
|
message="upstream 500", llm_provider="openai", model="gpt-4.1"
|
|
),
|
|
"litellm_params": {
|
|
"model_info": {"id": "dep-1"},
|
|
"litellm_metadata": {
|
|
INTERNAL_CALL_ORIGIN_METADATA_KEY: BACKGROUND_RESPONSE_COST_POLL_CALL_ORIGIN
|
|
},
|
|
},
|
|
},
|
|
)
|
|
is True
|
|
)
|
|
assert "dep-1" in self._cooled_down_ids(router)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_other_internal_origin_not_found_still_cools_down_deployment(self):
|
|
from litellm.constants import INTERNAL_CALL_ORIGIN_METADATA_KEY
|
|
from litellm.types.utils import AUTOROUTER_CLASSIFIER_CALL_ORIGIN
|
|
|
|
router = self._router()
|
|
assert (
|
|
self._deployment_callback_on_failure(
|
|
router,
|
|
{
|
|
"exception": self._not_found(),
|
|
"litellm_params": {
|
|
"model_info": {"id": "dep-1"},
|
|
"litellm_metadata": {INTERNAL_CALL_ORIGIN_METADATA_KEY: AUTOROUTER_CLASSIFIER_CALL_ORIGIN},
|
|
},
|
|
},
|
|
)
|
|
is True
|
|
)
|
|
assert "dep-1" in self._cooled_down_ids(router)
|
|
|
|
|
|
class TestCallerTimeoutCooldown:
|
|
"""A timeout the caller set (the proxy's `timeout` body field or x-litellm-timeout
|
|
header) comes back as a 408 whatever the deployment's health, so it must neither
|
|
count toward allowed_fails nor bench the deployment. A 408 without that marker, or
|
|
one that arrives before the caller's deadline could have fired, is the provider's
|
|
and keeps cooling the deployment down."""
|
|
|
|
def _router(self):
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "slow-model",
|
|
"litellm_params": {"model": "openai/gpt-5.6", "api_key": "sk-fake"},
|
|
"model_info": {"id": "dep-1"},
|
|
},
|
|
{
|
|
"model_name": "slow-model",
|
|
"litellm_params": {"model": "openai/gpt-5.6", "api_key": "sk-fake"},
|
|
"model_info": {"id": "dep-2"},
|
|
},
|
|
],
|
|
allowed_fails=0,
|
|
cooldown_time=120,
|
|
num_retries=0,
|
|
)
|
|
|
|
def _kwargs(self, marker, started=None, ended=None):
|
|
exception = litellm.Timeout(message="Request timed out", model="gpt-5.6", llm_provider="openai")
|
|
return {
|
|
"exception": exception,
|
|
"api_call_start_time": started,
|
|
"end_time": ended,
|
|
"litellm_params": {"model_info": {"id": "dep-1"}, "metadata": {}, **marker},
|
|
}
|
|
|
|
def _fail_count(self, router):
|
|
from litellm.router_utils.router_callbacks.track_deployment_metrics import (
|
|
get_deployment_failures_for_current_minute,
|
|
)
|
|
|
|
return get_deployment_failures_for_current_minute(ctx=router._core_ctx, deployment_id="dep-1")
|
|
|
|
def _cooled_down_ids(self, router):
|
|
active = router.cooldown_cache.get_active_cooldowns(model_ids=["dep-1"], parent_otel_span=None)
|
|
return [entry[0] for entry in active]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_caller_timeout_408_leaves_failure_counter_and_cooldown_untouched(self):
|
|
router = self._router()
|
|
started = datetime.now()
|
|
ended = started + timedelta(seconds=2.05)
|
|
kwargs = self._kwargs({"client_side_timeout": True, "timeout": 2}, started=started, ended=ended)
|
|
assert router.deployment_callback_on_failure(kwargs, None, started, ended) is False
|
|
assert self._fail_count(router) == 0
|
|
assert self._cooled_down_ids(router) == []
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_provider_timeout_408_still_counts_and_cools_down(self):
|
|
router = self._router()
|
|
now = datetime.now()
|
|
assert router.deployment_callback_on_failure(self._kwargs({}), None, now, now) is True
|
|
assert self._fail_count(router) == 1
|
|
assert self._cooled_down_ids(router) == ["dep-1"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_provider_408_before_caller_deadline_still_counts_and_cools_down(self):
|
|
"""The marker only says the caller configured a timeout. A 408 that comes back
|
|
well before that deadline was raised by the provider, so it is a real health
|
|
signal and must not hide behind the caller's timeout."""
|
|
router = self._router()
|
|
started = datetime.now()
|
|
ended = started + timedelta(seconds=0.4)
|
|
kwargs = self._kwargs({"client_side_timeout": True, "timeout": 30}, started=started, ended=ended)
|
|
assert router.deployment_callback_on_failure(kwargs, None, started, ended) is True
|
|
assert self._fail_count(router) == 1
|
|
assert self._cooled_down_ids(router) == ["dep-1"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_caller_timeout_marker_reaches_failure_callback_end_to_end(self):
|
|
router = self._router()
|
|
seen = []
|
|
recorded = threading.Event()
|
|
|
|
def record(kwargs, completion_response, start_time, end_time):
|
|
seen.append(kwargs)
|
|
recorded.set()
|
|
|
|
litellm.failure_callback.append(record)
|
|
try:
|
|
with pytest.raises(litellm.Timeout):
|
|
await router.acompletion(
|
|
model="slow-model",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
mock_timeout=True,
|
|
timeout=0.001,
|
|
client_side_timeout=True,
|
|
)
|
|
assert await asyncio.to_thread(recorded.wait, 5)
|
|
finally:
|
|
litellm.failure_callback.remove(record)
|
|
assert seen[0]["litellm_params"]["client_side_timeout"] is True
|
|
assert self._fail_count(router) == 0
|
|
assert self._cooled_down_ids(router) == []
|
|
|
|
|
|
def test_stream_chunks_have_generated_content_detects_text_and_non_text():
|
|
from litellm.router_rust.invoke.streaming import _stream_chunks_have_generated_content # noqa: PLC2701
|
|
from litellm.types.utils import (
|
|
ChatCompletionDeltaToolCall,
|
|
Delta,
|
|
Function,
|
|
StreamingChoices,
|
|
)
|
|
|
|
def _chunk(delta):
|
|
return litellm.ModelResponseStream(
|
|
id="chatcmpl-1",
|
|
model="gpt-4",
|
|
object="chat.completion.chunk",
|
|
choices=[StreamingChoices(finish_reason=None, index=0, delta=delta)],
|
|
)
|
|
|
|
assert _stream_chunks_have_generated_content([]) is False
|
|
|
|
empty_chunk = _chunk(Delta(role="assistant"))
|
|
assert _stream_chunks_have_generated_content([empty_chunk]) is False
|
|
|
|
text_chunk = _chunk(Delta(content="Hello"))
|
|
assert _stream_chunks_have_generated_content([text_chunk]) is True
|
|
|
|
reasoning_chunk = _chunk(Delta(reasoning_content="Thinking"))
|
|
assert _stream_chunks_have_generated_content([reasoning_chunk]) is True
|
|
|
|
tool_call_delta = Delta(
|
|
tool_calls=[
|
|
ChatCompletionDeltaToolCall(
|
|
id="call_1",
|
|
function=Function(name="get_weather", arguments="{}"),
|
|
type="function",
|
|
index=0,
|
|
)
|
|
]
|
|
)
|
|
tool_call_chunk = _chunk(tool_call_delta)
|
|
assert _stream_chunks_have_generated_content([tool_call_chunk]) is True
|
|
|
|
thinking_delta = Delta(thinking_blocks=[{"type": "thinking", "thinking": "Let me think..."}])
|
|
thinking_chunk = _chunk(thinking_delta)
|
|
assert _stream_chunks_have_generated_content([thinking_chunk]) is True
|
|
|
|
reasoning_items_delta = Delta(reasoning_items=[{"type": "reasoning", "id": "rs_1"}])
|
|
reasoning_items_chunk = _chunk(reasoning_items_delta)
|
|
assert _stream_chunks_have_generated_content([reasoning_items_chunk]) is True
|
|
|
|
audio_delta = Delta(audio={"data": "abc123", "expires_at": 1234567890, "transcript": "hello"})
|
|
audio_chunk = _chunk(audio_delta)
|
|
assert _stream_chunks_have_generated_content([audio_chunk]) is True
|
|
|
|
images_delta = Delta(
|
|
images=[{"image_url": {"url": "https://example.com/img.png"}, "index": 0, "type": "image_url"}]
|
|
)
|
|
images_chunk = _chunk(images_delta)
|
|
assert _stream_chunks_have_generated_content([images_chunk]) is True
|
|
|
|
annotations_delta = Delta(annotations=[{"type": "url_citation", "url_citation": {"url": "https://example.com"}}])
|
|
annotations_chunk = _chunk(annotations_delta)
|
|
assert _stream_chunks_have_generated_content([annotations_chunk]) is True
|
|
|
|
|
|
def test_get_configured_token_limits_reads_deployment_model_info():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "my-custom-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
"model_info": {"max_input_tokens": 32000, "max_output_tokens": 8000},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_token_limits("my-custom-model") == (32000, 8000)
|
|
|
|
|
|
def test_get_configured_token_limits_returns_none_for_unset_or_unknown():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "no-limits-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_token_limits("no-limits-model") == (None, None)
|
|
assert router.get_configured_token_limits("not-a-real-model") == (None, None)
|
|
|
|
|
|
def test_get_configured_token_limits_skips_wildcard_pattern_matching():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock/*",
|
|
"litellm_params": {"model": "bedrock/*"},
|
|
"model_info": {"max_input_tokens": 12345},
|
|
}
|
|
]
|
|
)
|
|
|
|
with patch.object(router.pattern_router, "route", side_effect=AssertionError("pattern route called")):
|
|
assert router.get_configured_token_limits("bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0") == (None, None)
|
|
|
|
|
|
def test_get_configured_token_limits_treats_malformed_values_as_absent():
|
|
malformed = ["", "unlimited", "128,000", [128000], {"max": 128000}, True]
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": f"bad-limit-{i}",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
"model_info": {"max_input_tokens": bad, "max_output_tokens": bad},
|
|
}
|
|
for i, bad in enumerate(malformed)
|
|
]
|
|
)
|
|
|
|
for i in range(len(malformed)):
|
|
assert router.get_configured_token_limits(f"bad-limit-{i}") == (None, None)
|
|
|
|
|
|
def test_get_configured_token_limits_coerces_numeric_strings():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "quoted-limits-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
"model_info": {"max_input_tokens": "32000", "max_output_tokens": "8000"},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_token_limits("quoted-limits-model") == (32000, 8000)
|
|
|
|
|
|
def test_get_model_listing_info_prefers_base_model_over_litellm_params_model():
|
|
"""The cost-map key comes from base_model when set, so a deployment pointing at an
|
|
opaque backend name still resolves the real catalog entry."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-claude-opus-5",
|
|
"litellm_params": {"model": "bedrock/eu.anthropic.claude-opus-5"},
|
|
"model_info": {"base_model": "eu.anthropic.claude-opus-5"},
|
|
}
|
|
]
|
|
)
|
|
|
|
info = router.get_model_listing_info("bedrock-claude-opus-5")
|
|
assert info is not None
|
|
assert info.cost_map_keys == ("eu.anthropic.claude-opus-5",)
|
|
|
|
|
|
def test_get_model_listing_info_falls_back_to_litellm_params_model():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-claude-opus-5",
|
|
"litellm_params": {"model": "bedrock/eu.anthropic.claude-opus-5"},
|
|
}
|
|
]
|
|
)
|
|
|
|
info = router.get_model_listing_info("bedrock-claude-opus-5")
|
|
assert info is not None
|
|
assert info.cost_map_keys == ("bedrock/eu.anthropic.claude-opus-5",)
|
|
|
|
|
|
def test_get_model_listing_info_ignores_blank_base_model():
|
|
"""A base_model set to an empty string is absent, not a cost-map key."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-claude-opus-5",
|
|
"litellm_params": {"model": "bedrock/eu.anthropic.claude-opus-5"},
|
|
"model_info": {"base_model": ""},
|
|
}
|
|
]
|
|
)
|
|
|
|
info = router.get_model_listing_info("bedrock-claude-opus-5")
|
|
assert info is not None
|
|
assert info.cost_map_keys == ("bedrock/eu.anthropic.claude-opus-5",)
|
|
|
|
|
|
def test_get_model_listing_info_returns_none_for_unknown_name():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "no-limits-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_model_listing_info("not-a-real-model") is None
|
|
|
|
|
|
def test_get_model_listing_info_carries_configured_limits():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "my-custom-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
"model_info": {"max_input_tokens": 32000, "max_output_tokens": 8000},
|
|
}
|
|
]
|
|
)
|
|
|
|
info = router.get_model_listing_info("my-custom-model")
|
|
assert info is not None
|
|
assert (info.max_input_tokens, info.max_output_tokens) == (32000, 8000)
|
|
|
|
|
|
def test_get_model_listing_info_dedupes_interchangeable_deployments():
|
|
"""The ordinary group is N deployments of one model, so it yields exactly one key."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-a"},
|
|
},
|
|
{
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-b"},
|
|
},
|
|
]
|
|
)
|
|
|
|
info = router.get_model_listing_info("gpt-4o")
|
|
assert info is not None
|
|
assert info.cost_map_keys == ("openai/gpt-4o",)
|
|
|
|
|
|
def test_get_model_listing_info_collects_every_model_in_a_mixed_group():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "house-claude",
|
|
"litellm_params": {"model": "anthropic/claude-3-haiku-20240307"},
|
|
},
|
|
{
|
|
"model_name": "house-claude",
|
|
"litellm_params": {"model": "bedrock/eu.anthropic.claude-opus-5"},
|
|
},
|
|
]
|
|
)
|
|
|
|
info = router.get_model_listing_info("house-claude")
|
|
assert info is not None
|
|
assert info.cost_map_keys == (
|
|
"anthropic/claude-3-haiku-20240307",
|
|
"bedrock/eu.anthropic.claude-opus-5",
|
|
)
|
|
|
|
|
|
def test_get_model_listing_info_reports_widest_configured_limits_in_a_mixed_group():
|
|
"""Matches how get_model_group_info aggregates for the Admin UI, so the two agree."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "house-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
"model_info": {"max_input_tokens": 32000, "max_output_tokens": 4096},
|
|
},
|
|
{
|
|
"model_name": "house-model",
|
|
"litellm_params": {"model": "openai/another-unmapped-model"},
|
|
"model_info": {"max_input_tokens": 128000, "max_output_tokens": 16384},
|
|
},
|
|
]
|
|
)
|
|
|
|
info = router.get_model_listing_info("house-model")
|
|
assert info is not None
|
|
assert (info.max_input_tokens, info.max_output_tokens) == (128000, 16384)
|
|
|
|
|
|
def test_get_model_listing_info_reads_base_model_from_litellm_params():
|
|
"""base_model resolution mirrors get_router_model_info, which also accepts it there."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "azure-deployment",
|
|
"litellm_params": {
|
|
"model": "azure/my-azure-deployment-name",
|
|
"base_model": "azure/gpt-4o",
|
|
"api_key": "sk-a",
|
|
"api_base": "https://example.openai.azure.com",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
info = router.get_model_listing_info("azure-deployment")
|
|
assert info is not None
|
|
assert info.cost_map_keys == ("azure/gpt-4o",)
|
|
|
|
|
|
def test_get_model_listing_info_skips_wildcard_pattern_matching():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock/*",
|
|
"litellm_params": {"model": "bedrock/*"},
|
|
"model_info": {"max_input_tokens": 12345},
|
|
}
|
|
]
|
|
)
|
|
|
|
with patch.object(router.pattern_router, "route", side_effect=AssertionError("pattern route called")):
|
|
assert router.get_model_listing_info("bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0") is None
|
|
|
|
|
|
def test_get_configured_mode_reads_deployment_model_info():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "tts-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-tts-model"},
|
|
"model_info": {"mode": "audio_speech"},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_mode("tts-model") == "audio_speech"
|
|
|
|
|
|
def test_get_configured_mode_returns_none_for_unset_or_unknown():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "no-mode-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_mode("no-mode-model") is None
|
|
assert router.get_configured_mode("not-a-real-model") is None
|
|
|
|
|
|
def test_get_configured_mode_skips_wildcard_pattern_matching():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock/*",
|
|
"litellm_params": {"model": "bedrock/*"},
|
|
"model_info": {"mode": "chat"},
|
|
}
|
|
]
|
|
)
|
|
|
|
with patch.object(router.pattern_router, "route", side_effect=AssertionError("pattern route called")):
|
|
assert router.get_configured_mode("bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0") is None
|
|
|
|
|
|
def test_get_configured_mode_treats_malformed_values_as_absent():
|
|
malformed = ["", " ", 12345, ["chat"], {"mode": "chat"}, True]
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": f"bad-mode-{i}",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
"model_info": {"mode": bad},
|
|
}
|
|
for i, bad in enumerate(malformed)
|
|
]
|
|
)
|
|
|
|
for i in range(len(malformed)):
|
|
assert router.get_configured_mode(f"bad-mode-{i}") is None
|
|
|
|
|
|
def test_get_configured_display_name_reads_deployment_model_info():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "Kimi K3-claude-compatible",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
"model_info": {"display_name": "Kimi K3"},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_display_name("Kimi K3-claude-compatible") == "Kimi K3"
|
|
|
|
|
|
def test_get_configured_display_name_returns_none_for_unset_or_unknown():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "no-display-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_display_name("no-display-model") is None
|
|
assert router.get_configured_display_name("not-a-real-model") is None
|
|
|
|
|
|
def test_get_configured_display_name_skips_wildcard_pattern_matching():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock/*",
|
|
"litellm_params": {"model": "bedrock/*"},
|
|
"model_info": {"display_name": "Bedrock"},
|
|
}
|
|
]
|
|
)
|
|
|
|
with patch.object(router.pattern_router, "route", side_effect=AssertionError("pattern route called")):
|
|
assert router.get_configured_display_name("bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0") is None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"configured",
|
|
[["ultrafast"], ["priority", {"id": "ultrafast", "name": "Ultrafast", "description": "Fastest"}], [], "not-a-list"],
|
|
)
|
|
def test_get_configured_service_tiers_returns_the_deployment_model_info_value_as_set(configured):
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra"},
|
|
"model_info": {"service_tiers": configured},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_service_tiers("gpt-6-astra") == (configured,)
|
|
|
|
|
|
def test_get_configured_service_tiers_returns_one_value_per_deployment_in_model_list_order():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra"},
|
|
"model_info": {"service_tiers": ["ultrafast"]},
|
|
},
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra", "api_base": "https://a.example"},
|
|
},
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra", "api_base": "https://b.example"},
|
|
"model_info": {"service_tiers": ["priority", "ultrafast"]},
|
|
},
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_service_tiers("gpt-6-astra") == (["ultrafast"], None, ["priority", "ultrafast"])
|
|
|
|
|
|
def test_get_configured_service_tiers_returns_none_for_an_unset_deployment_and_nothing_for_an_unknown_name():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "no-tiers-model",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_service_tiers("no-tiers-model") == (None,)
|
|
assert router.get_configured_service_tiers("not-a-real-model") == ()
|
|
|
|
|
|
def test_get_configured_service_tiers_does_not_apply_a_wildcard_deployment_to_matched_names():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {"model": "openai/*"},
|
|
"model_info": {"service_tiers": ["ultrafast"]},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_service_tiers("openai/gpt-6-astra") == ()
|
|
|
|
|
|
def test_get_configured_service_tiers_reads_only_the_deployments_a_request_can_route_to():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra"},
|
|
"model_info": {"service_tiers": ["ultrafast"]},
|
|
},
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra", "api_base": "https://paused.example"},
|
|
"model_info": {"blocked": True},
|
|
},
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra", "api_base": "https://team-2.example"},
|
|
"model_info": {"team_id": "team-2"},
|
|
},
|
|
]
|
|
)
|
|
|
|
assert router.get_configured_service_tiers("gpt-6-astra", team_id="team-1") == (["ultrafast"],)
|
|
assert router.get_configured_service_tiers("gpt-6-astra", team_id="team-2") == (["ultrafast"], None)
|
|
assert router.get_configured_service_tiers("gpt-6-astra") == (["ultrafast"],)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"litellm_params, model_info, expected",
|
|
[
|
|
({"model": "anthropic/claude-fable-5-1"}, None, True),
|
|
({"model": "bedrock/invoke/us.anthropic.claude-fable-5-1"}, None, True),
|
|
({"model": "openai/gpt-6-astra"}, None, None),
|
|
({"model": "gemini/gemini-3.8-flash"}, None, None),
|
|
({"model": "sonar-pro", "api_base": "https://api.perplexity.ai"}, None, None),
|
|
({"model": "gemini/gemini-3.8-flash"}, {"supports_search_tool": True}, True),
|
|
({"model": "anthropic/claude-fable-5-1"}, {"supports_search_tool": False}, False),
|
|
],
|
|
ids=[
|
|
"anthropic-defaults-on",
|
|
"bedrock-invoke-anthropic-defaults-on",
|
|
"openai-passthrough-unset",
|
|
"other-bridged-provider-unset",
|
|
"provider-inferred-from-api-base-unset",
|
|
"model-info-opt-in-wins",
|
|
"model-info-opt-out-wins",
|
|
],
|
|
)
|
|
def test_get_search_tool_support_defaults_from_the_provider_and_lets_model_info_override(
|
|
litellm_params: dict[str, str], model_info: dict[str, bool] | None, expected: bool | None
|
|
):
|
|
deployment: Final = {"model_name": "codex-model", "litellm_params": litellm_params}
|
|
router: Final = litellm.Router(model_list=[{**deployment, "model_info": model_info} if model_info else deployment])
|
|
|
|
assert router.get_search_tool_support("codex-model") == (expected,)
|
|
|
|
|
|
def test_get_search_tool_support_reports_each_routable_deployment_of_the_group():
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "mixed", "litellm_params": {"model": "anthropic/claude-fable-5-1"}},
|
|
{"model_name": "mixed", "litellm_params": {"model": "openai/gpt-6-astra"}},
|
|
{
|
|
"model_name": "mixed",
|
|
"litellm_params": {"model": "anthropic/claude-fable-5-1", "api_base": "https://paused.example"},
|
|
"model_info": {"blocked": True, "supports_search_tool": False},
|
|
},
|
|
]
|
|
)
|
|
|
|
assert router.get_search_tool_support("mixed") == (True, None)
|
|
assert router.get_search_tool_support("not-a-real-model") == ()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.respx(assert_all_called=False)
|
|
@pytest.mark.parametrize("provider", ["chatgpt", "github_copilot"])
|
|
@pytest.mark.parametrize("explicit_provider", [False, True])
|
|
async def test_search_tool_catalog_lookup_needs_no_credentials(
|
|
provider: str,
|
|
explicit_provider: bool,
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
respx_mock: respx.MockRouter,
|
|
) -> None:
|
|
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path / "chatgpt"))
|
|
monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path / "copilot"))
|
|
(tmp_path / "chatgpt").mkdir()
|
|
(tmp_path / "chatgpt" / "auth.json").write_text(
|
|
json.dumps({"access_token": "expired-access", "refresh_token": "test-refresh", "expires_at": 1})
|
|
)
|
|
(tmp_path / "copilot").mkdir()
|
|
(tmp_path / "copilot" / "api-key.json").write_text(json.dumps({"token": "expired-key", "expires_at": 1}))
|
|
(tmp_path / "copilot" / "access-token").write_text("test-access")
|
|
respx_mock.route().respond(401, json={"error": "invalid_grant"})
|
|
params: Final = (
|
|
{"model": "catalog-model", "custom_llm_provider": provider}
|
|
if explicit_provider
|
|
else {"model": f"{provider}/catalog-model"}
|
|
)
|
|
|
|
assert _search_tool_support({"model_name": "catalog-model", "litellm_params": params}) is None
|
|
assert len(respx_mock.calls) == 0
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"alias_value, expected_group",
|
|
[
|
|
("gpt-6-astra", "gpt-6-astra"),
|
|
({"model": "gpt-6-astra", "hidden": True}, "gpt-6-astra"),
|
|
({"model": "", "hidden": False}, "gpt-6"),
|
|
],
|
|
ids=["string-alias", "item-alias", "malformed-alias-is-itself"],
|
|
)
|
|
def test_routable_model_group_is_the_alias_target_else_the_name_itself(alias_value, expected_group):
|
|
router = litellm.Router(
|
|
model_list=[{"model_name": "gpt-6-astra", "litellm_params": {"model": "openai/gpt-6-astra"}}],
|
|
model_group_alias={"gpt-6": alias_value},
|
|
)
|
|
|
|
assert router.routable_model_group("gpt-6") == expected_group
|
|
assert router.routable_model_group("gpt-6-astra") == "gpt-6-astra"
|
|
assert router.routable_model_group("not-a-real-model") == "not-a-real-model"
|
|
|
|
|
|
def test_get_configured_service_tiers_reads_an_alias_off_its_target_deployments():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra"},
|
|
"model_info": {"service_tiers": ["ultrafast"]},
|
|
},
|
|
{
|
|
"model_name": "gpt-6-astra",
|
|
"litellm_params": {"model": "openai/gpt-6-astra", "api_base": "https://team-2.example"},
|
|
"model_info": {"team_id": "team-2", "service_tiers": ["priority"]},
|
|
},
|
|
],
|
|
model_group_alias={"gpt-6": "gpt-6-astra", "gpt-6-quiet": {"model": "gpt-6-astra", "hidden": True}},
|
|
)
|
|
|
|
assert router.get_configured_service_tiers("gpt-6") == router.get_configured_service_tiers("gpt-6-astra")
|
|
assert router.get_configured_service_tiers("gpt-6", team_id="team-1") == (["ultrafast"],)
|
|
assert router.get_configured_service_tiers("gpt-6", team_id="team-2") == (["ultrafast"], ["priority"])
|
|
assert router.get_configured_service_tiers("gpt-6-quiet", team_id="team-1") == (["ultrafast"],)
|
|
|
|
|
|
def _router_with_team_owned_deployments():
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "owned-by-teams",
|
|
"litellm_params": {"model": "openai/gpt-5.5"},
|
|
"model_info": {"team_id": "team-1", "service_tiers": ["priority"]},
|
|
},
|
|
{
|
|
"model_name": "owned-by-teams",
|
|
"litellm_params": {"model": "openai/paused-model"},
|
|
"model_info": {"team_id": "team-2", "blocked": True, "service_tiers": ["paused"]},
|
|
},
|
|
{
|
|
"model_name": "owned-by-teams",
|
|
"litellm_params": {"model": "openai/team-2-model"},
|
|
"model_info": {"team_id": "team-2", "service_tiers": ["flex"]},
|
|
},
|
|
{
|
|
"model_name": "owned-and-shared",
|
|
"litellm_params": {"model": "openai/team-1-model"},
|
|
"model_info": {"team_id": "team-1", "service_tiers": ["priority"]},
|
|
},
|
|
{
|
|
"model_name": "owned-and-shared",
|
|
"litellm_params": {"model": "openai/shared-model"},
|
|
"model_info": {"service_tiers": ["flex"]},
|
|
},
|
|
{"model_name": "openai/*", "litellm_params": {"model": "openai/*"}},
|
|
],
|
|
model_group_alias={"nickname": "owned-by-teams"},
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model_name, team_id, upstream_model, service_tiers",
|
|
[
|
|
("owned-by-teams", "team-1", "openai/gpt-5.5", (["priority"],)),
|
|
("owned-by-teams", "team-2", "openai/team-2-model", (["flex"],)),
|
|
("nickname", "team-1", "openai/gpt-5.5", (["priority"],)),
|
|
("nickname", "team-2", "openai/team-2-model", (["flex"],)),
|
|
("owned-by-teams", "team-3", None, ()),
|
|
("owned-by-teams", None, "openai/gpt-5.5", (["priority"], ["flex"])),
|
|
("owned-and-shared", "team-1", "openai/team-1-model", (["priority"], ["flex"])),
|
|
("owned-and-shared", "team-2", "openai/shared-model", (["flex"],)),
|
|
("owned-and-shared", None, "openai/shared-model", (["flex"],)),
|
|
("openai/gpt-5.5", "team-1", None, ()),
|
|
("not-a-real-model", None, None, ()),
|
|
],
|
|
)
|
|
def test_upstream_model_and_service_tiers_are_read_off_the_deployments_the_team_can_route_to(
|
|
model_name, team_id, upstream_model, service_tiers
|
|
):
|
|
router = _router_with_team_owned_deployments()
|
|
|
|
assert router.get_routable_upstream_model(model_name, team_id) == upstream_model
|
|
assert router.get_configured_service_tiers(model_name, team_id) == service_tiers
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("litellm_params", "provider"),
|
|
[
|
|
({"model": "us.openai.gpt-6.1-sol", "custom_llm_provider": "bedrock"}, "bedrock"),
|
|
({"model": "bedrock_mantle/openai.gpt-6.1-sol"}, "bedrock_mantle"),
|
|
({"model": "gpt-6.1-sol"}, None),
|
|
],
|
|
)
|
|
def test_the_upstream_provider_is_the_one_the_routable_deployment_declares(litellm_params, provider):
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "sol", "litellm_params": litellm_params, "model_info": {"team_id": "team-1"}},
|
|
{"model_name": "sol", "litellm_params": {"model": "azure/sol-deployment"}, "model_info": {"team_id": "t2"}},
|
|
]
|
|
)
|
|
|
|
assert router.registry().resolve("sol", kind="upstream_provider", team_id="team-1") == provider
|
|
assert router.registry().resolve("sol", kind="upstream_provider", team_id="t2") == "azure"
|
|
assert router.registry().resolve("not-a-real-model", kind="upstream_provider") is None
|
|
|
|
|
|
def test_get_configured_display_name_treats_malformed_values_as_absent():
|
|
malformed = ["", " ", 12345, ["Kimi K3"], {"name": "Kimi K3"}, True]
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": f"bad-display-{i}",
|
|
"litellm_params": {"model": "openai/some-unmapped-model"},
|
|
"model_info": {"display_name": bad},
|
|
}
|
|
for i, bad in enumerate(malformed)
|
|
]
|
|
)
|
|
|
|
for i in range(len(malformed)):
|
|
assert router.get_configured_display_name(f"bad-display-{i}") is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acreate_batch_still_falls_back_within_the_owning_model_group():
|
|
"""Holding a batch inside the model group that owns its input file must not
|
|
disable fallbacks outright (#35359): the owning group's second deployment is
|
|
still tried in `order`, and only the cross-group target is skipped."""
|
|
completion_window_error = "Invalid value: '5m'. Supported values are: '24h'."
|
|
attempted_models = []
|
|
|
|
async def _acreate_batch(**kwargs):
|
|
model = kwargs["model"]
|
|
attempted_models.append(model)
|
|
if model.startswith("azure/"):
|
|
raise litellm.BadRequestError(
|
|
message="Error code: 400 - {'error': {'code': 'quotaExceeded'}}",
|
|
model=model,
|
|
llm_provider="azure",
|
|
)
|
|
raise litellm.BadRequestError(
|
|
message=completion_window_error,
|
|
model=model,
|
|
llm_provider="openai",
|
|
)
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "my-gpt",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "sk-owning",
|
|
"order": 1,
|
|
},
|
|
"model_info": {"id": "my-gpt-1"},
|
|
},
|
|
{
|
|
"model_name": "my-gpt",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini-backup",
|
|
"api_key": "sk-owning",
|
|
"order": 2,
|
|
},
|
|
"model_info": {"id": "my-gpt-2"},
|
|
},
|
|
{
|
|
"model_name": "my-azure-gpt",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4o-mini",
|
|
"api_key": "sk-fallback",
|
|
"api_base": "https://fallback.openai.azure.com",
|
|
"api_version": "2024-08-01-preview",
|
|
},
|
|
"model_info": {"id": "my-azure-gpt-1"},
|
|
},
|
|
],
|
|
fallbacks=[{"my-gpt": ["my-azure-gpt"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
with patch.object(litellm, "acreate_batch", new=_acreate_batch):
|
|
with pytest.raises(litellm.BadRequestError) as raised:
|
|
await router.acreate_batch(
|
|
model="my-gpt",
|
|
input_file_id="file-owned-by-my-gpt",
|
|
endpoint="/v1/chat/completions",
|
|
completion_window="5m",
|
|
)
|
|
|
|
assert "24h" in str(raised.value)
|
|
assert "quotaExceeded" not in str(raised.value)
|
|
assert attempted_models == ["openai/gpt-4o-mini", "openai/gpt-4o-mini-backup"]
|
|
|
|
|
|
def _azure_sku_deployment(model_group: str, sku_host: str, mode: str, **model_info: object) -> dict[str, object]:
|
|
return {
|
|
"model_name": model_group,
|
|
"litellm_params": {
|
|
"model": f"azure/{sku_host}",
|
|
"api_base": f"https://{sku_host}.openai.azure.com",
|
|
"api_key": "dummy-key",
|
|
"api_version": "2025-04-01-preview",
|
|
},
|
|
"model_info": {"mode": mode, **model_info},
|
|
}
|
|
|
|
|
|
_AZURE_BATCH_CREATED: Final = {
|
|
"id": "batch_created",
|
|
"object": "batch",
|
|
"endpoint": "/v1/chat/completions",
|
|
"input_file_id": "file-batch-input",
|
|
"completion_window": "24h",
|
|
"status": "validating",
|
|
"created_at": 0,
|
|
}
|
|
|
|
|
|
async def _create_batches(router: Router, model_group: str, attempts: int) -> None:
|
|
for _ in range(attempts):
|
|
await router.acreate_batch(
|
|
model=model_group,
|
|
input_file_id="file-batch-input",
|
|
endpoint="/v1/chat/completions",
|
|
completion_window="24h",
|
|
)
|
|
|
|
|
|
def _azure_chat_row_with_provider_param() -> dict[str, object]:
|
|
row: Final = _azure_sku_deployment("gpt-mixed", "chat-sku", "chat")
|
|
return {**row, "litellm_params": {**row["litellm_params"], "model": "chat-sku", "custom_llm_provider": "azure"}}
|
|
|
|
|
|
_AZURE_CHAT_ROWS: Final = {
|
|
"no-stored-list": lambda: _azure_sku_deployment("gpt-mixed", "chat-sku", "chat"),
|
|
"stored-cost-map-list": lambda: _azure_sku_deployment(
|
|
"gpt-mixed", "chat-sku", "chat", supported_endpoints=["/v1/chat/completions", "/v1/batch"]
|
|
),
|
|
"custom-llm-provider-param": _azure_chat_row_with_provider_param,
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("build_chat_row", _AZURE_CHAT_ROWS.values(), ids=_AZURE_CHAT_ROWS.keys())
|
|
@pytest.mark.parametrize("routing_strategy", ["simple-shuffle", "latency-based-routing", "usage-based-routing"])
|
|
async def test_acreate_batch_on_a_mixed_group_only_reaches_batch_deployments(
|
|
monkeypatch: pytest.MonkeyPatch, routing_strategy: str, build_chat_row: Callable[[], dict[str, object]]
|
|
):
|
|
"""Azure answers 400 invalid_deployment_type when a batch is created on a GlobalStandard
|
|
(chat) deployment, so a group mixing chat and batch deployments must send every batch
|
|
create to the deployment that /model_group/info names as the one listing /v1/batch."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
build_chat_row(),
|
|
_azure_sku_deployment("gpt-mixed", "batch-sku", "batch"),
|
|
],
|
|
routing_strategy=routing_strategy,
|
|
num_retries=0,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
chat_route: Final = respx_mock.post(host="chat-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(
|
|
400,
|
|
json={"error": {"code": "invalid_deployment_type", "message": "SKU 'GlobalStandard' is not supported"}},
|
|
)
|
|
)
|
|
batch_route: Final = respx_mock.post(host="batch-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
await _create_batches(router, "gpt-mixed", attempts=20)
|
|
|
|
assert chat_route.call_count == 0
|
|
assert batch_route.call_count == 20
|
|
|
|
|
|
def _ordered(deployment: dict[str, object], order: int) -> dict[str, object]:
|
|
return {**deployment, "litellm_params": {**deployment["litellm_params"], "order": order}}
|
|
|
|
|
|
_AZURE_FILE_CREATED: Final = {
|
|
"id": "file-batch-input",
|
|
"object": "file",
|
|
"bytes": 1,
|
|
"created_at": 0,
|
|
"filename": "input.jsonl",
|
|
"purpose": "batch",
|
|
"status": "processed",
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(("purpose", "uploaded_to"), [("batch", "batch-deployment"), ("fine-tune", "chat-deployment")])
|
|
async def test_batch_file_upload_reaches_the_batch_deployment_when_the_chat_deployment_has_a_lower_order(
|
|
monkeypatch: pytest.MonkeyPatch, purpose: str, uploaded_to: str
|
|
):
|
|
from io import BytesIO
|
|
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_ordered(_azure_sku_deployment("gpt-mixed", "chat-sku", "chat", id="chat-deployment"), 1),
|
|
_ordered(_azure_sku_deployment("gpt-mixed", "batch-sku", "batch", id="batch-deployment"), 2),
|
|
],
|
|
num_retries=0,
|
|
)
|
|
batch_row: Final = {"custom_id": "1", "method": "POST", "url": "/chat/completions", "body": {"model": "gpt-mixed"}}
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
routes: Final = {
|
|
deployment_id: respx_mock.post(host=f"{sku_host}.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_FILE_CREATED)
|
|
)
|
|
for deployment_id, sku_host in (("chat-deployment", "chat-sku"), ("batch-deployment", "batch-sku"))
|
|
}
|
|
uploaded: Final = await router.acreate_file(
|
|
model="gpt-mixed", purpose=purpose, file=BytesIO(json.dumps(batch_row).encode())
|
|
)
|
|
|
|
assert {deployment_id: route.call_count for deployment_id, route in routes.items()} == {
|
|
"chat-deployment": int(uploaded_to == "chat-deployment"),
|
|
"batch-deployment": int(uploaded_to == "batch-deployment"),
|
|
}
|
|
assert uploaded._hidden_params["model_file_id_mapping"] == {uploaded_to: "file-batch-input"}
|
|
|
|
|
|
_AZURE_CHAT_COMPLETION: Final = {
|
|
"id": "chatcmpl-1",
|
|
"object": "chat.completion",
|
|
"created": 0,
|
|
"model": "gpt-mixed",
|
|
"choices": [{"index": 0, "finish_reason": "stop", "message": {"role": "assistant", "content": "ok"}}],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(("status", "raised"), [(429, openai.RateLimitError), (500, openai.InternalServerError)])
|
|
async def test_a_failed_batch_file_upload_tries_the_batch_deployment_once_and_leaves_chat_serving(
|
|
monkeypatch: pytest.MonkeyPatch, status: int, raised: type[Exception]
|
|
):
|
|
from io import BytesIO
|
|
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_ordered(_azure_sku_deployment("gpt-mixed", "chat-sku", "chat", id="chat-deployment"), 1),
|
|
_ordered(_azure_sku_deployment("gpt-mixed", "batch-sku", "batch", id="batch-deployment"), 2),
|
|
],
|
|
num_retries=0,
|
|
)
|
|
batch_row: Final = {"custom_id": "1", "method": "POST", "url": "/chat/completions", "body": {"model": "gpt-mixed"}}
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
batch_upload: Final = respx_mock.post(host="batch-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(status, json={"error": {"code": str(status), "message": "upload refused"}})
|
|
)
|
|
chat_completions: Final = respx_mock.post(host="chat-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_CHAT_COMPLETION)
|
|
)
|
|
with pytest.raises(raised):
|
|
await router.acreate_file(model="gpt-mixed", purpose="batch", file=BytesIO(json.dumps(batch_row).encode()))
|
|
for _ in range(3):
|
|
await router.acompletion(model="gpt-mixed", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert batch_upload.call_count == 1
|
|
assert chat_completions.call_count == 3
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(("status", "raised"), [(429, openai.RateLimitError), (401, openai.AuthenticationError)])
|
|
async def test_a_batch_file_upload_the_batch_deployment_refuses_stays_off_chat_and_leaves_chat_serving(
|
|
monkeypatch: pytest.MonkeyPatch, status: int, raised: type[Exception]
|
|
):
|
|
from io import BytesIO
|
|
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_ordered(_azure_sku_deployment("gpt-mixed", "chat-sku", "chat", id="chat-deployment"), 1),
|
|
_ordered(_azure_sku_deployment("gpt-mixed", "batch-sku", "batch", id="batch-deployment"), 2),
|
|
],
|
|
num_retries=2,
|
|
)
|
|
batch_row: Final = {"custom_id": "1", "method": "POST", "url": "/chat/completions", "body": {"model": "gpt-mixed"}}
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
batch_upload: Final = respx_mock.post(host="batch-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(status, json={"error": {"code": str(status), "message": "upload refused"}})
|
|
)
|
|
chat_upload: Final = respx_mock.post(host="chat-sku.openai.azure.com", path__regex=r"/files").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_FILE_CREATED)
|
|
)
|
|
chat_completions: Final = respx_mock.post(host="chat-sku.openai.azure.com", path__regex=r"/chat/completions").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_CHAT_COMPLETION)
|
|
)
|
|
with pytest.raises(raised, match="upload refused"):
|
|
await router.acreate_file(model="gpt-mixed", purpose="batch", file=BytesIO(json.dumps(batch_row).encode()))
|
|
await asyncio.gather(*(task for task in asyncio.all_tasks() if task is not asyncio.current_task()))
|
|
for _ in range(3):
|
|
await router.acompletion(model="gpt-mixed", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert (batch_upload.call_count, chat_upload.call_count, chat_completions.call_count) == (1, 0, 3)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_batch_file_upload_whose_batch_deployment_cools_down_after_the_pick_stays_off_chat(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
from io import BytesIO
|
|
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_ordered(_azure_sku_deployment("gpt-mixed", "chat-sku", "chat", id="chat-deployment"), 1),
|
|
_ordered(_azure_sku_deployment("gpt-mixed", "batch-sku", "batch", id="batch-deployment"), 2),
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
class _ConcurrentBatchFailure(CustomLogger):
|
|
async def async_pre_call_check(self, deployment, parent_otel_span):
|
|
_cool_down(router, "batch-deployment")
|
|
|
|
batch_row: Final = {"custom_id": "1", "method": "POST", "url": "/chat/completions", "body": {"model": "gpt-mixed"}}
|
|
|
|
with (
|
|
patch.object(litellm, "callbacks", [_ConcurrentBatchFailure()]), # test-quality-ok: router reads this global
|
|
respx.mock(assert_all_called=False) as respx_mock,
|
|
):
|
|
batch_upload: Final = respx_mock.post(host="batch-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_FILE_CREATED)
|
|
)
|
|
chat_upload: Final = respx_mock.post(host="chat-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_FILE_CREATED)
|
|
)
|
|
with pytest.raises(RouterRateLimitError, match="No deployments available"):
|
|
await router.acreate_file(model="gpt-mixed", purpose="batch", file=BytesIO(json.dumps(batch_row).encode()))
|
|
|
|
assert (batch_upload.call_count, chat_upload.call_count) == (0, 0)
|
|
|
|
|
|
def _azure_wildcard_deployment(sku_host: str, mode: str) -> dict[str, object]:
|
|
return {
|
|
"model_name": "mixed-wild/*",
|
|
"litellm_params": {
|
|
"model": "azure/*",
|
|
"api_base": f"https://{sku_host}.openai.azure.com",
|
|
"api_key": "dummy-key",
|
|
"api_version": "2025-04-01-preview",
|
|
},
|
|
"model_info": {"mode": mode},
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acreate_batch_on_a_mixed_wildcard_group_only_reaches_batch_deployments(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
"""A wildcard group resolves its deployments before the named-group path does, and its batch
|
|
creates must still only reach the deployments declared with model_info.mode batch."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[_azure_wildcard_deployment("chat-sku", "chat"), _azure_wildcard_deployment("batch-sku", "batch")],
|
|
num_retries=0,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
chat_route: Final = respx_mock.post(host="chat-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(
|
|
400,
|
|
json={"error": {"code": "invalid_deployment_type", "message": "SKU 'GlobalStandard' is not supported"}},
|
|
)
|
|
)
|
|
batch_route: Final = respx_mock.post(host="batch-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
await _create_batches(router, "mixed-wild/gpt-5.4-mini", attempts=20)
|
|
|
|
assert chat_route.call_count == 0
|
|
assert batch_route.call_count == 20
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_chat_calls_after_a_batch_create_still_use_every_deployment_in_the_group(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
"""Only the batch create itself is narrowed to mode batch deployments. Chat calls the same task makes
|
|
afterwards keep load balancing across the whole group."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_azure_sku_deployment("gpt-mixed", "chat-sku", "chat", id="chat-deployment"),
|
|
_azure_sku_deployment("gpt-mixed", "batch-sku", "batch", id="batch-deployment"),
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
respx_mock.post(host="batch-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
await _create_batches(router, "gpt-mixed", attempts=2)
|
|
|
|
chat_deployments: Final = {
|
|
(
|
|
await router.acompletion(
|
|
model="gpt-mixed", messages=[{"role": "user", "content": "hi"}], mock_response="ok"
|
|
)
|
|
)._hidden_params["model_id"]
|
|
for _ in range(40)
|
|
}
|
|
|
|
assert chat_deployments == {"chat-deployment", "batch-deployment"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acreate_batch_on_a_group_without_batch_deployments_still_uses_every_deployment(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
"""Bedrock and OpenAI batch through the same deployment that serves chat, so a group with
|
|
no mode batch deployment keeps load balancing batch creates across all of them."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_azure_sku_deployment("gpt-chat-only", "first-chat-sku", "chat"),
|
|
_azure_sku_deployment("gpt-chat-only", "second-chat-sku", "chat"),
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
first_route: Final = respx_mock.post(host="first-chat-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
second_route: Final = respx_mock.post(host="second-chat-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
await _create_batches(router, "gpt-chat-only", attempts=40)
|
|
|
|
assert first_route.call_count > 0
|
|
assert second_route.call_count > 0
|
|
assert first_route.call_count + second_route.call_count == 40
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(("team_id", "reached_host"), [(None, "chat-sku"), ("team-a", "batch-sku")])
|
|
async def test_acreate_batch_prefers_only_the_batch_deployments_the_caller_may_use(
|
|
monkeypatch: pytest.MonkeyPatch, team_id: str | None, reached_host: str
|
|
):
|
|
"""A batch deployment owned by one team must not take a mixed group's batch creates away from callers
|
|
outside that team, who still reach the group's shared deployments."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_azure_sku_deployment("gpt-mixed", "chat-sku", "chat"),
|
|
_azure_sku_deployment("gpt-mixed", "batch-sku", "batch", team_id="team-a"),
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
routes: Final = {
|
|
host: respx_mock.post(host=f"{host}.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
for host in ("chat-sku", "batch-sku")
|
|
}
|
|
await router.acreate_batch(
|
|
model="gpt-mixed",
|
|
input_file_id="file-batch-input",
|
|
endpoint="/v1/chat/completions",
|
|
completion_window="24h",
|
|
metadata={"user_api_key_team_id": team_id},
|
|
)
|
|
|
|
assert {host: route.call_count for host, route in routes.items()} == {
|
|
host: int(host == reached_host) for host in routes
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acreate_batch_on_a_legacy_routing_strategy_keeps_shared_deployments_for_callers_outside_the_batch_team(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
"""usage-based-routing picks through the sync path, which keeps every team's deployments, so a batch deployment
|
|
owned by one team must not pull the creates of callers outside that team onto its credentials."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_azure_sku_deployment("gpt-mixed", "chat-sku", "chat"),
|
|
_azure_sku_deployment("gpt-mixed", "batch-sku", "batch", team_id="team-a"),
|
|
],
|
|
routing_strategy="usage-based-routing",
|
|
num_retries=0,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
chat_route: Final = respx_mock.post(host="chat-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
respx_mock.post(host="batch-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
for _ in range(30):
|
|
await router.acreate_batch(
|
|
model="gpt-mixed",
|
|
input_file_id="file-batch-input",
|
|
endpoint="/v1/chat/completions",
|
|
completion_window="24h",
|
|
metadata={"user_api_key_team_id": None},
|
|
)
|
|
|
|
assert chat_route.call_count > 0
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("build_deployment", "model_name"),
|
|
[
|
|
(functools.partial(_azure_sku_deployment, "gpt-mixed"), "gpt-mixed"),
|
|
(_azure_wildcard_deployment, "mixed-wild/gpt-5.4-mini"),
|
|
],
|
|
ids=["named-group", "wildcard-group"],
|
|
)
|
|
def test_credentials_for_a_mixed_group_name_keep_resolving_to_its_first_deployment(
|
|
build_deployment: Callable[[str, str], dict[str, object]], model_name: str
|
|
):
|
|
"""Model-encoded file, batch, and vector store ids name only the group, and every read resolves their
|
|
credentials by that name, so adding a batch deployment to a group must not move ids created earlier
|
|
onto it."""
|
|
router: Final = Router(model_list=[build_deployment("chat-sku", "chat"), build_deployment("batch-sku", "batch")])
|
|
|
|
credentials: Final = router.get_deployment_credentials_with_provider(model_id=model_name)
|
|
|
|
assert credentials is not None and credentials["api_base"] == "https://chat-sku.openai.azure.com"
|
|
|
|
|
|
_AZURE_CHAT_SKU_REJECTS_BATCH: Final = httpx.Response(
|
|
400,
|
|
json={
|
|
"error": {
|
|
"code": "invalid_deployment_type",
|
|
"message": "The current deployment is not supported for batch jobs",
|
|
}
|
|
},
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acreate_batch_on_a_mixed_group_reaches_chat_as_on_main_while_its_batch_deployment_cools_down(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
"""While the batch deployment cools down, a create goes where main sends it, so the caller keeps getting
|
|
Azure's 400 instead of a router 429 that main never returned for this request."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_azure_sku_deployment("gpt-mixed", "chat-sku", "chat"),
|
|
_azure_sku_deployment("gpt-mixed", "batch-sku", "batch"),
|
|
],
|
|
num_retries=0,
|
|
)
|
|
_cool_down(router, *(d["model_info"]["id"] for d in router.model_list if d["model_info"]["mode"] == "batch"))
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
chat_route: Final = respx_mock.post(host="chat-sku.openai.azure.com").mock(
|
|
return_value=_AZURE_CHAT_SKU_REJECTS_BATCH
|
|
)
|
|
with pytest.raises(openai.BadRequestError):
|
|
await _create_batches(router, "gpt-mixed", attempts=1)
|
|
|
|
assert chat_route.call_count == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acreate_batch_with_tags_that_match_only_the_chat_deployment_reaches_it_as_on_main(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
"""Tag routing runs before the batch filter, so tags that leave only the chat deployment get Azure's 400 as
|
|
on main instead of a router error saying no deployment matches the tags."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
chat: Final = _azure_sku_deployment("gpt-mixed", "chat-sku", "chat")
|
|
batch: Final = _azure_sku_deployment("gpt-mixed", "batch-sku", "batch")
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{**chat, "litellm_params": {**chat["litellm_params"], "tags": ["chat-pin"]}},
|
|
{**batch, "litellm_params": {**batch["litellm_params"], "tags": ["batch-pin"]}},
|
|
],
|
|
enable_tag_filtering=True,
|
|
num_retries=0,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
chat_route: Final = respx_mock.post(host="chat-sku.openai.azure.com").mock(
|
|
return_value=_AZURE_CHAT_SKU_REJECTS_BATCH
|
|
)
|
|
batch_route: Final = respx_mock.post(host="batch-sku.openai.azure.com").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
with pytest.raises(openai.BadRequestError):
|
|
await router.acreate_batch(
|
|
model="gpt-mixed",
|
|
input_file_id="file-batch-input",
|
|
endpoint="/v1/chat/completions",
|
|
completion_window="24h",
|
|
litellm_metadata={"tags": ["chat-pin"]},
|
|
)
|
|
await router.acreate_batch(
|
|
model="gpt-mixed",
|
|
input_file_id="file-batch-input",
|
|
endpoint="/v1/chat/completions",
|
|
completion_window="24h",
|
|
litellm_metadata={"tags": ["batch-pin"]},
|
|
)
|
|
|
|
assert (chat_route.call_count, batch_route.call_count) == (1, 1)
|
|
|
|
|
|
def _openai_deployment(model_group: str, host: str, mode: str, **model_info: object) -> dict[str, object]:
|
|
return {
|
|
"model_name": model_group,
|
|
"litellm_params": {"model": "openai/gpt-5.4-mini", "api_base": f"https://{host}/v1", "api_key": "dummy-key"},
|
|
"model_info": {"mode": mode, **model_info},
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acreate_batch_on_an_openai_group_falls_back_to_chat_while_its_batch_deployment_cools_down(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
"""OpenAI runs batches on the deployment that serves chat, so only Azure groups skip chat deployments for
|
|
batch creates and an OpenAI group keeps serving them while its batch deployment cools down."""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_openai_deployment("gpt-mixed", "chat.openai.test", "chat"),
|
|
_openai_deployment("gpt-mixed", "batch.openai.test", "batch"),
|
|
],
|
|
num_retries=0,
|
|
)
|
|
_cool_down(router, *(d["model_info"]["id"] for d in router.model_list if d["model_info"]["mode"] == "batch"))
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
chat_route: Final = respx_mock.post(host="chat.openai.test").mock(
|
|
return_value=httpx.Response(200, json=_AZURE_BATCH_CREATED)
|
|
)
|
|
await _create_batches(router, "gpt-mixed", attempts=5)
|
|
|
|
assert chat_route.call_count == 5
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acreate_batch_request_bedrock_tags_override_deployment_tags():
|
|
import httpx
|
|
|
|
from litellm.llms.bedrock.common_utils import CommonBatchFilesUtils
|
|
|
|
deployment_tags = [{"key": "application", "value": "config-level"}]
|
|
request_tags = [{"key": "application", "value": "request-level"}]
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-batch-model",
|
|
"litellm_params": {
|
|
"model": "bedrock/us.anthropic.claude-sonnet-5",
|
|
"aws_batch_role_arn": "arn:aws:iam::123:role/batch-role",
|
|
"aws_region_name": "us-west-2",
|
|
"bedrock_tags": deployment_tags,
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
def fake_response():
|
|
return httpx.Response(
|
|
status_code=200,
|
|
json={
|
|
"jobArn": "arn:aws:bedrock:us-west-2:123:model-invocation-job/abc1234567",
|
|
"status": "Submitted",
|
|
},
|
|
)
|
|
|
|
mock_client = MagicMock()
|
|
mock_client.post = AsyncMock(side_effect=lambda *args, **kwargs: fake_response())
|
|
|
|
with (
|
|
patch.object(
|
|
CommonBatchFilesUtils,
|
|
"sign_aws_request",
|
|
return_value=({"Authorization": "signed"}, b"{}"),
|
|
) as mock_sign,
|
|
patch(
|
|
"litellm.llms.custom_httpx.llm_http_handler.get_async_httpx_client",
|
|
return_value=mock_client,
|
|
),
|
|
):
|
|
await router.acreate_batch(
|
|
model="bedrock-batch-model",
|
|
input_file_id="s3://bucket/input.jsonl",
|
|
endpoint="/v1/chat/completions",
|
|
completion_window="24h",
|
|
)
|
|
assert mock_sign.call_args.kwargs["data"]["tags"] == deployment_tags
|
|
|
|
await router.acreate_batch(
|
|
model="bedrock-batch-model",
|
|
input_file_id="s3://bucket/input.jsonl",
|
|
endpoint="/v1/chat/completions",
|
|
completion_window="24h",
|
|
bedrock_tags=request_tags,
|
|
)
|
|
assert mock_sign.call_args.kwargs["data"]["tags"] == request_tags
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_avector_store_search_injects_router():
|
|
"""
|
|
Regression: router.avector_store_search must pass the router down to the
|
|
SDK search call so provider transforms can resolve router-managed
|
|
embedding models (e.g. S3 Vectors query embeddings).
|
|
"""
|
|
from litellm.types.vector_stores import VectorStoreSearchResponse
|
|
|
|
expected_response = VectorStoreSearchResponse(object="vector_store.search_results.page", search_query="q", data=[])
|
|
mock_asearch = AsyncMock(return_value=expected_response)
|
|
# Router.__init__ binds asearch via a local import, so patch the module
|
|
# attribute before constructing the Router.
|
|
with patch(
|
|
"litellm.vector_stores.main.asearch", new=mock_asearch
|
|
): # test-quality-ok: the SDK call is the only place the injected router kwarg is observable
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo", "api_key": "test-key"},
|
|
}
|
|
]
|
|
)
|
|
search_response = await router.avector_store_search(
|
|
vector_store_id="v", query="q", custom_llm_provider="s3_vectors"
|
|
)
|
|
|
|
assert search_response is expected_response
|
|
mock_asearch.assert_awaited_once()
|
|
assert mock_asearch.await_args.kwargs["router"] is router
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_avector_store_create_does_not_inject_router():
|
|
"""The router injection is gated on the search call type: the create path
|
|
must keep calling the SDK without a router kwarg."""
|
|
expected_response = {"id": "vs_1", "object": "vector_store"}
|
|
mock_acreate = AsyncMock(return_value=expected_response)
|
|
# avector_store_create(model=None) resolves acreate via a local import at
|
|
# call time, so patching after Router construction works here.
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo", "api_key": "test-key"},
|
|
}
|
|
]
|
|
)
|
|
with patch(
|
|
"litellm.vector_stores.main.acreate", new=mock_acreate
|
|
): # test-quality-ok: the SDK call is the only place a leaked router kwarg would surface
|
|
create_response = await router.avector_store_create(model=None, custom_llm_provider="openai")
|
|
|
|
assert create_response is expected_response
|
|
mock_acreate.assert_awaited_once()
|
|
assert "router" not in mock_acreate.await_args.kwargs
|
|
|
|
|
|
def test_vector_store_search_injects_router():
|
|
"""
|
|
Sync parity for the router injection: router.vector_store_search must pass
|
|
the router down to the SDK search call so provider transforms can resolve
|
|
router-managed embedding models, same as avector_store_search.
|
|
"""
|
|
from litellm.types.vector_stores import VectorStoreSearchResponse
|
|
|
|
expected_response = VectorStoreSearchResponse(object="vector_store.search_results.page", search_query="q", data=[])
|
|
mock_search = MagicMock(return_value=expected_response)
|
|
# Router.__init__ binds search via a local import, so patch the module
|
|
# attribute before constructing the Router.
|
|
with patch(
|
|
"litellm.vector_stores.main.search", new=mock_search
|
|
): # test-quality-ok: the SDK call is the only place the injected router kwarg is observable
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo", "api_key": "test-key"},
|
|
}
|
|
]
|
|
)
|
|
search_response = router.vector_store_search(vector_store_id="v", query="q", custom_llm_provider="s3_vectors")
|
|
|
|
assert search_response is expected_response
|
|
mock_search.assert_called_once()
|
|
assert mock_search.call_args.kwargs["router"] is router
|
|
assert mock_search.call_args.kwargs["custom_llm_provider"] == "s3_vectors"
|
|
|
|
|
|
def test_vector_store_create_does_not_inject_router():
|
|
"""The sync create path must keep calling the SDK without a router kwarg."""
|
|
expected_response = {"id": "vs_1", "object": "vector_store"}
|
|
mock_create = MagicMock(return_value=expected_response)
|
|
# Router.__init__ binds create via a local import, so patch the module
|
|
# attribute before constructing the Router.
|
|
with patch(
|
|
"litellm.vector_stores.main.create", new=mock_create
|
|
): # test-quality-ok: the SDK call is the only place a leaked router kwarg would surface
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo", "api_key": "test-key"},
|
|
}
|
|
]
|
|
)
|
|
create_response = router.vector_store_create(custom_llm_provider="openai")
|
|
|
|
assert create_response is expected_response
|
|
mock_create.assert_called_once()
|
|
assert "router" not in mock_create.call_args.kwargs
|
|
|
|
|
|
class TestPreRoutingStrategyRegistryLifecycle:
|
|
"""
|
|
Regression tests: a deployment leaving the model_list must release the
|
|
pre-routing strategy slot it holds in `auto_routers` / `complexity_routers` /
|
|
`adaptive_routers` / `quality_routers`.
|
|
|
|
Before this fix, editing an auto-router-family model (a UI save, which reaches
|
|
every other pod as an `upsert_deployment` from the periodic DB reload) popped
|
|
the deployment out of the model_list and then failed to re-add it: registration
|
|
raised "already exists" against the stale registry entry, and
|
|
`ignore_invalid_deployments=True` swallowed the error. The router vanished from
|
|
the Models page and stayed gone until a proxy restart, while the DB row and the
|
|
"saved successfully" response both looked fine.
|
|
"""
|
|
|
|
@staticmethod
|
|
def _complexity_router_params(default_model: str, tags=None) -> dict:
|
|
return {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_config": {"tiers": {"SIMPLE": "gpt-4o-mini", "MEDIUM": "gpt-4o", "COMPLEX": "gpt-4o"}},
|
|
"complexity_router_default_model": default_model,
|
|
**({"tags": tags} if tags else {}),
|
|
}
|
|
|
|
@classmethod
|
|
def _router_with_complexity_router(cls, default_model: str = "gpt-4o") -> "litellm.Router":
|
|
return litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "gpt-4o"}},
|
|
{"model_name": "gpt-4o-mini", "litellm_params": {"model": "gpt-4o-mini"}},
|
|
{
|
|
"model_name": "smart-router",
|
|
"litellm_params": cls._complexity_router_params(default_model),
|
|
"model_info": {"id": "router-1", "db_model": True},
|
|
},
|
|
],
|
|
ignore_invalid_deployments=True,
|
|
)
|
|
|
|
@staticmethod
|
|
def _model_names(router: "litellm.Router") -> list:
|
|
return [model["model_name"] for model in router.model_list]
|
|
|
|
def test_upsert_of_edited_router_keeps_it_routable(self):
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
router = self._router_with_complexity_router()
|
|
|
|
router.upsert_deployment(
|
|
deployment=Deployment(
|
|
model_name="smart-router",
|
|
litellm_params=LiteLLM_Params(**self._complexity_router_params("gpt-4o-mini")),
|
|
model_info=ModelInfo(id="router-1", db_model=True),
|
|
)
|
|
)
|
|
|
|
assert "smart-router" in self._model_names(router)
|
|
registered = router.complexity_routers["smart-router"]
|
|
assert len(registered) == 1
|
|
# the surviving strategy is the edited one, not the pre-edit leftover
|
|
assert registered[0].strategy.config.default_model == "gpt-4o-mini"
|
|
|
|
def test_unchanged_upsert_leaves_router_untouched(self):
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
router = self._router_with_complexity_router()
|
|
strategy_before = router.complexity_routers["smart-router"][0].strategy
|
|
|
|
for _ in range(3):
|
|
router.upsert_deployment(
|
|
deployment=Deployment(
|
|
model_name="smart-router",
|
|
litellm_params=LiteLLM_Params(**self._complexity_router_params("gpt-4o")),
|
|
model_info=ModelInfo(id="router-1", db_model=True),
|
|
)
|
|
)
|
|
|
|
assert "smart-router" in self._model_names(router)
|
|
assert router.complexity_routers["smart-router"][0].strategy is strategy_before
|
|
|
|
def test_delete_frees_the_name_for_a_new_router(self):
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
router = self._router_with_complexity_router()
|
|
|
|
router.delete_deployment(id="router-1")
|
|
assert "smart-router" not in router.complexity_routers
|
|
|
|
router.add_deployment(
|
|
deployment=Deployment(
|
|
model_name="smart-router",
|
|
litellm_params=LiteLLM_Params(**self._complexity_router_params("gpt-4o-mini")),
|
|
model_info=ModelInfo(id="router-2", db_model=True),
|
|
)
|
|
)
|
|
|
|
assert "smart-router" in self._model_names(router)
|
|
assert router.complexity_routers["smart-router"][0].strategy.config.default_model == "gpt-4o-mini"
|
|
|
|
def test_delete_only_frees_the_matching_tag_slot(self):
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "gpt-4o"}},
|
|
{"model_name": "gpt-4o-mini", "litellm_params": {"model": "gpt-4o-mini"}},
|
|
{
|
|
"model_name": "shared-router",
|
|
"litellm_params": self._complexity_router_params("gpt-4o", tags=["team-a"]),
|
|
"model_info": {"id": "router-a"},
|
|
},
|
|
{
|
|
"model_name": "shared-router",
|
|
"litellm_params": self._complexity_router_params("gpt-4o-mini", tags=["team-b"]),
|
|
"model_info": {"id": "router-b"},
|
|
},
|
|
],
|
|
ignore_invalid_deployments=True,
|
|
)
|
|
assert len(router.complexity_routers["shared-router"]) == 2
|
|
|
|
router.delete_deployment(id="router-a")
|
|
|
|
remaining = router.complexity_routers["shared-router"]
|
|
assert len(remaining) == 1
|
|
assert remaining[0].tags == ("team-b",)
|
|
|
|
def test_delete_of_regular_model_preserves_router_sharing_its_name(self):
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "gpt-4o"}},
|
|
{"model_name": "gpt-4o-mini", "litellm_params": {"model": "gpt-4o-mini"}},
|
|
{
|
|
"model_name": "shared-name",
|
|
"litellm_params": self._complexity_router_params("gpt-4o"),
|
|
"model_info": {"id": "router-1"},
|
|
},
|
|
{
|
|
"model_name": "shared-name",
|
|
"litellm_params": {"model": "openai/gpt-4o"},
|
|
"model_info": {"id": "regular-1"},
|
|
},
|
|
],
|
|
ignore_invalid_deployments=True,
|
|
)
|
|
strategy = router.complexity_routers["shared-name"][0].strategy
|
|
|
|
router.delete_deployment(id="regular-1")
|
|
|
|
assert router.complexity_routers["shared-name"][0].strategy is strategy
|
|
|
|
def test_upsert_of_edited_adaptive_router_rebuilds_it(self):
|
|
"""Adaptive routers are built by set_model_list()'s deferred pass, not by
|
|
add_deployment(), so releasing the slot on edit must be paired with a rebuild -
|
|
otherwise the edit silently turns adaptive routing off."""
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
def adaptive_params(available_models: list) -> dict:
|
|
return {
|
|
"model": "auto_router/adaptive_router",
|
|
"adaptive_router_config": {"available_models": available_models},
|
|
}
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "openai/gpt-4o"}},
|
|
{"model_name": "gpt-4o-mini", "litellm_params": {"model": "openai/gpt-4o-mini"}},
|
|
{
|
|
"model_name": "adaptive-router",
|
|
"litellm_params": adaptive_params(["gpt-4o-mini"]),
|
|
"model_info": {"id": "router-1", "db_model": True},
|
|
},
|
|
],
|
|
ignore_invalid_deployments=True,
|
|
)
|
|
assert "adaptive-router" in router.adaptive_routers
|
|
|
|
router.upsert_deployment(
|
|
deployment=Deployment(
|
|
model_name="adaptive-router",
|
|
litellm_params=LiteLLM_Params(**adaptive_params(["gpt-4o", "gpt-4o-mini"])),
|
|
model_info=ModelInfo(id="router-1", db_model=True),
|
|
)
|
|
)
|
|
|
|
assert "adaptive-router" in self._model_names(router)
|
|
registered = router.adaptive_routers["adaptive-router"]
|
|
assert len(registered) == 1
|
|
assert set(registered[0].strategy.config.available_models) == {"gpt-4o", "gpt-4o-mini"}
|
|
|
|
def test_delete_of_adaptive_enabled_complexity_router_frees_both_registries(self):
|
|
"""A complexity router with adaptive set is registered in BOTH complexity_routers
|
|
and adaptive_routers under the same (model_name, tags). Releasing only the first
|
|
match leaves the adaptive strategy live, so a deleted alias stays routable and its
|
|
post-call hook keeps recording."""
|
|
import litellm as litellm_module
|
|
from litellm.router_strategy.adaptive_router.hooks import AdaptiveRouterPostCallHook
|
|
|
|
params = {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_config": {
|
|
"tiers": {"SIMPLE": "gpt-4o-mini", "MEDIUM": "gpt-4o"},
|
|
"adaptive": True,
|
|
},
|
|
"complexity_router_default_model": "gpt-4o",
|
|
}
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "openai/gpt-4o"}},
|
|
{"model_name": "gpt-4o-mini", "litellm_params": {"model": "openai/gpt-4o-mini"}},
|
|
{
|
|
"model_name": "hybrid-router",
|
|
"litellm_params": params,
|
|
"model_info": {"id": "router-1", "db_model": True},
|
|
},
|
|
],
|
|
ignore_invalid_deployments=True,
|
|
)
|
|
assert "hybrid-router" in router.complexity_routers
|
|
assert "hybrid-router" in router.adaptive_routers
|
|
hooks = litellm_module.logging_callback_manager.get_custom_loggers_for_type(AdaptiveRouterPostCallHook)
|
|
assert len(hooks) == 1
|
|
|
|
router.delete_deployment(id="router-1")
|
|
|
|
assert "hybrid-router" not in router.complexity_routers
|
|
assert "hybrid-router" not in router.adaptive_routers
|
|
remaining_hooks = litellm_module.logging_callback_manager.get_custom_loggers_for_type(
|
|
AdaptiveRouterPostCallHook
|
|
)
|
|
assert remaining_hooks == []
|
|
|
|
def test_upsert_of_edited_quality_router_keeps_it_routable(self):
|
|
"""_unregister_pre_routing_strategy_for_deployment dispatches on four prefixes;
|
|
quality_router is one of them and would otherwise go unexercised."""
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
def quality_params(default_model: str) -> dict:
|
|
return {
|
|
"model": "auto_router/quality_router",
|
|
"quality_router_default_model": default_model,
|
|
}
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "openai/gpt-4o"}},
|
|
{"model_name": "gpt-4o-mini", "litellm_params": {"model": "openai/gpt-4o-mini"}},
|
|
{
|
|
"model_name": "quality-router",
|
|
"litellm_params": quality_params("gpt-4o"),
|
|
"model_info": {"id": "router-1", "db_model": True},
|
|
},
|
|
],
|
|
ignore_invalid_deployments=True,
|
|
)
|
|
assert "quality-router" in router.quality_routers
|
|
|
|
router.upsert_deployment(
|
|
deployment=Deployment(
|
|
model_name="quality-router",
|
|
litellm_params=LiteLLM_Params(**quality_params("gpt-4o-mini")),
|
|
model_info=ModelInfo(id="router-1", db_model=True),
|
|
)
|
|
)
|
|
|
|
assert "quality-router" in self._model_names(router)
|
|
registered = router.quality_routers["quality-router"]
|
|
assert len(registered) == 1
|
|
assert registered[0].strategy.config.default_model == "gpt-4o-mini"
|
|
|
|
@staticmethod
|
|
def _hybrid_router_params(tiers: dict) -> dict:
|
|
return {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_config": {"tiers": tiers, "adaptive": True},
|
|
"complexity_router_default_model": "gpt-4o",
|
|
}
|
|
|
|
@classmethod
|
|
def _router_with_hybrid_router(cls) -> "litellm.Router":
|
|
return litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "openai/gpt-4o"}},
|
|
{"model_name": "gpt-4o-mini", "litellm_params": {"model": "openai/gpt-4o-mini"}},
|
|
{
|
|
"model_name": "hybrid-router",
|
|
"litellm_params": cls._hybrid_router_params({"SIMPLE": "gpt-4o-mini", "MEDIUM": "gpt-4o"}),
|
|
"model_info": {"id": "router-1", "db_model": True},
|
|
},
|
|
],
|
|
ignore_invalid_deployments=True,
|
|
)
|
|
|
|
def test_upsert_of_edited_hybrid_complexity_router_relinks_adaptive(self):
|
|
"""Editing an adaptive-enabled complexity router releases its adaptive companion
|
|
along with the complexity slot; the finalize re-run must fire for it (not just for
|
|
`auto_router/adaptive_router` deployments) or the rebuilt complexity router keeps
|
|
routing while bandit recording, DB persistence and /adaptive_router/state all
|
|
silently stop until the next full reload."""
|
|
import litellm as litellm_module
|
|
from litellm.router_strategy.adaptive_router.hooks import AdaptiveRouterPostCallHook
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
router = self._router_with_hybrid_router()
|
|
assert "hybrid-router" in router.adaptive_routers
|
|
|
|
router.upsert_deployment(
|
|
deployment=Deployment(
|
|
model_name="hybrid-router",
|
|
litellm_params=LiteLLM_Params(
|
|
**self._hybrid_router_params({"SIMPLE": "gpt-4o-mini", "MEDIUM": "gpt-4o", "COMPLEX": "gpt-4o"})
|
|
),
|
|
model_info=ModelInfo(id="router-1", db_model=True),
|
|
)
|
|
)
|
|
|
|
assert "hybrid-router" in self._model_names(router)
|
|
assert "hybrid-router" in router.complexity_routers
|
|
assert "hybrid-router" in router.adaptive_routers
|
|
rebuilt = router.complexity_routers["hybrid-router"][0].strategy
|
|
assert router.adaptive_routers["hybrid-router"][0].strategy is rebuilt.adaptive_router
|
|
hooks = litellm_module.logging_callback_manager.get_custom_loggers_for_type(AdaptiveRouterPostCallHook)
|
|
assert len(hooks) == 1
|
|
|
|
def test_upsert_turning_adaptive_on_builds_the_companion(self):
|
|
"""An edit that flips `adaptive: true` on an existing complexity router must
|
|
register the companion immediately; neither side of the old prefix-only gate
|
|
matches a complexity deployment, so the flip was a silent no-op until restart."""
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
router = self._router_with_complexity_router()
|
|
assert "smart-router" not in router.adaptive_routers
|
|
|
|
params = self._complexity_router_params("gpt-4o")
|
|
params["complexity_router_config"] = {**params["complexity_router_config"], "adaptive": True}
|
|
router.upsert_deployment(
|
|
deployment=Deployment(
|
|
model_name="smart-router",
|
|
litellm_params=LiteLLM_Params(**params),
|
|
model_info=ModelInfo(id="router-1", db_model=True),
|
|
)
|
|
)
|
|
|
|
assert "smart-router" in router.adaptive_routers
|
|
|
|
def test_sync_adaptive_router_hooks_keeps_one_hook_per_registered_router(self):
|
|
"""Re-syncing must replace, not accumulate: a duplicated hook double-fires
|
|
bandit signal recording for every request."""
|
|
import litellm as litellm_module
|
|
from litellm.router_strategy.adaptive_router.hooks import AdaptiveRouterPostCallHook
|
|
|
|
router = self._router_with_hybrid_router()
|
|
|
|
router._sync_adaptive_router_hooks()
|
|
router._sync_adaptive_router_hooks()
|
|
|
|
hooks = litellm_module.logging_callback_manager.get_custom_loggers_for_type(AdaptiveRouterPostCallHook)
|
|
assert len(hooks) == 1
|
|
|
|
|
|
def test_model_info_is_active_for_environment_matrix(monkeypatch):
|
|
"""The model-write endpoints consult this predicate to tell a deliberately
|
|
environment-inactive model from one dropped by a failed reload; the Router's own
|
|
deployment gate delegates to it, so the two can never diverge."""
|
|
from litellm.router import model_info_is_active_for_environment
|
|
|
|
assert model_info_is_active_for_environment(model_info=None) is True
|
|
assert model_info_is_active_for_environment(model_info={"id": "m1"}) is True
|
|
assert model_info_is_active_for_environment(model_info={"supported_environments": None}) is True
|
|
|
|
monkeypatch.setenv("LITELLM_ENVIRONMENT", "development")
|
|
assert model_info_is_active_for_environment(model_info={"supported_environments": ["development"]}) is True
|
|
assert model_info_is_active_for_environment(model_info={"supported_environments": ["production"]}) is False
|
|
|
|
monkeypatch.delenv("LITELLM_ENVIRONMENT")
|
|
with pytest.raises(ValueError, match="LITELLM_ENVIRONMENT"):
|
|
model_info_is_active_for_environment(model_info={"supported_environments": ["production"]})
|
|
|
|
|
|
def test_pre_call_checks_keeps_deployment_when_provider_is_unresolvable(monkeypatch):
|
|
"""
|
|
Pre-call checks filter deployments; they must never be the thing that fails
|
|
a request. A deployment whose provider cannot be resolved simply skips the
|
|
supported-params check instead of raising out of deployment selection.
|
|
"""
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "custom-alias",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
)
|
|
|
|
def _raise_no_provider(**kwargs):
|
|
raise litellm.BadRequestError(
|
|
message="LLM Provider NOT provided.",
|
|
model="custom-alias",
|
|
llm_provider="",
|
|
)
|
|
|
|
monkeypatch.setattr(litellm, "get_llm_provider", _raise_no_provider)
|
|
|
|
deployments = [
|
|
{
|
|
"litellm_params": {"model": "some-unresolvable-model"},
|
|
"model_info": {"id": "d1"},
|
|
}
|
|
]
|
|
result = router._pre_call_checks(
|
|
model="custom-alias",
|
|
healthy_deployments=deployments,
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
request_kwargs={},
|
|
)
|
|
|
|
assert len(result) == 1
|
|
|
|
|
|
class TestUpsertDeploymentRollback:
|
|
"""
|
|
Regression tests: `upsert_deployment` pops the previous deployment before
|
|
re-adding the edited one. When the re-add raises under
|
|
`ignore_invalid_deployments=True`, the pop must be rolled back so this pod
|
|
keeps serving the previous configuration instead of silently dropping a live
|
|
deployment (the "Error upserting deployment" drop behind the access-group
|
|
reload 500 in the 2-replica e2e suite).
|
|
"""
|
|
|
|
def test_failed_upsert_keeps_previous_deployment_serving(self):
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "prod-model",
|
|
"litellm_params": {"model": "gpt-4o", "api_key": "sk-old"},
|
|
"model_info": {"id": "prod-1", "db_model": True},
|
|
}
|
|
],
|
|
ignore_invalid_deployments=True,
|
|
)
|
|
|
|
result = router.upsert_deployment(
|
|
deployment=Deployment(
|
|
model_name="prod-model",
|
|
litellm_params=LiteLLM_Params(model="auto_router/broken"),
|
|
model_info=ModelInfo(id="prod-1", db_model=True),
|
|
)
|
|
)
|
|
|
|
assert result is None
|
|
restored = router.get_deployment(model_id="prod-1")
|
|
assert restored is not None
|
|
assert restored.litellm_params.model == "gpt-4o"
|
|
assert [model["model_name"] for model in router.model_list] == ["prod-model"]
|
|
|
|
def test_failed_fresh_add_returns_none_without_restore(self):
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
router = litellm.Router(model_list=[], ignore_invalid_deployments=True)
|
|
|
|
result = router.upsert_deployment(
|
|
deployment=Deployment(
|
|
model_name="fresh-router",
|
|
litellm_params=LiteLLM_Params(model="auto_router/broken"),
|
|
model_info=ModelInfo(id="fresh-1", db_model=True),
|
|
)
|
|
)
|
|
|
|
assert result is None
|
|
assert router.get_deployment(model_id="fresh-1") is None
|
|
assert router.model_list == []
|
|
|
|
|
|
class TestUpsertDeploymentRename:
|
|
"""
|
|
Issue #38360: renaming a model wrote the new `model_name` to the db, but the reload's
|
|
`upsert_deployment` compared only `litellm_params` and `model_info`. A rename with no
|
|
other edit therefore compared equal and the router kept the old name until a restart,
|
|
so `/model/info` and `/v1/models` served the stale name and the new one was unroutable.
|
|
"""
|
|
|
|
@staticmethod
|
|
def _router() -> "litellm.Router":
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "old-name",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-test"},
|
|
"model_info": {"id": "rename-1", "db_model": True},
|
|
}
|
|
]
|
|
)
|
|
|
|
@staticmethod
|
|
def _deployment(model_name: str, tpm: int | None = None):
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
return Deployment(
|
|
model_name=model_name,
|
|
litellm_params=LiteLLM_Params(model="openai/gpt-4o", api_key="sk-test", tpm=tpm),
|
|
model_info=ModelInfo(id="rename-1", db_model=True),
|
|
)
|
|
|
|
def test_rename_only_updates_the_router(self):
|
|
router = self._router()
|
|
|
|
assert router.upsert_deployment(deployment=self._deployment("new-name")) is not None
|
|
|
|
assert [model["model_name"] for model in router.model_list] == ["new-name"]
|
|
renamed = router.get_deployment(model_id="rename-1")
|
|
assert renamed is not None
|
|
assert renamed.model_name == "new-name"
|
|
|
|
def test_rename_only_makes_the_new_name_routable(self):
|
|
router = self._router()
|
|
|
|
router.upsert_deployment(deployment=self._deployment("new-name"))
|
|
|
|
assert router.get_model_ids(model_name="new-name") == ["rename-1"]
|
|
assert router.get_model_ids(model_name="old-name") == []
|
|
|
|
def test_rename_alongside_another_edit_still_updates(self):
|
|
router = self._router()
|
|
|
|
router.upsert_deployment(deployment=self._deployment("new-name", tpm=1234))
|
|
|
|
assert router.get_model_ids(model_name="new-name") == ["rename-1"]
|
|
renamed = router.get_deployment(model_id="rename-1")
|
|
assert renamed is not None
|
|
assert renamed.litellm_params.tpm == 1234
|
|
|
|
def test_unchanged_deployment_is_still_a_no_op(self):
|
|
router = self._router()
|
|
|
|
assert router.upsert_deployment(deployment=self._deployment("old-name")) is None
|
|
assert [model["model_name"] for model in router.model_list] == ["old-name"]
|
|
|
|
|
|
class TestConsumedRequestTagsStamp:
|
|
"""Issue #36621: when a request's tags select a tagged pre-routing strategy, those
|
|
tags are consumed by the selection; the hook must stamp the rewritten model group so
|
|
tag filtering skips request-body tags there, and must clear the stamp on every
|
|
re-entry (fallbacks reuse the same request_kwargs) so it cannot leak elsewhere."""
|
|
|
|
class _RewriteStrategy:
|
|
def __init__(self, rewrite_to: str):
|
|
self.rewrite_to = rewrite_to
|
|
|
|
async def async_pre_routing_hook(
|
|
self, model, request_kwargs, messages=None, input=None, specific_deployment=False, call_type=None
|
|
):
|
|
from litellm.types.router import PreRoutingHookResponse
|
|
|
|
return PreRoutingHookResponse(model=self.rewrite_to, messages=messages)
|
|
|
|
@classmethod
|
|
def _router(cls, marker_tags=("route",)) -> "litellm.Router":
|
|
from litellm.types.router import TaggedPreRoutingStrategy
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt4o", "litellm_params": {"model": "openai/gpt-4o"}},
|
|
{"model_name": "gemini-flash", "litellm_params": {"model": "gemini/gemini-3.6-flash"}},
|
|
],
|
|
enable_tag_filtering=True,
|
|
)
|
|
router.auto_routers = {
|
|
"gpt4o": [TaggedPreRoutingStrategy(tags=marker_tags, strategy=cls._RewriteStrategy("gemini-flash"))]
|
|
}
|
|
return router
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_stamps_the_rewritten_group_when_request_tags_selected_the_router(self):
|
|
from litellm.constants import CONSUMED_REQUEST_TAGS_METADATA_KEY
|
|
from litellm.types.router import ConsumedRequestTagsStamp
|
|
|
|
router = self._router()
|
|
request_kwargs = {"metadata": {"tags": ["route"]}}
|
|
|
|
await router.async_pre_routing_hook(model="gpt4o", request_kwargs=request_kwargs)
|
|
|
|
assert request_kwargs["metadata"][CONSUMED_REQUEST_TAGS_METADATA_KEY] == ConsumedRequestTagsStamp(
|
|
model_group="gemini-flash", tags=("route",)
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_stamps_into_litellm_metadata_when_the_request_uses_that_bucket(self):
|
|
from litellm.constants import CONSUMED_REQUEST_TAGS_METADATA_KEY
|
|
from litellm.types.router import ConsumedRequestTagsStamp
|
|
|
|
router = self._router()
|
|
request_kwargs = {"litellm_metadata": {"tags": ["route"]}}
|
|
|
|
await router.async_pre_routing_hook(model="gpt4o", request_kwargs=request_kwargs)
|
|
|
|
assert request_kwargs["litellm_metadata"][CONSUMED_REQUEST_TAGS_METADATA_KEY] == ConsumedRequestTagsStamp(
|
|
model_group="gemini-flash", tags=("route",)
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_reentry_with_a_plain_group_clears_the_stale_stamp(self):
|
|
from litellm.constants import CONSUMED_REQUEST_TAGS_METADATA_KEY
|
|
|
|
router = self._router()
|
|
request_kwargs = {"metadata": {"tags": ["route"]}}
|
|
|
|
await router.async_pre_routing_hook(model="gpt4o", request_kwargs=request_kwargs)
|
|
await router.async_pre_routing_hook(model="gemini-flash", request_kwargs=request_kwargs)
|
|
|
|
assert CONSUMED_REQUEST_TAGS_METADATA_KEY not in request_kwargs["metadata"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_stamp_when_the_request_is_untagged(self):
|
|
from litellm.constants import CONSUMED_REQUEST_TAGS_METADATA_KEY
|
|
|
|
router = self._router()
|
|
request_kwargs = {"metadata": {}}
|
|
|
|
await router.async_pre_routing_hook(model="gpt4o", request_kwargs=request_kwargs)
|
|
|
|
assert CONSUMED_REQUEST_TAGS_METADATA_KEY not in request_kwargs["metadata"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_stamp_when_the_selected_strategy_carries_no_tags(self):
|
|
from litellm.constants import CONSUMED_REQUEST_TAGS_METADATA_KEY
|
|
|
|
router = self._router(marker_tags=())
|
|
request_kwargs = {"metadata": {"tags": ["route"]}}
|
|
|
|
await router.async_pre_routing_hook(model="gpt4o", request_kwargs=request_kwargs)
|
|
|
|
assert CONSUMED_REQUEST_TAGS_METADATA_KEY not in request_kwargs["metadata"]
|
|
|
|
|
|
class TestClaudeCodeSubagentSessionRouterBinding:
|
|
class _RewriteStrategy:
|
|
def __init__(self, routed_model: str = "cheap-model") -> None:
|
|
self.routed_model = routed_model
|
|
|
|
async def async_pre_routing_hook(
|
|
self, model, request_kwargs, messages=None, input=None, specific_deployment=False, call_type=None
|
|
):
|
|
from litellm.types.router import PreRoutingHookResponse
|
|
|
|
return PreRoutingHookResponse(
|
|
model=self.routed_model,
|
|
messages=messages,
|
|
routing_decision={
|
|
"router_model_name": "smart-router",
|
|
"router_type": "complexity",
|
|
"routed_model": self.routed_model,
|
|
"cause": "heuristic_scorer",
|
|
},
|
|
)
|
|
|
|
@classmethod
|
|
def _router(
|
|
cls,
|
|
cheap_response: str = "cheap response",
|
|
fallbacks: list[dict[str, list[str]]] | None = None,
|
|
) -> "litellm.Router":
|
|
from litellm.types.router import TaggedPreRoutingStrategy
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "cheap-model",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "mock_response": cheap_response},
|
|
},
|
|
{
|
|
"model_name": "expensive-model",
|
|
"litellm_params": {"model": "openai/gpt-4o", "mock_response": "expensive response"},
|
|
},
|
|
],
|
|
fallbacks=fallbacks,
|
|
num_retries=0,
|
|
)
|
|
router.complexity_routers = {
|
|
"smart-router": (TaggedPreRoutingStrategy(tags=(), strategy=cls._RewriteStrategy()),),
|
|
"premium-router": (TaggedPreRoutingStrategy(tags=(), strategy=cls._RewriteStrategy("expensive-model")),),
|
|
}
|
|
return router
|
|
|
|
@staticmethod
|
|
def _request_kwargs(
|
|
*,
|
|
key_hash: str = "key-hash-a",
|
|
app: str = "cli",
|
|
agent_id: str | None = None,
|
|
fallback_depth: int | None = None,
|
|
) -> dict:
|
|
headers = {
|
|
"X-Claude-Code-Session-Id": "session-1234",
|
|
"x-app": app,
|
|
**({"x-claude-code-agent-id": agent_id} if agent_id is not None else {}),
|
|
}
|
|
return {
|
|
"metadata": {"user_api_key_hash": key_hash},
|
|
"proxy_server_request": {"headers": headers},
|
|
**({"fallback_depth": fallback_depth} if fallback_depth is not None else {}),
|
|
}
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("app", ["cli", "cli-bg"])
|
|
async def test_subagent_concrete_model_uses_the_main_sessions_router(self, app):
|
|
router = self._router()
|
|
|
|
await router.acompletion(
|
|
model="smart-router",
|
|
messages=[{"role": "user", "content": "main turn"}],
|
|
**self._request_kwargs(app=app),
|
|
)
|
|
subagent_kwargs = self._request_kwargs(app=app, agent_id="agent-1234")
|
|
|
|
response = await router.acompletion(
|
|
model="expensive-model",
|
|
messages=[{"role": "user", "content": "subagent turn"}],
|
|
**subagent_kwargs,
|
|
)
|
|
|
|
assert response.choices[0].message.content == "cheap response"
|
|
assert subagent_kwargs["metadata"]["model_group"] == "smart-router"
|
|
assert subagent_kwargs["metadata"]["routing_decision"]["router_model_name"] == "smart-router"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_main_thread_side_calls_to_a_plain_model_keep_the_session_router(self):
|
|
router = self._router()
|
|
|
|
await router.async_pre_routing_hook(model="smart-router", request_kwargs=self._request_kwargs())
|
|
await router.async_pre_routing_hook(model="expensive-model", request_kwargs=self._request_kwargs())
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(agent_id="agent-1234"),
|
|
)
|
|
|
|
assert response is not None
|
|
assert response.model == "cheap-model"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_redis_cleanup_failure_does_not_reject_a_subagent_request(self):
|
|
|
|
router = self._router()
|
|
del router.complexity_routers["smart-router"]
|
|
redis_cache = redis_cache_double()
|
|
redis_cache.async_get_cache = AsyncMock(return_value="smart-router")
|
|
redis_cache.async_delete_cache = AsyncMock(side_effect=ConnectionError("redis unavailable"))
|
|
router.update_redis_cache(cache=redis_cache)
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(agent_id="agent-1234"),
|
|
)
|
|
|
|
assert response is None
|
|
redis_cache.async_delete_cache.assert_awaited_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_redis_write_failures_do_not_reject_main_or_subagent_requests(self):
|
|
|
|
router = self._router()
|
|
redis_cache = redis_cache_double()
|
|
redis_cache.async_get_cache = AsyncMock(return_value="smart-router")
|
|
redis_cache.async_set_cache = AsyncMock(side_effect=Exception("redis unavailable"))
|
|
router.update_redis_cache(cache=redis_cache)
|
|
|
|
main_response = await router.async_pre_routing_hook(
|
|
model="smart-router",
|
|
request_kwargs=self._request_kwargs(),
|
|
)
|
|
subagent_response = await router.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(agent_id="agent-1234"),
|
|
)
|
|
|
|
assert main_response is not None
|
|
assert main_response.model == "cheap-model"
|
|
assert subagent_response is not None
|
|
assert subagent_response.model == "cheap-model"
|
|
assert redis_cache.async_set_cache.await_count == 2
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_subagents_follow_the_main_threads_latest_router_across_workers(self):
|
|
from types import SimpleNamespace
|
|
|
|
shared_binding = SimpleNamespace(value=None)
|
|
shared_redis = redis_cache_double()
|
|
shared_redis.async_get_cache = AsyncMock(side_effect=lambda key, **_: shared_binding.value)
|
|
shared_redis.async_set_cache = AsyncMock(
|
|
side_effect=lambda key, value, **_: setattr(shared_binding, "value", value)
|
|
)
|
|
main_worker, subagent_worker = self._router(), self._router()
|
|
main_worker.update_redis_cache(cache=shared_redis)
|
|
subagent_worker.update_redis_cache(cache=shared_redis)
|
|
|
|
await main_worker.async_pre_routing_hook(model="smart-router", request_kwargs=self._request_kwargs())
|
|
first = await subagent_worker.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(agent_id="agent-1234"),
|
|
)
|
|
await main_worker.async_pre_routing_hook(model="premium-router", request_kwargs=self._request_kwargs())
|
|
second = await subagent_worker.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(agent_id="agent-1234"),
|
|
)
|
|
|
|
assert first is not None
|
|
assert first.model == "cheap-model"
|
|
assert second is not None
|
|
assert second.model == "expensive-model"
|
|
assert shared_binding.value == "premium-router"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_pre_routing_strategies_means_no_session_cache_traffic(self):
|
|
|
|
router = self._router()
|
|
router.complexity_routers.clear()
|
|
redis_cache = redis_cache_double()
|
|
redis_cache.async_get_cache = AsyncMock(return_value=None)
|
|
redis_cache.async_set_cache = AsyncMock()
|
|
redis_cache.async_delete_cache = AsyncMock()
|
|
router.update_redis_cache(cache=redis_cache)
|
|
|
|
for request_kwargs in (self._request_kwargs(), self._request_kwargs(agent_id="agent-1234")):
|
|
response = await router.async_pre_routing_hook(model="expensive-model", request_kwargs=request_kwargs)
|
|
assert response is None
|
|
|
|
redis_cache.async_get_cache.assert_not_awaited()
|
|
redis_cache.async_set_cache.assert_not_awaited()
|
|
redis_cache.async_delete_cache.assert_not_awaited()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_background_and_fallback_requests_do_not_clear_the_session_router(self):
|
|
router = self._router()
|
|
|
|
await router.async_pre_routing_hook(model="smart-router", request_kwargs=self._request_kwargs())
|
|
await router.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(app="cli-bg"),
|
|
)
|
|
await router.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(fallback_depth=1),
|
|
)
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(agent_id="agent-1234"),
|
|
)
|
|
|
|
assert response is not None
|
|
assert response.model == "cheap-model"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_subagent_fallback_does_not_reapply_the_session_router(self):
|
|
router = self._router()
|
|
|
|
await router.async_pre_routing_hook(model="smart-router", request_kwargs=self._request_kwargs())
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(agent_id="agent-1234", fallback_depth=1),
|
|
)
|
|
|
|
assert response is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_subagent_can_fallback_to_its_original_requested_model(self):
|
|
router = self._router(
|
|
cheap_response="litellm.RateLimitError",
|
|
fallbacks=[{"cheap-model": ["expensive-model"]}],
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="smart-router",
|
|
messages=[{"role": "user", "content": "main turn"}],
|
|
**self._request_kwargs(),
|
|
)
|
|
subagent_kwargs = self._request_kwargs(agent_id="agent-1234")
|
|
|
|
response = await router.acompletion(
|
|
model="expensive-model",
|
|
messages=[{"role": "user", "content": "subagent turn"}],
|
|
**subagent_kwargs,
|
|
)
|
|
|
|
assert response.choices[0].message.content == "expensive response"
|
|
assert subagent_kwargs["metadata"]["routing_decision"]["routed_model"] == "cheap-model"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_subagent_can_use_the_bound_router_name_fallback(self):
|
|
router = self._router(
|
|
cheap_response="litellm.RateLimitError",
|
|
fallbacks=[{"smart-router": ["expensive-model"]}],
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="smart-router",
|
|
messages=[{"role": "user", "content": "main turn"}],
|
|
**self._request_kwargs(),
|
|
)
|
|
|
|
response = await router.acompletion(
|
|
model="expensive-model",
|
|
messages=[{"role": "user", "content": "subagent turn"}],
|
|
**self._request_kwargs(agent_id="agent-1234"),
|
|
)
|
|
|
|
assert response.choices[0].message.content == "expensive response"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_anthropic_subagent_four_fallback_hops_use_each_current_model_chain(self):
|
|
from litellm.types.router import TaggedPreRoutingStrategy
|
|
|
|
failing_groups = ("cheap-model", "fallback-1", "fallback-2", "fallback-3")
|
|
router = litellm.Router(
|
|
model_list=[
|
|
*(
|
|
{
|
|
"model_name": group,
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-3-haiku-20240307",
|
|
"mock_response": "litellm.RateLimitError",
|
|
},
|
|
}
|
|
for group in failing_groups
|
|
),
|
|
{
|
|
"model_name": "requested-model",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-3-haiku-20240307",
|
|
"mock_response": "requested response",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "fallback-4",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-3-haiku-20240307",
|
|
"mock_response": "fourth fallback response",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[
|
|
{"smart-router": ["fallback-1"]},
|
|
{"fallback-1": ["fallback-2"]},
|
|
{"fallback-2": ["fallback-3"]},
|
|
{"fallback-3": ["fallback-4"]},
|
|
],
|
|
num_retries=0,
|
|
max_fallbacks=4,
|
|
)
|
|
router.complexity_routers = {
|
|
"smart-router": (TaggedPreRoutingStrategy(tags=(), strategy=self._RewriteStrategy()),)
|
|
}
|
|
main_kwargs = self._request_kwargs()
|
|
main_kwargs["litellm_metadata"] = main_kwargs.pop("metadata")
|
|
await router.async_pre_routing_hook(model="smart-router", request_kwargs=main_kwargs)
|
|
subagent_kwargs = self._request_kwargs(agent_id="agent-1234")
|
|
subagent_kwargs["litellm_metadata"] = subagent_kwargs.pop("metadata")
|
|
|
|
response = await router.aanthropic_messages(
|
|
model="requested-model",
|
|
messages=[{"role": "user", "content": "subagent turn"}],
|
|
max_tokens=64,
|
|
**subagent_kwargs,
|
|
)
|
|
|
|
assert response["content"][0]["text"] == "fourth fallback response"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_session_router_binding_is_scoped_to_the_authenticated_key(self):
|
|
router = self._router()
|
|
|
|
await router.async_pre_routing_hook(model="smart-router", request_kwargs=self._request_kwargs())
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model="expensive-model",
|
|
request_kwargs=self._request_kwargs(key_hash="key-hash-b", agent_id="agent-1234"),
|
|
)
|
|
|
|
assert response is None
|
|
|
|
|
|
class TestAutoRouterMaxInputCharsWiring:
|
|
"""`auto_router_max_input_chars` on the deployment has to reach the AutoRouter that embeds prompts.
|
|
|
|
Without it the cap silently reverts to the default, so an operator whose embedding model has a
|
|
512-token window cannot lower it and every long prompt falls back to the default model instead
|
|
of being routed.
|
|
"""
|
|
|
|
@staticmethod
|
|
def _router(**extra_params) -> "litellm.Router":
|
|
pytest.importorskip("semantic_router", reason="auto-router needs the semantic-router extra")
|
|
return litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "gpt-4o"}},
|
|
{
|
|
"model_name": "my-auto-router",
|
|
"litellm_params": {
|
|
"model": "auto_router/my-auto-router",
|
|
"auto_router_config": json.dumps(
|
|
{"routes": [{"name": "gpt-4o", "utterances": ["write me code"]}]}
|
|
),
|
|
"auto_router_default_model": "gpt-4o",
|
|
"auto_router_embedding_model": "text-embedding-3-small",
|
|
**extra_params,
|
|
},
|
|
},
|
|
]
|
|
)
|
|
|
|
@staticmethod
|
|
def _registered_auto_router(router: "litellm.Router"):
|
|
return router.auto_routers["my-auto-router"][0].strategy
|
|
|
|
def test_should_pass_the_configured_cap_to_the_auto_router(self):
|
|
router = self._router(auto_router_max_input_chars=512)
|
|
|
|
assert self._registered_auto_router(router).max_input_chars == 512
|
|
|
|
def test_should_fall_back_to_the_shared_default_when_the_deployment_omits_it(self):
|
|
from litellm.constants import DEFAULT_AUTO_ROUTER_MAX_INPUT_CHARS
|
|
|
|
router = self._router()
|
|
|
|
assert self._registered_auto_router(router).max_input_chars == DEFAULT_AUTO_ROUTER_MAX_INPUT_CHARS
|
|
|
|
|
|
class TestTaggedAutoRouterOnSharedModelName:
|
|
"""A tagged auto-router marker sharing its model_name with a plain deployment must not
|
|
capture requests whose tags don't match it when tag filtering is enabled (#36620)."""
|
|
|
|
class _FixedRouteLayer:
|
|
def __call__(self, text: str):
|
|
from semantic_router.schema import RouteChoice
|
|
|
|
return RouteChoice(name="gemini-flash")
|
|
|
|
@classmethod
|
|
def _router(cls, marker_tags, include_plain_sibling: bool, enable_tag_filtering: bool) -> "litellm.Router":
|
|
pytest.importorskip("semantic_router", reason="auto-router needs the semantic-router extra")
|
|
marker = {
|
|
"model_name": "gpt4o",
|
|
"litellm_params": {
|
|
"model": "auto_router/gpt4o-router",
|
|
"auto_router_config": json.dumps(
|
|
{"routes": [{"name": "gemini-flash", "utterances": ["capital city questions"]}]}
|
|
),
|
|
"auto_router_default_model": "gemini-flash",
|
|
"auto_router_embedding_model": "text-embedding-3-small",
|
|
**({"tags": marker_tags} if marker_tags else {}),
|
|
},
|
|
}
|
|
plain = {"model_name": "gpt4o", "litellm_params": {"model": "openai/gpt-4o"}}
|
|
tier = {"model_name": "gemini-flash", "litellm_params": {"model": "gemini/gemini-3.6-flash"}}
|
|
router = litellm.Router(
|
|
model_list=[plain, marker, tier] if include_plain_sibling else [marker, tier],
|
|
enable_tag_filtering=enable_tag_filtering,
|
|
)
|
|
router.auto_routers["gpt4o"][0].strategy.routelayer = cls._FixedRouteLayer()
|
|
return router
|
|
|
|
@staticmethod
|
|
async def _hook_response(router: "litellm.Router", request_kwargs: dict):
|
|
return await router.async_pre_routing_hook(
|
|
model="gpt4o",
|
|
request_kwargs=request_kwargs,
|
|
messages=[{"role": "user", "content": "What is the capital of France?"}],
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_untagged_request_bypasses_the_tagged_marker_when_a_plain_deployment_shares_the_name(self):
|
|
router = self._router(marker_tags=["route"], include_plain_sibling=True, enable_tag_filtering=True)
|
|
|
|
assert await self._hook_response(router, {}) is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_request_tagged_for_the_marker_is_still_semantically_routed(self):
|
|
router = self._router(marker_tags=["route"], include_plain_sibling=True, enable_tag_filtering=True)
|
|
|
|
response = await self._hook_response(router, {"metadata": {"tags": ["route"]}})
|
|
|
|
assert response is not None
|
|
assert response.model == "gemini-flash"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_request_level_tag_filtering_from_key_settings_bypasses_the_marker(self):
|
|
router = self._router(marker_tags=["route"], include_plain_sibling=True, enable_tag_filtering=False)
|
|
|
|
assert await self._hook_response(router, {"enable_tag_filtering": True}) is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_globally_disabled_filtering_still_lets_the_marker_capture_untagged_requests(self):
|
|
router = self._router(marker_tags=["route"], include_plain_sibling=True, enable_tag_filtering=False)
|
|
|
|
response = await self._hook_response(router, {})
|
|
|
|
assert response is not None
|
|
assert response.model == "gemini-flash"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_marker_only_alias_still_captures_untagged_requests(self):
|
|
router = self._router(marker_tags=["route"], include_plain_sibling=False, enable_tag_filtering=True)
|
|
|
|
response = await self._hook_response(router, {})
|
|
|
|
assert response is not None
|
|
assert response.model == "gemini-flash"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_untagged_marker_sharing_the_name_still_captures_untagged_requests(self):
|
|
router = self._router(marker_tags=None, include_plain_sibling=True, enable_tag_filtering=True)
|
|
|
|
response = await self._hook_response(router, {})
|
|
|
|
assert response is not None
|
|
assert response.model == "gemini-flash"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_untagged_selection_never_lands_on_the_marker_deployment(self):
|
|
router = self._router(marker_tags=["route"], include_plain_sibling=True, enable_tag_filtering=True)
|
|
|
|
for _ in range(20):
|
|
deployment = await router.async_get_available_deployment(
|
|
model="gpt4o",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "What is the capital of France?"}],
|
|
)
|
|
assert deployment["litellm_params"]["model"] == "openai/gpt-4o"
|
|
|
|
|
|
class TestAutoRouterSharedModelNameConnectionParams:
|
|
"""A plain deployment sharing its model_name with an `auto_router/` marker must not have
|
|
its api_base and api_key grafted onto the routed tier's outbound call (#36619)."""
|
|
|
|
PLAIN_API_BASE = "https://plain-sibling.openai.example/v1"
|
|
PLAIN_API_KEY = "sk-plain-sibling-secret"
|
|
|
|
class _FixedRouteLayer:
|
|
def __call__(self, text: str):
|
|
from semantic_router.schema import RouteChoice
|
|
|
|
return RouteChoice(name="gemini-flash")
|
|
|
|
@classmethod
|
|
def _router(cls, plain_entry_first: bool) -> "litellm.Router":
|
|
pytest.importorskip("semantic_router", reason="auto-router needs the semantic-router extra")
|
|
plain = {
|
|
"model_name": "gpt4o",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o",
|
|
"api_key": cls.PLAIN_API_KEY,
|
|
"api_base": cls.PLAIN_API_BASE,
|
|
},
|
|
}
|
|
marker = {
|
|
"model_name": "gpt4o",
|
|
"litellm_params": {
|
|
"model": "auto_router/gpt4o-router",
|
|
"auto_router_config": json.dumps(
|
|
{"routes": [{"name": "gemini-flash", "utterances": ["capital city questions"]}]}
|
|
),
|
|
"auto_router_default_model": "gemini-flash",
|
|
"auto_router_embedding_model": "text-embedding-3-small",
|
|
"drop_params": True,
|
|
},
|
|
}
|
|
tier = {
|
|
"model_name": "gemini-flash",
|
|
"litellm_params": {"model": "gemini/gemini-3.6-flash", "api_key": "sk-tier-key"},
|
|
}
|
|
shared_name_entries = [plain, marker] if plain_entry_first else [marker, plain]
|
|
router = litellm.Router(model_list=[*shared_name_entries, tier])
|
|
router.auto_routers["gpt4o"][0].strategy.routelayer = cls._FixedRouteLayer()
|
|
return router
|
|
|
|
@staticmethod
|
|
def _gemini_response() -> httpx.Response:
|
|
return httpx.Response(
|
|
status_code=200,
|
|
json={
|
|
"candidates": [{"content": {"parts": [{"text": "Paris"}], "role": "model"}, "finishReason": "STOP"}],
|
|
"usageMetadata": {"promptTokenCount": 5, "candidatesTokenCount": 1, "totalTokenCount": 6},
|
|
"modelVersion": "gemini-3.6-flash",
|
|
},
|
|
request=httpx.Request("POST", "https://generativelanguage.googleapis.com"),
|
|
)
|
|
|
|
@pytest.mark.parametrize("plain_entry_first", [True, False], ids=["plain_entry_first", "marker_entry_first"])
|
|
async def test_routed_tier_call_goes_out_on_its_own_endpoint_and_credentials(self, plain_entry_first):
|
|
"""The outbound provider request for the routed tier hits the tier's own Gemini host
|
|
with the tier's own key, never the plain sibling's api_base or api_key."""
|
|
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
|
|
|
|
router = self._router(plain_entry_first)
|
|
|
|
with patch.object(
|
|
AsyncHTTPHandler, "post", new_callable=AsyncMock, return_value=self._gemini_response()
|
|
) as mock_post:
|
|
await router.acompletion(
|
|
model="gpt4o",
|
|
messages=[{"role": "user", "content": "What is the capital of France?"}],
|
|
)
|
|
|
|
call = mock_post.call_args
|
|
outbound_url = str(call.kwargs["url"] if "url" in call.kwargs else call.args[0])
|
|
outbound_headers = dict(call.kwargs.get("headers") or {})
|
|
|
|
assert "generativelanguage.googleapis.com" in outbound_url
|
|
assert "gemini-3.6-flash" in outbound_url
|
|
assert self.PLAIN_API_BASE not in outbound_url
|
|
assert self.PLAIN_API_KEY not in outbound_url
|
|
assert self.PLAIN_API_KEY not in json.dumps(outbound_headers)
|
|
|
|
|
|
class TestGetAllowedFailsFromPolicy:
|
|
def _make_router(self, **policy_kwargs) -> litellm.Router:
|
|
from litellm.types.router import AllowedFailsPolicy
|
|
|
|
return litellm.Router(
|
|
model_list=[{"model_name": "gpt-4", "litellm_params": {"model": "gpt-4", "api_key": "fake"}}],
|
|
allowed_fails_policy=AllowedFailsPolicy(**policy_kwargs),
|
|
)
|
|
|
|
def test_no_policy_returns_none(self):
|
|
router = litellm.Router(
|
|
model_list=[{"model_name": "gpt-4", "litellm_params": {"model": "gpt-4", "api_key": "fake"}}],
|
|
)
|
|
assert router.get_allowed_fails_from_policy(litellm.RateLimitError("429", "openai", "gpt-4")) is None
|
|
|
|
def test_internal_server_error_allowed_fails(self):
|
|
router = self._make_router(InternalServerErrorAllowedFails=7)
|
|
exc = litellm.InternalServerError("500", "openai", "gpt-4")
|
|
assert router.get_allowed_fails_from_policy(exc) == 7
|
|
|
|
def test_service_unavailable_error_allowed_fails(self):
|
|
router = self._make_router(ServiceUnavailableErrorAllowedFails=4)
|
|
exc = litellm.ServiceUnavailableError("503", "openai", "gpt-4")
|
|
assert router.get_allowed_fails_from_policy(exc) == 4
|
|
|
|
def test_bad_gateway_error_allowed_fails(self):
|
|
router = self._make_router(BadGatewayErrorAllowedFails=2)
|
|
exc = litellm.BadGatewayError("502", "openai", "gpt-4")
|
|
assert router.get_allowed_fails_from_policy(exc) == 2
|
|
|
|
def test_not_found_error_allowed_fails(self):
|
|
router = self._make_router(NotFoundErrorAllowedFails=1)
|
|
exc = litellm.NotFoundError("404", "openai", "gpt-4")
|
|
assert router.get_allowed_fails_from_policy(exc) == 1
|
|
|
|
def test_payment_required_error_uses_bad_request_allowed_fails(self):
|
|
assert (
|
|
self._make_router(BadRequestErrorAllowedFails=6).get_allowed_fails_from_policy(
|
|
litellm.PaymentRequiredError("402 error", "openai", "gpt-4")
|
|
)
|
|
== 6
|
|
)
|
|
|
|
def test_unmatched_exception_returns_none(self):
|
|
router = self._make_router(InternalServerErrorAllowedFails=5)
|
|
exc = litellm.RateLimitError("429", "openai", "gpt-4")
|
|
assert router.get_allowed_fails_from_policy(exc) is None
|
|
|
|
|
|
class _LogCapture(logging.Handler):
|
|
def __init__(self, level):
|
|
super().__init__(level=level)
|
|
self._level = level
|
|
self.messages = []
|
|
|
|
def emit(self, record):
|
|
if record.levelno == self._level:
|
|
self.messages.append(record.getMessage())
|
|
|
|
|
|
class _FallbackAttemptRecorder(CustomLogger):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.failed_targets = []
|
|
self.breadcrumbs_per_target = []
|
|
|
|
async def log_failure_fallback_event(self, original_model_group, kwargs, original_exception):
|
|
self.failed_targets.append(kwargs.get("model"))
|
|
self.breadcrumbs_per_target.append(kwargs.get("metadata", {}).get("previous_models", ()))
|
|
|
|
|
|
def _cyclic_fallback_router(num_retries=0):
|
|
groups = ["group-a", "group-b", "group-c", "group-d"]
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": group,
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "sk-fake",
|
|
"mock_response": "litellm.InternalServerError",
|
|
},
|
|
"model_info": {"id": f"{group}-deployment"},
|
|
}
|
|
for group in groups
|
|
],
|
|
fallbacks=[
|
|
{"group-a": ["group-b", "group-c"]},
|
|
{"group-b": ["group-a", "group-c"]},
|
|
{"group-c": ["group-d"]},
|
|
{"group-d": ["group-b", "group-a"]},
|
|
],
|
|
num_retries=num_retries,
|
|
)
|
|
|
|
|
|
async def _drive_cyclic_fallback(router, capture, recorder=None, **request_kwargs):
|
|
router_logger = logging.getLogger("LiteLLM Router")
|
|
previous_level = router_logger.level
|
|
router_logger.setLevel(capture.level)
|
|
router_logger.addHandler(capture)
|
|
if recorder is not None:
|
|
litellm.callbacks.append(recorder)
|
|
try:
|
|
with pytest.raises(litellm.InternalServerError):
|
|
await router.acompletion(model="group-a", messages=[{"role": "user", "content": "hi"}], **request_kwargs)
|
|
finally:
|
|
router_logger.removeHandler(capture)
|
|
router_logger.setLevel(previous_level)
|
|
if recorder is not None:
|
|
litellm.callbacks.remove(recorder)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cyclic_fallback_graph_does_not_amplify_one_request():
|
|
"""A fallback graph whose entries loop back on each other is easy to build by accident,
|
|
and every group in the loop fails identically on a deterministic error, so the walk must
|
|
not revisit a group and must not re-emit a growing chained traceback at each level. Left
|
|
unbounded, one request blocks the event loop long enough for health probes to fail."""
|
|
recorder = _FallbackAttemptRecorder()
|
|
capture = _LogCapture(logging.ERROR)
|
|
|
|
await _drive_cyclic_fallback(_cyclic_fallback_router(), capture, recorder)
|
|
|
|
assert sorted(set(recorder.failed_targets)) == ["group-b", "group-c", "group-d"]
|
|
assert len(recorder.failed_targets) == len(set(recorder.failed_targets))
|
|
assert not any("Traceback (most recent call last)" in message for message in capture.messages)
|
|
assert sum(len(message) for message in capture.messages) < 5_000
|
|
|
|
|
|
_FLAT_ATTEMPT_RECORD_KEYS = frozenset(
|
|
{"model_group", "deployment_id", "model", "base_model", "exception_type", "exception_string", "attempted_retries"}
|
|
)
|
|
_BREADCRUMB_CREDENTIAL_CANARY = "Bearer sk-ant-oat01-RETRY-BREADCRUMB-CANARY-doNotShip"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_retry_records_are_flat_and_name_the_failed_group_on_fallback_hops():
|
|
"""Each failed attempt leaves a flat record in previous_models, which reaches spend logs and
|
|
logging callbacks. Nothing downstream reads the failed attempt's kwargs or metadata, and copying
|
|
them is what carried client credentials and multiplied the payload on every retry. A fallback hop
|
|
calls log_retry too, so the record has to name the group that failed, not the one taken next."""
|
|
router = _cyclic_fallback_router(num_retries=1)
|
|
capture = _LogCapture(logging.ERROR)
|
|
recorder = _FallbackAttemptRecorder()
|
|
|
|
await _drive_cyclic_fallback(router, capture, recorder)
|
|
|
|
records = [record for hop in recorder.breadcrumbs_per_target for record in hop]
|
|
assert records, "no retry records were recorded"
|
|
for record in records:
|
|
assert set(record) == _FLAT_ATTEMPT_RECORD_KEYS
|
|
assert record["exception_type"] == "InternalServerError"
|
|
assert record["deployment_id"] == f"{record['model_group']}-deployment"
|
|
group_failed_before_hop = {"group-b": "group-a", "group-c": "group-b", "group-d": "group-c"}
|
|
for failed_target, hop_records in zip(recorder.failed_targets, recorder.breadcrumbs_per_target):
|
|
groups = [record["model_group"] for record in hop_records]
|
|
first_own_attempt = groups.index(failed_target)
|
|
assert groups[first_own_attempt - 1] == group_failed_before_hop[failed_target]
|
|
assert set(groups[first_own_attempt:]) == {failed_target}
|
|
assert [record["attempted_retries"] for record in hop_records[first_own_attempt:]][:2] == [0, 1]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"container_key, request_kwargs",
|
|
[
|
|
(
|
|
"provider_specific_header",
|
|
{
|
|
"provider_specific_header": {
|
|
"custom_llm_provider": "openai",
|
|
"extra_headers": {"authorization": _BREADCRUMB_CREDENTIAL_CANARY},
|
|
}
|
|
},
|
|
),
|
|
(
|
|
"extra_headers",
|
|
{"extra_headers": {"authorization": _BREADCRUMB_CREDENTIAL_CANARY}},
|
|
),
|
|
(
|
|
"api_key",
|
|
{"api_key": _BREADCRUMB_CREDENTIAL_CANARY},
|
|
),
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_retry_records_never_carry_a_forwarded_credential(container_key, request_kwargs):
|
|
"""previous_models reaches spend logs and logging callbacks. Any request kwarg can carry a client's
|
|
forwarded Authorization token or a provider key, so the record must not carry request kwargs at
|
|
all: neither the credential-bearing container nor the raw secret, whatever key holds it."""
|
|
router = _cyclic_fallback_router(num_retries=1)
|
|
capture = _LogCapture(logging.ERROR)
|
|
metadata = {}
|
|
|
|
await _drive_cyclic_fallback(router, capture, metadata=metadata, **request_kwargs)
|
|
|
|
records = metadata["previous_models"]
|
|
assert records, "no retry records were recorded"
|
|
dumped = json.dumps(records)
|
|
assert container_key not in dumped
|
|
assert _BREADCRUMB_CREDENTIAL_CANARY not in dumped
|
|
|
|
|
|
def _always_failing_router(num_retries):
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "broken-group",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "sk-fake",
|
|
"mock_response": "litellm.InternalServerError",
|
|
},
|
|
}
|
|
],
|
|
num_retries=num_retries,
|
|
)
|
|
|
|
|
|
async def _fail_one_proxy_shaped_request(router, request_marker, expected_error=litellm.InternalServerError):
|
|
"""The proxy hands the router a metadata dict and a proxy_server_request whose body is a
|
|
shallow copy of the request, so body["metadata"] is the very same dict the router later
|
|
stamps previous_models onto."""
|
|
metadata = {"request_marker": request_marker}
|
|
with pytest.raises(expected_error):
|
|
await router.acompletion(
|
|
model="broken-group",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
metadata=metadata,
|
|
proxy_server_request={
|
|
"url": "http://localhost:4000/v1/chat/completions",
|
|
"method": "POST",
|
|
"headers": {},
|
|
"body": {"model": "broken-group", "metadata": metadata},
|
|
},
|
|
)
|
|
return metadata["previous_models"]
|
|
|
|
|
|
def _nested_breadcrumb_lists(node):
|
|
if isinstance(node, dict):
|
|
return [v for k, v in node.items() if k == "previous_models"] + [
|
|
found for v in node.values() for found in _nested_breadcrumb_lists(v)
|
|
]
|
|
if isinstance(node, (list, tuple)):
|
|
return [found for item in node for found in _nested_breadcrumb_lists(item)]
|
|
return []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_retry_records_stay_per_request_and_flat_across_failing_requests():
|
|
"""Every failed attempt appends a record to metadata["previous_models"], and the proxy's
|
|
request snapshot aliases that same metadata dict. Kept on the Router and copied wholesale,
|
|
each breadcrumb once embedded every earlier one from every earlier request, so the breadcrumb
|
|
tree, and with it the debug repr of the kwargs, roughly doubled on each failed attempt until
|
|
a single-worker proxy spent minutes in the redaction regex and stopped answering."""
|
|
router = _always_failing_router(num_retries=2)
|
|
|
|
records_per_request = [
|
|
await _fail_one_proxy_shaped_request(router, f"request-{request_number}") for request_number in range(1, 7)
|
|
]
|
|
|
|
for records in records_per_request:
|
|
assert [record["attempted_retries"] for record in records] == [0, 1, 2]
|
|
for record in records:
|
|
assert set(record) == _FLAT_ATTEMPT_RECORD_KEYS
|
|
assert _nested_breadcrumb_lists(record) == []
|
|
assert len({len(repr(records)) for records in records_per_request}) == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_retry_records_keep_only_the_last_four_attempts():
|
|
router = _always_failing_router(num_retries=6)
|
|
|
|
records = await _fail_one_proxy_shaped_request(router, "request-1")
|
|
|
|
assert [record["attempted_retries"] for record in records] == [3, 4, 5, 6]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_num_retries_per_request_stops_retries_at_caps_above_four(monkeypatch):
|
|
monkeypatch.setattr(litellm, "num_retries_per_request", 5)
|
|
router = _always_failing_router(num_retries=6)
|
|
|
|
records = await _fail_one_proxy_shaped_request(router, "request-1", expected_error=litellm.APIConnectionError)
|
|
|
|
assert [record["attempted_retries"] for record in records] == [3, 4, 5, 6]
|
|
assert ["Max retries per request hit!" in record["exception_string"] for record in records] == [
|
|
False,
|
|
False,
|
|
True,
|
|
True,
|
|
]
|
|
|
|
|
|
def _failing_group_with_healthy_fallback_router(num_retries: int) -> litellm.Router:
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "broken-group",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "sk-fake",
|
|
"mock_response": "litellm.InternalServerError",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "healthy-group",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-fake", "mock_response": "ok"},
|
|
},
|
|
],
|
|
fallbacks=[{"broken-group": ["healthy-group"]}],
|
|
num_retries=num_retries,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"cap, planted_count, hop_refused",
|
|
[(2, None, True), (4, None, False), (2, -100, True)],
|
|
ids=["cap-spent-before-the-hop", "cap-not-reached-by-the-hop", "planted-negative-count-does-not-lift-the-cap"],
|
|
)
|
|
async def test_num_retries_per_request_counts_retries_across_fallback_hops(
|
|
monkeypatch: pytest.MonkeyPatch, cap: int, planted_count: int | None, hop_refused: bool
|
|
) -> None:
|
|
"""num_retries_per_request caps the retries of one request, fallback hops included. Each hop starts a
|
|
fresh per-hop attempted_retries at zero, so a cap read from that counter let every hop retry from zero
|
|
and a request could spend far more retries than the cap allows. A caller who plants a negative count
|
|
in the request metadata must not push the cap further away either."""
|
|
monkeypatch.setattr(litellm, "num_retries_per_request", cap)
|
|
router = _failing_group_with_healthy_fallback_router(num_retries=1)
|
|
recorder = _FallbackAttemptRecorder()
|
|
litellm.callbacks.append(recorder)
|
|
try:
|
|
metadata = {} if planted_count is None else {"request_retry_count": planted_count}
|
|
request = router.acompletion(
|
|
model="broken-group", messages=[{"role": "user", "content": "hi"}], metadata=metadata
|
|
)
|
|
if not hop_refused:
|
|
assert (await request).choices[0].message.content == "ok"
|
|
return
|
|
with pytest.raises(litellm.InternalServerError):
|
|
await request
|
|
finally:
|
|
litellm.callbacks.remove(recorder)
|
|
|
|
assert recorder.failed_targets == ["healthy-group"]
|
|
hop_refusals = [
|
|
record["attempted_retries"]
|
|
for record in recorder.breadcrumbs_per_target[0]
|
|
if record["model_group"] == "healthy-group" and "Max retries per request hit!" in record["exception_string"]
|
|
]
|
|
assert hop_refusals == [0, 1]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_traceback_stays_available_at_debug_level():
|
|
"""Dropping the stack from the ERROR line is only safe because the fallback path still
|
|
emits it once per level at DEBUG, which is what an operator needs to diagnose why every
|
|
fallback failed. This pins that remaining debug traceback."""
|
|
capture = _LogCapture(logging.DEBUG)
|
|
|
|
await _drive_cyclic_fallback(_cyclic_fallback_router(), capture)
|
|
|
|
assert any("Traceback (most recent call last)" in message for message in capture.messages)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_failure_detail_from_upstream_is_bounded():
|
|
"""The detail each level records about the level below it is attacker-influenced, since
|
|
it carries whatever the upstream error said. It has to be bounded on its own, so a walk
|
|
over several groups cannot compound one large message into the log or into the message
|
|
handed back to the caller."""
|
|
huge_message = "z" * 50_000
|
|
capture = _LogCapture(logging.ERROR)
|
|
|
|
await _drive_cyclic_fallback(
|
|
_cyclic_fallback_router(),
|
|
capture,
|
|
mock_response=litellm.InternalServerError(message=huge_message, llm_provider="openai", model="group-a"),
|
|
)
|
|
|
|
assert capture.messages, "the fallback failure path did not log at ERROR"
|
|
assert huge_message not in "".join(capture.messages)
|
|
assert max(len(message) for message in capture.messages) < 5_000
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"complexity_router_config,expect_callback",
|
|
[
|
|
({"tiers": {"SIMPLE": "gpt-4o"}}, True),
|
|
({"tiers": {"SIMPLE": "gpt-4o"}, "deployment_affinity": False}, False),
|
|
({"tiers": {"SIMPLE": "gpt-4o"}, "deployment_affinity": False, "session_affinity": True}, True),
|
|
],
|
|
)
|
|
def test_complexity_router_registers_affinity_callback_for_deployment_pin(complexity_router_config, expect_callback):
|
|
"""The marker the complexity router stamps is inert unless a DeploymentAffinityCheck is
|
|
registered to read it, so deployment_affinity has to pull the callback in, and its default-on
|
|
means a bare config registers one. Opting out must skip the callback entirely rather than
|
|
register a filter that can never fire, including when session_affinity is on, since the two
|
|
pins are independent."""
|
|
from litellm.router_utils.pre_call_checks.deployment_affinity_check import (
|
|
DeploymentAffinityCheck,
|
|
)
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "gpt-4o"}},
|
|
{
|
|
"model_name": "my-complexity-router",
|
|
"litellm_params": {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_config": complexity_router_config,
|
|
},
|
|
},
|
|
]
|
|
)
|
|
try:
|
|
registered = any(isinstance(cb, DeploymentAffinityCheck) for cb in router.optional_callbacks or [])
|
|
assert registered is expect_callback
|
|
finally:
|
|
for cb in router.optional_callbacks or []:
|
|
litellm.logging_callback_manager.remove_callback_from_all_lists(cb)
|
|
|
|
|
|
def test_get_router_model_info_does_not_wipe_cached_pricing():
|
|
"""A Deployment's model_info declares the mirrored pricing fields with None defaults;
|
|
merging it must not write those Nones into the lru_cache'd dict get_model_info() owns,
|
|
or /model/info loses built-in prices for every model a worker serves."""
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
litellm.get_model_info.cache_clear()
|
|
expected = copy.deepcopy(litellm.get_model_info(model="anthropic/claude-sonnet-4-5"))
|
|
|
|
router = litellm.Router(model_list=[])
|
|
merged = router.get_router_model_info(
|
|
deployment=Deployment(
|
|
model_name="sonnet",
|
|
litellm_params=LiteLLM_Params(model="claude-sonnet-4-5", custom_llm_provider="anthropic"),
|
|
model_info=ModelInfo(id="sonnet-1"),
|
|
),
|
|
received_model_name="sonnet",
|
|
)
|
|
|
|
assert litellm.get_model_info(model="anthropic/claude-sonnet-4-5") == expected
|
|
for field in ("input_cost_per_token", "output_cost_per_token", "cache_read_input_token_cost"):
|
|
assert merged[field] == expected[field]
|
|
|
|
|
|
def test_get_router_model_info_keeps_explicit_pricing_overrides():
|
|
from litellm.types.router import Deployment, LiteLLM_Params, ModelInfo
|
|
|
|
litellm.get_model_info.cache_clear()
|
|
router = litellm.Router(model_list=[])
|
|
merged = router.get_router_model_info(
|
|
deployment=Deployment(
|
|
model_name="sonnet",
|
|
litellm_params=LiteLLM_Params(model="claude-sonnet-4-5", custom_llm_provider="anthropic"),
|
|
model_info=ModelInfo(id="sonnet-1", input_cost_per_token=1e-08),
|
|
),
|
|
received_model_name="sonnet",
|
|
)
|
|
|
|
assert merged["input_cost_per_token"] == 1e-08
|
|
assert litellm.get_model_info(model="anthropic/claude-sonnet-4-5")["input_cost_per_token"] != 1e-08
|
|
|
|
|
|
class TestModelGroupAliasReachesPreRoutingStrategies:
|
|
"""A `model_group_alias` whose target is a strategy router must dispatch exactly like the
|
|
router's own model_name. The four strategy registries are keyed by the marker deployment's
|
|
model_name, so the alias has to be resolved before the pre-routing hook looks anything up,
|
|
and a group that resolves only to markers is not callable at all (LIT-4664)."""
|
|
|
|
MARKER_TIMEOUT = 42.0
|
|
REGISTRY_NAMES = ("auto_routers", "complexity_routers", "adaptive_routers", "quality_routers")
|
|
|
|
class _RewriteStrategy:
|
|
async def async_pre_routing_hook(
|
|
self, model, request_kwargs, messages=None, input=None, specific_deployment=False, call_type=None
|
|
):
|
|
from litellm.types.router import PreRoutingHookResponse
|
|
|
|
return PreRoutingHookResponse(model="gemini-flash", messages=messages)
|
|
|
|
@classmethod
|
|
def _router(cls, registry_name: str | None) -> "litellm.Router":
|
|
from litellm.types.router import TaggedPreRoutingStrategy
|
|
|
|
tiers = dict.fromkeys(("SIMPLE", "MEDIUM", "COMPLEX", "REASONING"), "gemini-flash")
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "smart-route",
|
|
"litellm_params": {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_config": {"tiers": tiers},
|
|
"complexity_router_default_model": "gemini-flash",
|
|
"timeout": cls.MARKER_TIMEOUT,
|
|
},
|
|
},
|
|
{
|
|
"model_name": "gemini-flash",
|
|
"litellm_params": {"model": "gemini/gemini-3.6-flash", "mock_response": "routed by the tier"},
|
|
},
|
|
],
|
|
model_group_alias={"smart-alias": "smart-route"},
|
|
)
|
|
for name in cls.REGISTRY_NAMES:
|
|
setattr(router, name, {})
|
|
if registry_name is not None:
|
|
setattr(
|
|
router,
|
|
registry_name,
|
|
{"smart-route": [TaggedPreRoutingStrategy(tags=(), strategy=cls._RewriteStrategy())]},
|
|
)
|
|
return router
|
|
|
|
@staticmethod
|
|
def _messages() -> list[dict[str, str]]:
|
|
return [{"role": "user", "content": "What is the capital of France?"}]
|
|
|
|
@pytest.mark.parametrize("registry_name", REGISTRY_NAMES)
|
|
@pytest.mark.asyncio
|
|
async def test_alias_dispatches_to_the_strategy_registered_under_the_target(self, registry_name):
|
|
router = self._router(registry_name)
|
|
request_kwargs = {"metadata": {}}
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model="smart-alias", request_kwargs=request_kwargs, messages=self._messages()
|
|
)
|
|
|
|
assert response is not None
|
|
assert response.model == "gemini-flash"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_alias_call_still_forwards_the_marker_own_params_to_the_routed_tier(self):
|
|
router = self._router("auto_routers")
|
|
request_kwargs = {"metadata": {}}
|
|
|
|
await router.async_pre_routing_hook(
|
|
model="smart-alias", request_kwargs=request_kwargs, messages=self._messages()
|
|
)
|
|
|
|
assert request_kwargs["timeout"] == self.MARKER_TIMEOUT
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_alias_deployment_selection_lands_on_the_tier_never_the_marker(self):
|
|
router = self._router("auto_routers")
|
|
|
|
deployment = await router.async_get_available_deployment(
|
|
model="smart-alias", request_kwargs={"metadata": {}}, messages=self._messages()
|
|
)
|
|
|
|
assert deployment["litellm_params"]["model"] == "gemini/gemini-3.6-flash"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_alias_call_completes_and_still_bills_the_name_the_caller_sent(self):
|
|
router = self._router("auto_routers")
|
|
metadata: dict = {}
|
|
|
|
response = await router.acompletion(model="smart-alias", messages=self._messages(), metadata=metadata)
|
|
|
|
assert response.choices[0].message.content == "routed by the tier"
|
|
assert metadata["model_group"] == "smart-alias"
|
|
assert metadata["model_group_alias"] == "smart-alias"
|
|
|
|
def test_a_group_of_only_markers_is_not_a_callable_model(self):
|
|
router = self._router(None)
|
|
|
|
with pytest.raises(litellm.BadRequestError, match="strategy router marker"):
|
|
router.get_available_deployment(
|
|
model="smart-route", messages=self._messages(), request_kwargs={"metadata": {}}
|
|
)
|
|
|
|
|
|
class TestTeamPublicNameReachesPreRoutingStrategies:
|
|
"""A team-scoped strategy router is stored under an internal `model_name_{team}_{uuid}` with the
|
|
caller-facing name in `model_info.team_public_model_name`, and the four registries key on that
|
|
internal name. A team key asks for the public name, so the hook has to resolve it to the team's
|
|
marker through the same team-first resolution the deployment path uses, and a resolution that
|
|
yields only markers is not callable on any path (LIT-7363)."""
|
|
|
|
MARKER_TIMEOUT = 42.0
|
|
REGISTRY_NAMES = ("auto_routers", "complexity_routers", "adaptive_routers", "quality_routers")
|
|
TEAM = "team-a"
|
|
OTHER_TEAM = "team-b"
|
|
PUBLIC_NAME = "smart-route"
|
|
INTERNAL_NAME = "model_name_team-a_0b3c"
|
|
SIBLING_INTERNAL_NAME = "model_name_team-a_9e1d"
|
|
|
|
class _RewriteStrategy:
|
|
def __init__(self, rewrite_to: str = "gemini-flash"):
|
|
self.rewrite_to = rewrite_to
|
|
|
|
async def async_pre_routing_hook(
|
|
self, model, request_kwargs, messages=None, input=None, specific_deployment=False, call_type=None
|
|
):
|
|
from litellm.types.router import PreRoutingHookResponse
|
|
|
|
return PreRoutingHookResponse(model=self.rewrite_to, messages=messages)
|
|
|
|
@classmethod
|
|
def _team_marker(cls, internal_name: str, tags: list[str] | None = None) -> dict:
|
|
tiers = dict.fromkeys(("SIMPLE", "MEDIUM", "COMPLEX", "REASONING"), "gemini-flash")
|
|
return {
|
|
"model_name": internal_name,
|
|
"litellm_params": {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_config": {"tiers": tiers},
|
|
"complexity_router_default_model": "gemini-flash",
|
|
"timeout": cls.MARKER_TIMEOUT,
|
|
**({"tags": tags} if tags else {}),
|
|
},
|
|
"model_info": {"team_id": cls.TEAM, "team_public_model_name": cls.PUBLIC_NAME},
|
|
}
|
|
|
|
@classmethod
|
|
def _router(
|
|
cls,
|
|
registrations: dict[str, "TestTeamPublicNameReachesPreRoutingStrategies._RewriteStrategy"],
|
|
registry_name: str = "complexity_routers",
|
|
extra_deployments: tuple[dict, ...] = (),
|
|
markers: tuple[dict, ...] | None = None,
|
|
enable_tag_filtering: bool = False,
|
|
) -> "litellm.Router":
|
|
from litellm.types.router import TaggedPreRoutingStrategy
|
|
|
|
markers = markers if markers is not None else (cls._team_marker(cls.INTERNAL_NAME),)
|
|
tier = {
|
|
"model_name": "gemini-flash",
|
|
"litellm_params": {"model": "gemini/gemini-3.6-flash", "mock_response": "routed by the tier"},
|
|
}
|
|
router = litellm.Router(
|
|
model_list=[*markers, tier, *extra_deployments],
|
|
enable_tag_filtering=enable_tag_filtering,
|
|
)
|
|
tags_by_name = {m["model_name"]: tuple(m["litellm_params"].get("tags") or ()) for m in markers}
|
|
for name in cls.REGISTRY_NAMES:
|
|
setattr(router, name, {})
|
|
setattr(
|
|
router,
|
|
registry_name,
|
|
{
|
|
name: [TaggedPreRoutingStrategy(tags=tags_by_name[name], strategy=strategy)]
|
|
for name, strategy in registrations.items()
|
|
},
|
|
)
|
|
return router
|
|
|
|
@staticmethod
|
|
def _messages() -> list[dict[str, str]]:
|
|
return [{"role": "user", "content": "What is the capital of France?"}]
|
|
|
|
@classmethod
|
|
def _team_request(cls, team_id: str | None = "team-a", tags: list[str] | None = None) -> dict:
|
|
metadata = {**({"user_api_key_team_id": team_id} if team_id else {}), **({"tags": tags} if tags else {})}
|
|
return {"metadata": metadata}
|
|
|
|
@pytest.mark.parametrize("registry_name", REGISTRY_NAMES)
|
|
@pytest.mark.asyncio
|
|
async def test_team_key_dispatches_to_the_strategy_registered_under_the_internal_name(self, registry_name):
|
|
router = self._router({self.INTERNAL_NAME: self._RewriteStrategy()}, registry_name=registry_name)
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model=self.PUBLIC_NAME, request_kwargs=self._team_request(), messages=self._messages()
|
|
)
|
|
|
|
assert response is not None
|
|
assert response.model == "gemini-flash"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_team_key_deployment_selection_lands_on_the_tier_and_forwards_the_marker_params(self):
|
|
router = self._router({self.INTERNAL_NAME: self._RewriteStrategy()})
|
|
request_kwargs = self._team_request()
|
|
|
|
deployment = await router.async_get_available_deployment(
|
|
model=self.PUBLIC_NAME, request_kwargs=request_kwargs, messages=self._messages()
|
|
)
|
|
|
|
assert deployment["litellm_params"]["model"] == "gemini/gemini-3.6-flash"
|
|
assert request_kwargs["timeout"] == self.MARKER_TIMEOUT
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_another_team_never_reaches_the_strategy_or_the_marker(self):
|
|
router = self._router({self.INTERNAL_NAME: self._RewriteStrategy()})
|
|
|
|
assert (
|
|
await router.async_pre_routing_hook(
|
|
model=self.PUBLIC_NAME, request_kwargs=self._team_request(self.OTHER_TEAM), messages=self._messages()
|
|
)
|
|
is None
|
|
)
|
|
with pytest.raises(litellm.BadRequestError):
|
|
await router.async_get_available_deployment(
|
|
model=self.PUBLIC_NAME, request_kwargs=self._team_request(self.OTHER_TEAM), messages=self._messages()
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_sibling_team_markers_select_by_request_tag_then_default(self):
|
|
router = self._router(
|
|
{
|
|
self.INTERNAL_NAME: self._RewriteStrategy("cn-model"),
|
|
self.SIBLING_INTERNAL_NAME: self._RewriteStrategy("us-model"),
|
|
},
|
|
markers=(
|
|
self._team_marker(self.INTERNAL_NAME, tags=["cn"]),
|
|
self._team_marker(self.SIBLING_INTERNAL_NAME, tags=["us", "default"]),
|
|
),
|
|
)
|
|
|
|
async def routed(tags: list[str] | None) -> str | None:
|
|
response = await router.async_pre_routing_hook(
|
|
model=self.PUBLIC_NAME, request_kwargs=self._team_request(tags=tags), messages=self._messages()
|
|
)
|
|
return response.model if response else None
|
|
|
|
assert await routed(["cn"]) == "cn-model"
|
|
assert await routed(["us"]) == "us-model"
|
|
assert await routed(None) == "us-model"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_team_public_name_shadows_a_global_model_for_that_team_only(self):
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
self._team_marker(self.INTERNAL_NAME),
|
|
{"model_name": "gemini-flash", "litellm_params": {"model": "gemini/gemini-3.6-flash"}},
|
|
{"model_name": self.PUBLIC_NAME, "litellm_params": {"model": "openai/gpt-4o"}},
|
|
],
|
|
)
|
|
|
|
async def routed(request_kwargs: dict) -> str | None:
|
|
response = await router.async_pre_routing_hook(
|
|
model=self.PUBLIC_NAME, request_kwargs=request_kwargs, messages=self._messages()
|
|
)
|
|
if response is not None:
|
|
assert response.routing_decision is not None
|
|
assert response.routing_decision["router_model_name"] == self.INTERNAL_NAME
|
|
assert response.routing_decision["router_config_id"] == router.model_list[0]["model_info"]["id"]
|
|
return response.model if response else None
|
|
|
|
async def selected(request_kwargs: dict) -> str:
|
|
deployment = await router.async_get_available_deployment(
|
|
model=self.PUBLIC_NAME, request_kwargs=request_kwargs, messages=self._messages()
|
|
)
|
|
return deployment["litellm_params"]["model"]
|
|
|
|
assert await routed(self._team_request()) == "gemini-flash"
|
|
assert await selected(self._team_request()) == "gemini/gemini-3.6-flash"
|
|
for request_kwargs in (self._team_request(None), self._team_request(self.OTHER_TEAM)):
|
|
assert await routed(request_kwargs) is None
|
|
assert await selected(request_kwargs) == "openai/gpt-4o"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_tag_filtering_hands_untagged_team_requests_to_the_team_plain_sibling(self):
|
|
plain_sibling = {
|
|
"model_name": self.SIBLING_INTERNAL_NAME,
|
|
"litellm_params": {"model": "openai/gpt-4o"},
|
|
"model_info": {"team_id": self.TEAM, "team_public_model_name": self.PUBLIC_NAME},
|
|
}
|
|
router = self._router(
|
|
{self.INTERNAL_NAME: self._RewriteStrategy()},
|
|
markers=(self._team_marker(self.INTERNAL_NAME, tags=["route"]),),
|
|
extra_deployments=(plain_sibling,),
|
|
enable_tag_filtering=True,
|
|
)
|
|
|
|
tagged = await router.async_pre_routing_hook(
|
|
model=self.PUBLIC_NAME, request_kwargs=self._team_request(tags=["route"]), messages=self._messages()
|
|
)
|
|
assert tagged is not None and tagged.model == "gemini-flash"
|
|
for _ in range(20):
|
|
deployment = await router.async_get_available_deployment(
|
|
model=self.PUBLIC_NAME, request_kwargs=self._team_request(), messages=self._messages()
|
|
)
|
|
assert deployment["litellm_params"]["model"] == "openai/gpt-4o"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_marker_only_team_resolution_is_rejected_as_uncallable(self):
|
|
import re
|
|
|
|
from litellm.types.router import RouterErrors
|
|
|
|
router = self._router({})
|
|
|
|
with pytest.raises(
|
|
litellm.BadRequestError, match=re.escape(RouterErrors.only_strategy_marker_deployments.value)
|
|
):
|
|
await router.async_get_available_deployment(
|
|
model=self.PUBLIC_NAME, request_kwargs=self._team_request(), messages=self._messages()
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_proxy_admin_without_a_team_reaches_the_team_strategy_by_public_name(self):
|
|
router = self._router({self.INTERNAL_NAME: self._RewriteStrategy()})
|
|
request_kwargs = {"metadata": {"user_api_key_auth": SimpleNamespace(user_role="proxy_admin")}}
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model=self.PUBLIC_NAME, request_kwargs=request_kwargs, messages=self._messages()
|
|
)
|
|
|
|
assert response is not None
|
|
assert response.model == "gemini-flash"
|
|
|
|
def test_compression_policy_follows_the_same_resolution_for_every_principal(self):
|
|
from litellm.proxy.guardrails.auto_router_compression import AutoRouterCompressionPolicy, policy_for_model
|
|
|
|
marker = self._team_marker(self.INTERNAL_NAME)
|
|
marker["litellm_params"]["auto_router_routing_compression"] = "headroom-team"
|
|
router = self._router({}, markers=(marker,))
|
|
admin = {"metadata": {"user_api_key_auth": SimpleNamespace(user_role="proxy_admin")}}
|
|
expected = AutoRouterCompressionPolicy(routing="headroom-team", model=None)
|
|
|
|
assert policy_for_model(router, self.PUBLIC_NAME, self._team_request(), ()) == expected
|
|
assert policy_for_model(router, self.PUBLIC_NAME, admin, ()) == expected
|
|
assert policy_for_model(router, self.PUBLIC_NAME, self._team_request(self.OTHER_TEAM), ()) is None
|
|
assert policy_for_model(router, self.PUBLIC_NAME, self._team_request(None), ()) is None
|
|
|
|
|
|
class TestAutoRouterCompressionDecoupling:
|
|
"""An auto router's `auto_router_routing_compression` / `auto_router_model_compression`
|
|
decouple what the routing decision sees from what the model call sees. The one
|
|
assertion that must hold under any mutation: the strategy can be routed on
|
|
compressed text while the caller's own `messages` list - the one that would reach
|
|
the model - is never touched."""
|
|
|
|
class _RecordingStrategy:
|
|
"""Echoes back whatever `messages` it was handed, like every real strategy does."""
|
|
|
|
def __init__(self):
|
|
self.received_messages: list[dict] | None = None
|
|
|
|
async def async_pre_routing_hook(
|
|
self, model, request_kwargs, messages=None, input=None, specific_deployment=False, call_type=None
|
|
):
|
|
from litellm.types.router import PreRoutingHookResponse
|
|
|
|
self.received_messages = messages
|
|
return PreRoutingHookResponse(model="gemini-flash", messages=messages)
|
|
|
|
class _CompressingGuardrail(CustomGuardrail):
|
|
def __init__(self, guardrail_name: str):
|
|
super().__init__(guardrail_name=guardrail_name)
|
|
self.call_count = 0
|
|
|
|
async def apply_guardrail(self, inputs, request_data, input_type, logging_obj=None):
|
|
self.call_count += 1
|
|
structured_messages = inputs.get("structured_messages") or []
|
|
compressed = [{**m, "content": f"[COMPRESSED] {m.get('content')}"} for m in structured_messages]
|
|
return {**inputs, "structured_messages": compressed}
|
|
|
|
@staticmethod
|
|
def _messages() -> list[dict[str, str]]:
|
|
return [{"role": "user", "content": "What is the capital of France?"}]
|
|
|
|
def _router(self, marker_litellm_params: dict) -> tuple[litellm.Router, "_RecordingStrategy"]:
|
|
from litellm.types.router import TaggedPreRoutingStrategy
|
|
|
|
tiers = dict.fromkeys(("SIMPLE", "MEDIUM", "COMPLEX", "REASONING"), "gemini-flash")
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "smart-router",
|
|
"litellm_params": {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_config": {"tiers": tiers},
|
|
"complexity_router_default_model": "gemini-flash",
|
|
**marker_litellm_params,
|
|
},
|
|
},
|
|
{
|
|
"model_name": "gemini-flash",
|
|
"litellm_params": {"model": "gemini/gemini-3.6-flash", "mock_response": "routed by the tier"},
|
|
},
|
|
],
|
|
)
|
|
for name in ("auto_routers", "complexity_routers", "adaptive_routers", "quality_routers"):
|
|
setattr(router, name, {})
|
|
strategy = self._RecordingStrategy()
|
|
router.complexity_routers = {"smart-router": [TaggedPreRoutingStrategy(tags=(), strategy=strategy)]}
|
|
return router, strategy
|
|
|
|
@pytest.fixture
|
|
def registered_guardrail(self, monkeypatch):
|
|
from litellm.proxy.guardrails import guardrail_registry
|
|
|
|
# Registered under a compression provider name: both hops refuse a name that
|
|
# does not resolve to one, so a bare callback would never be used.
|
|
monkeypatch.setitem(guardrail_registry.guardrail_class_registry, "headroom", self._CompressingGuardrail)
|
|
guardrail = self._CompressingGuardrail(guardrail_name="fake-compress")
|
|
litellm.logging_callback_manager.add_litellm_callback(guardrail)
|
|
yield guardrail
|
|
litellm.logging_callback_manager.remove_callback_from_all_lists(guardrail)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_routing_side_compression_never_reaches_the_caller_messages(self, registered_guardrail):
|
|
router, strategy = self._router(
|
|
{
|
|
"auto_router_routing_compression": "fake-compress",
|
|
"auto_router_model_compression": "none",
|
|
}
|
|
)
|
|
original_messages = self._messages()
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model="smart-router", request_kwargs={"metadata": {}}, messages=original_messages
|
|
)
|
|
|
|
assert strategy.received_messages == [
|
|
{"role": "user", "content": "[COMPRESSED] What is the capital of France?"}
|
|
]
|
|
assert response.messages == original_messages
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_model_side_compression_alone_leaves_routing_uncompressed(self, registered_guardrail):
|
|
router, strategy = self._router(
|
|
{
|
|
"auto_router_routing_compression": "none",
|
|
"auto_router_model_compression": "fake-compress",
|
|
}
|
|
)
|
|
original_messages = self._messages()
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model="smart-router", request_kwargs={"metadata": {}}, messages=original_messages
|
|
)
|
|
|
|
assert strategy.received_messages == original_messages
|
|
assert response.messages == original_messages
|
|
assert registered_guardrail.call_count == 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_routing_none_classifies_on_the_live_messages_not_a_pre_guardrail_copy(self, registered_guardrail):
|
|
"""Routing asked for no compression while the model hop compressed, so the only
|
|
messages left are that guardrail's output and the strategy classifies on them.
|
|
|
|
Keeping a pre-compression copy to classify on instead is what this deliberately
|
|
gives up: that copy is taken before the pre-call guardrails run, so it still
|
|
holds whatever a masking guardrail exists to strip, and routing-side compression
|
|
POSTs its input to an external service."""
|
|
router, strategy = self._router(
|
|
{
|
|
"auto_router_routing_compression": "none",
|
|
"auto_router_model_compression": "fake-compress",
|
|
}
|
|
)
|
|
model_compressed = [{"role": "user", "content": "[COMPRESSED] What is the capital of France?"}]
|
|
|
|
await router.async_pre_routing_hook(
|
|
model="smart-router", request_kwargs={"metadata": {}}, messages=model_compressed
|
|
)
|
|
|
|
assert strategy.received_messages == model_compressed
|
|
assert registered_guardrail.call_count == 0
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.asyncio
|
|
async def test_same_compression_still_compresses_routing_when_nothing_armed_it(self, registered_guardrail):
|
|
"""Regression: only the proxy calls arm_pre_call. Used through the SDK, nothing
|
|
arms the model-side guardrail and nothing has compressed anything, so reusing a
|
|
model-hop result that was never produced would serve the request with no
|
|
compression on either hop, silently ignoring the configuration."""
|
|
from litellm.proxy.guardrails import auto_router_compression
|
|
|
|
router, strategy = self._router(
|
|
{
|
|
"auto_router_routing_compression": "fake-compress",
|
|
"auto_router_model_compression": "fake-compress",
|
|
}
|
|
)
|
|
uncompressed = self._messages()
|
|
assert auto_router_compression.model_hop_compression_armed() is False
|
|
|
|
await router.async_pre_routing_hook(
|
|
model="smart-router", request_kwargs={"metadata": {}}, messages=uncompressed
|
|
)
|
|
|
|
assert strategy.received_messages != uncompressed
|
|
assert registered_guardrail.call_count == 1
|
|
|
|
async def test_same_compression_on_both_hops_compresses_once(self, registered_guardrail):
|
|
"""The same/different distinction exists so a shared choice does not pay for
|
|
compression twice: by the time the router runs, `messages` already reflects
|
|
whatever the ordinary pre-call guardrail pipeline did for the model call, so
|
|
the routing decision must reuse it rather than calling the guardrail again."""
|
|
from litellm.proxy.guardrails import auto_router_compression
|
|
|
|
router, strategy = self._router(
|
|
{
|
|
"auto_router_routing_compression": "fake-compress",
|
|
"auto_router_model_compression": "fake-compress",
|
|
}
|
|
)
|
|
# Stands in for what the proxy's ordinary pre-call guardrail pipeline would
|
|
# have already produced for the model call, since `auto_router_model_compression`
|
|
# names a guardrail: the router never triggers that pipeline itself.
|
|
already_compressed_messages = [{"role": "user", "content": "[COMPRESSED] What is the capital of France?"}]
|
|
# arm_pre_call is what would have armed that guardrail, and only the proxy calls
|
|
# it; the reuse below is conditional on it having run.
|
|
armed = auto_router_compression._model_hop_armed.set(True)
|
|
|
|
try:
|
|
response = await router.async_pre_routing_hook(
|
|
model="smart-router", request_kwargs={"metadata": {}}, messages=already_compressed_messages
|
|
)
|
|
finally:
|
|
auto_router_compression._model_hop_armed.reset(armed)
|
|
|
|
assert strategy.received_messages == already_compressed_messages
|
|
assert response.messages == already_compressed_messages
|
|
assert registered_guardrail.call_count == 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_no_policy_is_fully_unaffected(self, registered_guardrail):
|
|
router, strategy = self._router({})
|
|
original_messages = self._messages()
|
|
|
|
response = await router.async_pre_routing_hook(
|
|
model="smart-router", request_kwargs={"metadata": {}}, messages=original_messages
|
|
)
|
|
|
|
assert strategy.received_messages is original_messages
|
|
assert response.messages == original_messages
|
|
assert registered_guardrail.call_count == 0
|
|
|
|
|
|
@pytest.mark.usefixtures("local_model_cost_map")
|
|
@pytest.mark.usefixtures("local_model_cost_map")
|
|
class TestAzureBaseModelFallbackLogging:
|
|
"""When an azure deployment has no base_model but its model name is a known
|
|
azure key in the cost map, get_router_model_info resolves it via the
|
|
fallback, so it must not log the per-request 'Could not identify azure
|
|
model' ERROR. The ERROR must remain for genuinely unmappable deployment
|
|
names. Issue #33172."""
|
|
|
|
def _router_with_azure_deployment(self, deployment_model: str):
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "my-group",
|
|
"litellm_params": {
|
|
"model": deployment_model,
|
|
"api_key": "fake-key",
|
|
"api_base": "https://fake.openai.azure.com",
|
|
},
|
|
"model_info": {"id": "azure-base-model-test-id"},
|
|
}
|
|
]
|
|
)
|
|
|
|
def test_explicit_base_model_still_wins(self):
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "my-group",
|
|
"litellm_params": {
|
|
"model": "azure/some-deployment",
|
|
"api_key": "fake-key",
|
|
"api_base": "https://fake.openai.azure.com",
|
|
},
|
|
"model_info": {
|
|
"id": "azure-base-model-test-id",
|
|
"base_model": "azure/gpt-4o-mini",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
model_info = router.get_router_model_info(
|
|
deployment=None, received_model_name="my-group", id="azure-base-model-test-id"
|
|
)
|
|
assert model_info["max_input_tokens"] == litellm.model_cost["azure/gpt-4o-mini"]["max_input_tokens"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model,provider,expected",
|
|
[
|
|
("anthropic/claude-opus-5", None, True),
|
|
("claude-opus-4-8", None, True),
|
|
("anthropic/claude-opus-4-7", None, False),
|
|
("anthropic/claude-opus-4-6", None, False),
|
|
("anthropic/claude-sonnet-5", None, False),
|
|
("anthropic/off-map-opus", None, False),
|
|
("vertex_ai/claude-opus-5", None, False),
|
|
("bedrock/claude-opus-5", None, False),
|
|
("claude-opus-5", "vertex_ai", False),
|
|
("claude-opus-5", "bedrock", False),
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("operator_flag", [True, False])
|
|
def test_model_group_info_fast_mode_uses_exact_provider_catalog(
|
|
local_model_cost_map: None, model: str, provider: str | None, expected: bool, operator_flag: bool
|
|
) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "fast-group",
|
|
"litellm_params": {"model": model, "custom_llm_provider": provider, "api_key": "fake-key"},
|
|
"model_info": {"supports_fast_mode": operator_flag},
|
|
}
|
|
]
|
|
)
|
|
|
|
result: Final = router.get_model_group_info("fast-group")
|
|
|
|
assert result is not None
|
|
assert result.supports_fast_mode is expected
|
|
|
|
|
|
@pytest.mark.parametrize("flag", [None, False, "true", 1])
|
|
def test_model_group_info_fast_mode_fails_closed_without_explicit_boolean(
|
|
local_model_cost_map: None, monkeypatch: pytest.MonkeyPatch, flag: object
|
|
) -> None:
|
|
entry: Final = {
|
|
key: value for key, value in litellm.model_cost["claude-opus-5"].items() if key != "supports_fast_mode"
|
|
}
|
|
if flag is not None:
|
|
entry["supports_fast_mode"] = flag
|
|
monkeypatch.setitem(litellm.model_cost, "claude-opus-5", entry)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "fast-group",
|
|
"litellm_params": {"model": "anthropic/claude-opus-5", "api_key": "fake-key"},
|
|
"model_info": {"supports_fast_mode": True},
|
|
}
|
|
]
|
|
)
|
|
|
|
result: Final = router.get_model_group_info("fast-group")
|
|
|
|
assert result is not None
|
|
assert result.supports_fast_mode is False
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"other_model,expected",
|
|
[
|
|
("anthropic/claude-opus-4-8", True),
|
|
("anthropic/claude-opus-4-7", False),
|
|
("anthropic/off-map-opus", False),
|
|
("vertex_ai/claude-opus-5", False),
|
|
("bedrock/claude-opus-5", False),
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("reverse", [True, False])
|
|
def test_model_group_info_fast_mode_requires_every_deployment(
|
|
local_model_cost_map: None, other_model: str, expected: bool, reverse: bool
|
|
) -> None:
|
|
models: Final = (other_model, "anthropic/claude-opus-5") if reverse else ("anthropic/claude-opus-5", other_model)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "fast-group",
|
|
"litellm_params": {"model": model, "api_key": "fake-key"},
|
|
}
|
|
for model in models
|
|
]
|
|
)
|
|
|
|
result: Final = router.get_model_group_info("fast-group")
|
|
|
|
assert result is not None
|
|
assert result.supports_fast_mode is expected
|
|
|
|
|
|
class TestAddDeploymentApiBaseProviderResolution:
|
|
def test_bare_model_with_known_api_base_initializes(self):
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "groq-pinned",
|
|
"litellm_params": {
|
|
"model": "llama-3.3-70b-versatile",
|
|
"api_base": "https://api.groq.com/openai/v1",
|
|
"api_key": "fake-key",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "deepseek-pinned",
|
|
"litellm_params": {
|
|
"model": "deepseek-chat",
|
|
"api_base": "https://api.deepseek.com/v1",
|
|
"api_key": "fake-key",
|
|
},
|
|
},
|
|
]
|
|
)
|
|
|
|
model_list = router.get_model_list()
|
|
assert model_list is not None
|
|
assert {m["model_name"] for m in model_list} == {"groq-pinned", "deepseek-pinned"}
|
|
|
|
def test_bare_model_with_unknown_api_base_still_raises(self):
|
|
with pytest.raises(litellm.BadRequestError, match="LLM Provider NOT provided"):
|
|
litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "mystery",
|
|
"litellm_params": {
|
|
"model": "some-unknown-model",
|
|
"api_base": "https://llm.internal.example.com/v1",
|
|
"api_key": "fake-key",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
def test_explicit_custom_llm_provider_beats_api_base_endpoint_match(self):
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai-via-gateway",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"custom_llm_provider": "openai",
|
|
"api_base": "https://api.groq.com/openai/v1",
|
|
"api_key": "fake-key",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
deployment = router.get_deployment_by_model_group_name("openai-via-gateway")
|
|
assert deployment is not None
|
|
assert deployment.litellm_params.custom_llm_provider == "openai"
|
|
|
|
|
|
# =====================================================================
|
|
# anthropic_messages mid-stream-fallback helpers, added for #24004
|
|
# (mid-stream fallback not supported for anthropic_messages route type).
|
|
#
|
|
# anthropic_messages goes through _ageneric_api_call_with_fallbacks rather
|
|
# than _acompletion, so its returned iterator was never wrapped by the chat
|
|
# completions fallback handler: an SSE `event: error` frame from a native
|
|
# Anthropic/Bedrock passthrough passed through to the client silently, and a
|
|
# MidStreamFallbackError raised by the completion-bridge path's
|
|
# CustomStreamWrapper (e.g. a Vertex AI transport drop) propagated
|
|
# unhandled.
|
|
#
|
|
# Targets the helpers introduced on Router:
|
|
# - _aanthropic_messages_streaming_iterator
|
|
# - _aanthropic_messages_fallback_attempt
|
|
# - _aanthropic_messages_with_streaming_fallbacks
|
|
# - _dispatch_generic_call_type
|
|
# =====================================================================
|
|
|
|
|
|
def _anthropic_messages_overloaded_error_chunk() -> bytes:
|
|
return b'event: error\ndata: {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}\n\n'
|
|
|
|
|
|
def _anthropic_messages_content_chunk(text: str = "hi") -> bytes:
|
|
payload = f'{{"type": "content_block_delta", "delta": {{"type": "text_delta", "text": "{text}"}}}}'
|
|
return f"event: content_block_delta\ndata: {payload}\n\n".encode()
|
|
|
|
|
|
def _anthropic_messages_message_start_chunk() -> bytes:
|
|
"""A lifecycle/bookkeeping frame Anthropic sends before any real content -
|
|
routinely the very first event before an overload error."""
|
|
return b'event: message_start\ndata: {"type": "message_start", "message": {"id": "msg_1"}}\n\n'
|
|
|
|
|
|
def _anthropic_messages_ping_chunk() -> bytes:
|
|
return b'event: ping\ndata: {"type": "ping"}\n\n'
|
|
|
|
|
|
# -------- _aanthropic_messages_streaming_iterator (passthrough) --------
|
|
|
|
|
|
# -------- _aanthropic_messages_fallback_attempt --------
|
|
|
|
|
|
# -------- _aanthropic_messages_with_streaming_fallbacks --------
|
|
|
|
|
|
# -------- mid-stream error handling --------
|
|
|
|
|
|
def test_anthropic_stream_forwards_ping_live_direct_call():
|
|
ping = _anthropic_messages_ping_chunk()
|
|
content = _anthropic_messages_content_chunk("hi")
|
|
assert _anthropic_stream_forwards_ping_live(ping, has_generated_content=False) is True
|
|
assert _anthropic_stream_forwards_ping_live(ping, has_generated_content=True) is False
|
|
assert _anthropic_stream_forwards_ping_live(content, has_generated_content=False) is False
|
|
|
|
|
|
def test_anthropic_stream_error_is_gateway_verdict_direct_call():
|
|
assert _anthropic_stream_error_is_gateway_verdict(SERVER_FULFILLED_TOOL_LEAK_ERROR_SSE_BYTES) is True
|
|
assert _anthropic_stream_error_is_gateway_verdict(AGENTIC_LOOP_LIMIT_ERROR_SSE_BYTES) is True
|
|
assert _anthropic_stream_error_is_gateway_verdict(_anthropic_messages_overloaded_error_chunk()) is False
|
|
assert _anthropic_stream_error_is_gateway_verdict(_anthropic_messages_ping_chunk()) is False
|
|
|
|
|
|
def test_is_retriable_anthropic_status_direct_call():
|
|
assert _is_retriable_anthropic_status(429) is True
|
|
assert _is_retriable_anthropic_status(503) is True
|
|
assert _is_retriable_anthropic_status(500) is True
|
|
assert _is_retriable_anthropic_status(400) is False
|
|
assert _is_retriable_anthropic_status(404) is False
|
|
|
|
|
|
def test_anthropic_stream_should_decline_fallback_direct_call():
|
|
pre_first_chunk_error = MidStreamFallbackError(
|
|
message="overloaded", model="primary", llm_provider="anthropic", is_pre_first_chunk=True
|
|
)
|
|
post_first_chunk_error = MidStreamFallbackError(
|
|
message="overloaded", model="primary", llm_provider="anthropic", is_pre_first_chunk=False
|
|
)
|
|
assert _anthropic_stream_should_decline_fallback(False, pre_first_chunk_error) is False
|
|
assert _anthropic_stream_should_decline_fallback(True, pre_first_chunk_error) is True
|
|
assert _anthropic_stream_should_decline_fallback(False, post_first_chunk_error) is True
|
|
|
|
|
|
def test_anthropic_stream_commits_now_direct_call():
|
|
content = _anthropic_messages_content_chunk("hi")
|
|
lifecycle_chunk = _anthropic_messages_message_start_chunk()
|
|
assert _stream_commits_now(content, has_generated_content=False, buffered_chunk_count=0) is True
|
|
assert _stream_commits_now(content, has_generated_content=True, buffered_chunk_count=0) is False
|
|
assert (
|
|
_stream_commits_now(
|
|
lifecycle_chunk,
|
|
has_generated_content=False,
|
|
buffered_chunk_count=MAX_BUFFERED_PRE_CONTENT_ANTHROPIC_CHUNKS,
|
|
)
|
|
is True
|
|
)
|
|
assert (
|
|
_stream_commits_now(
|
|
lifecycle_chunk,
|
|
has_generated_content=False,
|
|
buffered_chunk_count=MAX_BUFFERED_PRE_CONTENT_ANTHROPIC_CHUNKS - 1,
|
|
)
|
|
is False
|
|
)
|
|
|
|
|
|
def _mid_stream_opt_out_router() -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{"model_name": "primary", "litellm_params": {"model": "openai/gpt-5.4", "api_key": "k1"}},
|
|
{"model_name": "fallback", "litellm_params": {"model": "openai/gpt-5.4-mini", "api_key": "k2"}},
|
|
],
|
|
fallbacks=[{"primary": ["fallback"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
|
|
def _mid_stream_opt_out_primary_error() -> litellm.InternalServerError:
|
|
return litellm.InternalServerError(message="primary failed at stream start", llm_provider="openai", model="primary")
|
|
|
|
|
|
def _mid_stream_opt_out_trigger(primary_error: Exception) -> MidStreamFallbackError:
|
|
return MidStreamFallbackError(
|
|
message=str(primary_error),
|
|
model="primary",
|
|
llm_provider="openai",
|
|
original_exception=primary_error,
|
|
is_pre_first_chunk=True,
|
|
)
|
|
|
|
|
|
class _MidStreamOptOutChatStream(CustomStreamWrapper):
|
|
"""A chat deployment stream, as the router sees one, that dies before its first chunk."""
|
|
|
|
def __init__(self, error: Exception, model: str = "primary") -> None:
|
|
super().__init__(completion_stream=object(), model=model, custom_llm_provider="openai", logging_obj=MagicMock())
|
|
self._error: Final = error
|
|
|
|
def __aiter__(self):
|
|
return self
|
|
|
|
async def __anext__(self) -> object:
|
|
raise self._error
|
|
|
|
def __iter__(self):
|
|
return self
|
|
|
|
def __next__(self) -> object:
|
|
raise self._error
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_disable_fallbacks_reaches_the_mid_stream_hop():
|
|
"""`disable_fallbacks=True` sent to the public entrypoint survives the fallback wrapper's handoff
|
|
into the stream: the primary's own error surfaces and no fallback deployment is ever called."""
|
|
router = _mid_stream_opt_out_router()
|
|
primary_error = _mid_stream_opt_out_primary_error()
|
|
|
|
async def primary_stream(**kwargs):
|
|
return _MidStreamOptOutChatStream(_mid_stream_opt_out_trigger(primary_error), model=kwargs["model"])
|
|
|
|
with patch("litellm.acompletion", side_effect=primary_stream) as provider_calls:
|
|
with pytest.raises(litellm.InternalServerError) as raised:
|
|
await router.acompletion(
|
|
model="primary", messages=[{"role": "user", "content": "Hi"}], stream=True, disable_fallbacks=True
|
|
)
|
|
|
|
assert raised.value is primary_error
|
|
assert [call.kwargs["metadata"]["model_group"] for call in provider_calls.call_args_list] == ["primary"]
|
|
|
|
|
|
def test_completion_disable_fallbacks_reaches_the_mid_stream_hop():
|
|
"""Sync counterpart of test_acompletion_disable_fallbacks_reaches_the_mid_stream_hop."""
|
|
router = _mid_stream_opt_out_router()
|
|
primary_error = _mid_stream_opt_out_primary_error()
|
|
|
|
def primary_stream(**kwargs):
|
|
return _MidStreamOptOutChatStream(_mid_stream_opt_out_trigger(primary_error), model=kwargs["model"])
|
|
|
|
with patch("litellm.completion", side_effect=primary_stream) as provider_calls:
|
|
with pytest.raises(litellm.InternalServerError) as raised:
|
|
router.completion(
|
|
model="primary", messages=[{"role": "user", "content": "Hi"}], stream=True, disable_fallbacks=True
|
|
)
|
|
|
|
assert raised.value is primary_error
|
|
assert [call.kwargs["metadata"]["model_group"] for call in provider_calls.call_args_list] == ["primary"]
|
|
|
|
|
|
class _AnthropicMessagesStringStatusError(Exception):
|
|
def __init__(self):
|
|
super().__init__("bad request")
|
|
self.status_code = "400"
|
|
|
|
|
|
class _AnthropicMessagesResponseOnlyStatusError(Exception):
|
|
def __init__(self):
|
|
super().__init__("bad request")
|
|
self.response = SimpleNamespace(status_code=400)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"error, expected_status",
|
|
[
|
|
(BedrockError(status_code=503, message="unavailable"), 503),
|
|
(_AnthropicMessagesStringStatusError(), 400),
|
|
(_AnthropicMessagesResponseOnlyStatusError(), 400),
|
|
(httpx.ReadError("connection reset by upstream"), None),
|
|
],
|
|
ids=["int", "digit-str", "response-only", "none"],
|
|
)
|
|
def test_anthropic_stream_raised_error_status_reads_every_status_shape(error, expected_status):
|
|
assert _stream_raised_error_status(error) == expected_status
|
|
|
|
|
|
# -------- _dispatch_generic_call_type --------
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_function_with_fallbacks_stamps_zero_attempted_fallbacks():
|
|
"""A request served by the primary model group records attempted_fallbacks=0 and
|
|
the requested model group in metadata, mirroring the x-litellm-attempted-fallbacks header."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "mock_response": "hi"},
|
|
}
|
|
]
|
|
)
|
|
metadata = {}
|
|
|
|
await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "hey"}],
|
|
metadata=metadata,
|
|
)
|
|
|
|
assert metadata["attempted_fallbacks"] == 0
|
|
assert metadata["original_model_group"] == "gpt-3.5-turbo"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_function_with_fallbacks_stamps_route_bucket_not_litellm_metadata():
|
|
"""A chat completion carrying both metadata buckets gets stamped in the route's bucket
|
|
(metadata), matching where run_async_fallback rewrites, so the two never diverge."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "mock_response": "hi"},
|
|
}
|
|
]
|
|
)
|
|
metadata = {}
|
|
litellm_metadata = {"client_key": "client_value"}
|
|
|
|
await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "hey"}],
|
|
metadata=metadata,
|
|
litellm_metadata=litellm_metadata,
|
|
)
|
|
|
|
assert metadata["attempted_fallbacks"] == 0
|
|
assert metadata["original_model_group"] == "gpt-3.5-turbo"
|
|
assert litellm_metadata["client_key"] == "client_value"
|
|
assert "attempted_fallbacks" not in litellm_metadata
|
|
assert "original_model_group" not in litellm_metadata
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_function_with_fallbacks_overrides_client_supplied_stamp_values():
|
|
"""Client-supplied attempted_fallbacks and original_model_group are replaced on entry,
|
|
so a reused metadata dict or a spoofed value cannot leak stale attribution into logs."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "mock_response": "hi"},
|
|
}
|
|
]
|
|
)
|
|
metadata = {"attempted_fallbacks": 99, "original_model_group": "stale-group"}
|
|
|
|
await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "hey"}],
|
|
metadata=metadata,
|
|
)
|
|
|
|
assert metadata["attempted_fallbacks"] == 0
|
|
assert metadata["original_model_group"] == "gpt-3.5-turbo"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_function_with_fallbacks_stamps_despite_forged_reentry_params():
|
|
"""A client injecting fallback_depth or a JSON-shaped attempted_targets via request
|
|
litellm params cannot skip the entry stamp; only the router's own in-process
|
|
AttemptedFallbackTargets instance marks a genuine re-entrant hop."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "mock_response": "hi"},
|
|
}
|
|
]
|
|
)
|
|
metadata = {"attempted_fallbacks": 99, "original_model_group": "spoofed-group"}
|
|
|
|
await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "hey"}],
|
|
metadata=metadata,
|
|
fallback_depth=3,
|
|
attempted_targets={"keys": ["spoofed-group"]},
|
|
)
|
|
|
|
assert metadata["attempted_fallbacks"] == 0
|
|
assert metadata["original_model_group"] == "gpt-3.5-turbo"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_function_with_fallbacks_skips_stamp_on_genuine_reentrant_hop():
|
|
"""A re-entrant hop carrying the router's own AttemptedFallbackTargets instance keeps
|
|
the per-hop metadata that run_async_fallback wrote instead of resetting it to zero."""
|
|
from litellm.router_utils.fallback_event_handlers import AttemptedFallbackTargets
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "mock_response": "hi"},
|
|
}
|
|
]
|
|
)
|
|
metadata = {"attempted_fallbacks": 1, "original_model_group": "prod-chat"}
|
|
|
|
await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "hey"}],
|
|
metadata=metadata,
|
|
attempted_targets=AttemptedFallbackTargets(keys=frozenset(("prod-chat",))),
|
|
)
|
|
|
|
assert metadata["attempted_fallbacks"] == 1
|
|
assert metadata["original_model_group"] == "prod-chat"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_run_async_fallback_keeps_caller_metadata_keys_on_the_wire(monkeypatch):
|
|
"""Under enable_preview_features, add_openai_metadata forwards only the first 16
|
|
string pairs of request metadata to the provider body, so the fallback hop must
|
|
spread caller keys before the router's own stamps: a stamp inserted first evicts
|
|
the caller's 16th key from the wire while the internal stamp rides in its place."""
|
|
monkeypatch.setattr(litellm, "enable_preview_features", True)
|
|
caller_metadata = {f"user_key_{i}": f"value_{i}" for i in range(16)}
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary-group",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "api_key": "sk-test"},
|
|
},
|
|
{
|
|
"model_name": "fallback-group",
|
|
"litellm_params": {"model": "gpt-3.5-turbo", "api_key": "sk-test"},
|
|
},
|
|
],
|
|
fallbacks=[{"primary-group": ["fallback-group"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
wire_bodies = []
|
|
|
|
def _respond(request: httpx.Request) -> httpx.Response:
|
|
wire_bodies.append(json.loads(request.content))
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-wire",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-3.5-turbo",
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
},
|
|
)
|
|
|
|
client = openai.AsyncOpenAI(
|
|
api_key="sk-test",
|
|
http_client=httpx.AsyncClient(transport=httpx.MockTransport(_respond)),
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="primary-group",
|
|
messages=[{"role": "user", "content": "hey"}],
|
|
metadata=dict(caller_metadata),
|
|
mock_testing_fallbacks=True,
|
|
client=client,
|
|
)
|
|
|
|
assert len(wire_bodies) == 1
|
|
assert wire_bodies[0]["metadata"] == caller_metadata
|
|
|
|
wire_bodies.clear()
|
|
small_metadata = {"team": "alpha", "env": "prod"}
|
|
await router.acompletion(
|
|
model="primary-group",
|
|
messages=[{"role": "user", "content": "hey again"}],
|
|
metadata=dict(small_metadata),
|
|
mock_testing_fallbacks=True,
|
|
client=client,
|
|
)
|
|
|
|
assert len(wire_bodies) == 1
|
|
small_wire = wire_bodies[0]["metadata"]
|
|
assert {k: small_wire[k] for k in small_metadata} == small_metadata
|
|
assert small_wire["original_model_group"] == "primary-group"
|
|
assert small_wire["model_group"] == "fallback-group"
|
|
|
|
|
|
def _permission_denied_error() -> litellm.PermissionDeniedError:
|
|
return litellm.PermissionDeniedError(
|
|
message="OpenrouterException - this key has no access to the model",
|
|
llm_provider="openrouter",
|
|
model="openrouter/openai/gpt-4o",
|
|
response=httpx.Response(status_code=403, request=httpx.Request(method="POST", url="https://openrouter.ai")),
|
|
)
|
|
|
|
|
|
def test_permission_denied_error_is_not_retried_against_a_single_deployment():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "openrouter/openai/gpt-4o", "api_key": "sk-test"}},
|
|
]
|
|
)
|
|
|
|
with pytest.raises(litellm.PermissionDeniedError):
|
|
router.should_retry_this_error(
|
|
error=_permission_denied_error(),
|
|
healthy_deployments=router.model_list,
|
|
all_deployments=router.model_list,
|
|
)
|
|
|
|
|
|
def test_permission_denied_error_is_retried_when_other_deployments_exist():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "openrouter/openai/gpt-4o", "api_key": "sk-test"}},
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-test"}},
|
|
]
|
|
)
|
|
|
|
assert (
|
|
router.should_retry_this_error(
|
|
error=_permission_denied_error(),
|
|
healthy_deployments=router.model_list,
|
|
all_deployments=router.model_list,
|
|
)
|
|
is True
|
|
)
|
|
|
|
|
|
class _AllowlistFallbackAccessCheck:
|
|
def __init__(self, allowed_models: frozenset[str]):
|
|
self.allowed_models = allowed_models
|
|
self.checked_models = []
|
|
|
|
async def __call__(self, *, model, request_kwargs, ctx):
|
|
self.checked_models.append(model)
|
|
return model in self.allowed_models
|
|
|
|
|
|
def _router_with_failing_primary(fallback_access_check) -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {
|
|
"model": "openai/primary",
|
|
"api_key": "k",
|
|
"mock_response": Exception("primary is down"),
|
|
},
|
|
},
|
|
{
|
|
"model_name": "secret-fallback",
|
|
"litellm_params": {
|
|
"model": "openai/secret",
|
|
"api_key": "k",
|
|
"mock_response": "served by secret-fallback",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"primary": ["secret-fallback"]}],
|
|
num_retries=0,
|
|
fallback_access_check=fallback_access_check,
|
|
)
|
|
|
|
|
|
class _DenyFallbackBudgetCheck:
|
|
async def __call__(self, *, model: str, request_kwargs: Mapping[str, object], ctx: RouterContext) -> bool:
|
|
return False
|
|
|
|
|
|
def _router_with_failing_fallback_chain(
|
|
fallback_targets: tuple[str, ...],
|
|
*,
|
|
fallback_access_check: _AllowlistFallbackAccessCheck | None = None,
|
|
fallback_budget_check: _DenyFallbackBudgetCheck | None = None,
|
|
) -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {
|
|
"model": "openai/primary",
|
|
"api_key": "k",
|
|
"mock_response": Exception("primary is down"),
|
|
},
|
|
},
|
|
*(
|
|
{
|
|
"model_name": target,
|
|
"litellm_params": {
|
|
"model": f"openai/{target}",
|
|
"api_key": "k",
|
|
"mock_response": Exception(f"{target} is down"),
|
|
},
|
|
}
|
|
for target in fallback_targets
|
|
),
|
|
],
|
|
fallbacks=[{"primary": list(fallback_targets)}],
|
|
num_retries=0,
|
|
fallback_access_check=fallback_access_check,
|
|
fallback_budget_check=fallback_budget_check,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_access_check_blocks_config_fallback_the_caller_cannot_use():
|
|
access_check = _AllowlistFallbackAccessCheck(allowed_models=frozenset())
|
|
router = _router_with_failing_primary(access_check)
|
|
|
|
with pytest.raises(Exception, match="primary is down"):
|
|
await router.acompletion(model="primary", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert access_check.checked_models == ["secret-fallback"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_access_check_reports_denied_fallback_as_skipped(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(litellm, "expose_router_debug_in_errors", True)
|
|
access_check: Final = _AllowlistFallbackAccessCheck(allowed_models=frozenset())
|
|
router: Final = _router_with_failing_primary(access_check)
|
|
primary_only_router: Final = Router(model_list=router.model_list, fallbacks=[], num_retries=0)
|
|
|
|
with pytest.raises(litellm.InternalServerError) as primary_exception:
|
|
await primary_only_router.acompletion(model="primary", messages=[{"role": "user", "content": "hi"}])
|
|
with pytest.raises(litellm.InternalServerError) as fallback_exception:
|
|
await router.acompletion(model="primary", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
message: Final = str(fallback_exception.value)
|
|
assert type(fallback_exception.value) is type(primary_exception.value)
|
|
assert fallback_exception.value.status_code == primary_exception.value.status_code
|
|
assert message.count("primary is down") == 1
|
|
assert "Fallback to secret-fallback was skipped because the caller is not authorized to call it." in message
|
|
assert "also failed" not in message
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_access_check_separates_denied_and_failed_targets(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(litellm, "expose_router_debug_in_errors", True)
|
|
access_check: Final = _AllowlistFallbackAccessCheck(allowed_models=frozenset({"allowed-fallback"}))
|
|
router: Final = _router_with_failing_fallback_chain(
|
|
("secret-fallback", "allowed-fallback"),
|
|
fallback_access_check=access_check,
|
|
)
|
|
|
|
with pytest.raises(litellm.InternalServerError) as exception:
|
|
await router.acompletion(model="primary", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
message: Final = str(exception.value)
|
|
assert "Fallback to allowed-fallback also failed:" in message
|
|
assert "Fallback to secret-fallback was skipped because the caller is not authorized" in message
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_budget_check_reports_skipped_targets(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(litellm, "expose_router_debug_in_errors", True)
|
|
router: Final = _router_with_failing_fallback_chain(
|
|
("paid-fallback",),
|
|
fallback_budget_check=_DenyFallbackBudgetCheck(),
|
|
)
|
|
|
|
with pytest.raises(litellm.InternalServerError) as exception:
|
|
await router.acompletion(model="primary", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
message: Final = str(exception.value)
|
|
assert "Fallback to paid-fallback was skipped because the caller is over budget for it." in message
|
|
assert "also failed" not in message
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_access_check_lets_an_authorized_config_fallback_through():
|
|
router = _router_with_failing_primary(_AllowlistFallbackAccessCheck(allowed_models=frozenset({"secret-fallback"})))
|
|
|
|
response = await router.acompletion(model="primary", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert response.choices[0].message.content == "served by secret-fallback"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_without_fallback_access_check_attempts_every_config_fallback():
|
|
router = _router_with_failing_primary(None)
|
|
|
|
response = await router.acompletion(model="primary", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert response.choices[0].message.content == "served by secret-fallback"
|
|
|
|
|
|
def _resolution_router() -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{"model_name": "pinned", "litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-test"}},
|
|
{"model_name": "pooled", "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-test"}},
|
|
{"model_name": "pooled", "litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-test"}},
|
|
{"model_name": "bedrock/*", "litellm_params": {"model": "bedrock/*", "api_key": "sk-test"}},
|
|
],
|
|
model_group_alias={"nickname": "pinned"},
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model_name,expected",
|
|
[
|
|
("pinned", ("openai/gpt-4o",)),
|
|
("nickname", ("openai/gpt-4o",)),
|
|
("pooled", ("openai/gpt-4o-mini", "anthropic/claude-haiku-4-5")),
|
|
("bedrock/anthropic.claude-3-5-sonnet", ("bedrock/anthropic.claude-3-5-sonnet",)),
|
|
("never-configured", ()),
|
|
],
|
|
ids=["exact-name", "model-group-alias", "every-member-of-a-pool", "wildcard-expands", "resolves-to-nothing"],
|
|
)
|
|
def test_resolved_litellm_models_answers_through_every_channel_a_request_uses(
|
|
model_name: str, expected: tuple[str, ...]
|
|
) -> None:
|
|
"""A caller comparing two names by what serves them needs each channel the request path
|
|
composes, since the deployment name an admin picked carries no information on its own.
|
|
|
|
`resolves-to-nothing` is the contract that keeps the fallback out of here: an empty
|
|
result is not "the call fails", so what to do about it stays each caller's policy.
|
|
"""
|
|
assert set(_resolution_router().resolved_litellm_models(model_name)) == set(expected)
|
|
|
|
|
|
def test_resolved_litellm_models_base_model_precedence_is_opt_in() -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "pooled",
|
|
"litellm_params": {"model": "openai/deployment-a", "api_key": "sk-test", "base_model": "gpt-5.5"},
|
|
"model_info": {"base_model": "gpt-6-astra"},
|
|
},
|
|
{
|
|
"model_name": "pooled",
|
|
"litellm_params": {"model": "openai/deployment-b", "api_key": "sk-test", "base_model": "gpt-5.6-sol"},
|
|
},
|
|
{
|
|
"model_name": "team-deployment",
|
|
"litellm_params": {"model": "openai/deployment-c", "api_key": "sk-test"},
|
|
"model_info": {
|
|
"base_model": "gpt-6-astra",
|
|
"team_id": "test-team",
|
|
"team_public_model_name": "team-model",
|
|
},
|
|
},
|
|
],
|
|
model_group_alias={"nickname": "pooled"},
|
|
)
|
|
assert router.resolved_litellm_models("nickname") == ("openai/deployment-a", "openai/deployment-b")
|
|
assert router.resolved_litellm_models("nickname", use_base_model=True) == ("gpt-6-astra", "gpt-5.6-sol")
|
|
assert router.resolved_litellm_models("team-model", "test-team") == ("openai/deployment-c",)
|
|
assert router.resolved_litellm_models("team-model", "test-team", use_base_model=True) == ("gpt-6-astra",)
|
|
assert router.resolved_litellm_models("never-configured", use_base_model=True) == ()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_aspeech_without_voice_dispatches_ref_audio_cloning(respx_mock, monkeypatch):
|
|
import base64
|
|
|
|
monkeypatch.setenv("MISTRAL_API_KEY", "sk-mistral-test")
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
audio_bytes = b"RIFFfake-wav-bytes"
|
|
respx_mock.post("https://api.mistral.ai/v1/audio/speech").respond(
|
|
json={"audio_data": base64.b64encode(audio_bytes).decode()}
|
|
)
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "voxtral-tts",
|
|
"litellm_params": {"model": "mistral/voxtral-mini-tts-2603"},
|
|
}
|
|
]
|
|
)
|
|
|
|
response = await router.aspeech(model="voxtral-tts", input="clone me", ref_audio="ZmFrZQ==")
|
|
|
|
request_body = json.loads(respx_mock.calls.last.request.content)
|
|
assert request_body == {"model": "voxtral-mini-tts-2603", "input": "clone me", "ref_audio": "ZmFrZQ=="}
|
|
assert response.content == audio_bytes
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_aspeech_without_voice_keeps_deployment_default_voice(respx_mock, monkeypatch):
|
|
import base64
|
|
|
|
monkeypatch.setenv("MISTRAL_API_KEY", "sk-mistral-test")
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
audio_bytes = b"RIFFfake-wav-bytes"
|
|
respx_mock.post("https://api.mistral.ai/v1/audio/speech").respond(
|
|
json={"audio_data": base64.b64encode(audio_bytes).decode()}
|
|
)
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "voxtral-tts",
|
|
"litellm_params": {"model": "mistral/voxtral-mini-tts-2603", "voice": "en_paul_neutral"},
|
|
}
|
|
]
|
|
)
|
|
|
|
await router.aspeech(model="voxtral-tts", input="use my default")
|
|
|
|
request_body = json.loads(respx_mock.calls.last.request.content)
|
|
assert request_body["voice_id"] == "en_paul_neutral"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_aspeech_request_voice_overrides_deployment_default(respx_mock, monkeypatch):
|
|
import base64
|
|
|
|
monkeypatch.setenv("MISTRAL_API_KEY", "sk-mistral-test")
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
audio_bytes = b"RIFFfake-wav-bytes"
|
|
respx_mock.post("https://api.mistral.ai/v1/audio/speech").respond(
|
|
json={"audio_data": base64.b64encode(audio_bytes).decode()}
|
|
)
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "voxtral-tts",
|
|
"litellm_params": {"model": "mistral/voxtral-mini-tts-2603", "voice": "en_paul_neutral"},
|
|
}
|
|
]
|
|
)
|
|
|
|
await router.aspeech(model="voxtral-tts", input="override me", voice="gb_oliver_neutral")
|
|
|
|
request_body = json.loads(respx_mock.calls.last.request.content)
|
|
assert request_body["voice_id"] == "gb_oliver_neutral"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_prompt_management_factory_marks_injection_for_every_deployment(monkeypatch):
|
|
"""The factory stamps a provisional deployment's model_info into kwargs before the
|
|
prompt pass runs, then routes on the returned model, so any deployment can end up
|
|
billed. An injection recorded there must carry the every-deployment sentinel, never
|
|
the provisional deployment's id, or a differently-billed deployment loses the credit."""
|
|
import time
|
|
|
|
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLogging
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "cached-claude",
|
|
"litellm_params": {
|
|
"model": "anthropic_cache_control_hook/claude-sonnet-5",
|
|
"prompt_id": "cache-points",
|
|
},
|
|
"model_info": {"id": "provisional-dep"},
|
|
}
|
|
]
|
|
)
|
|
captured: dict = {}
|
|
|
|
async def _capture_acompletion(**kwargs):
|
|
captured.update(kwargs)
|
|
return litellm.ModelResponse()
|
|
|
|
monkeypatch.setattr(litellm, "acompletion", _capture_acompletion)
|
|
logging_obj = LiteLLMLogging(
|
|
model="cached-claude",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
stream=False,
|
|
call_type="acompletion",
|
|
start_time=time.time(),
|
|
litellm_call_id="lit-6445",
|
|
function_id="f",
|
|
)
|
|
await router.acompletion(
|
|
model="cached-claude",
|
|
messages=[
|
|
{"role": "system", "content": "a static system prompt"},
|
|
{"role": "user", "content": "hi"},
|
|
],
|
|
cache_control_injection_points=[{"location": "message", "role": "system"}],
|
|
litellm_logging_obj=logging_obj,
|
|
)
|
|
bucket = captured.get("litellm_metadata") or captured["metadata"]
|
|
assert captured["model_info"]["id"] == "provisional-dep"
|
|
assert bucket["litellm_gateway_injected_cache"] == ""
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"retry_policy,upstream_status,error_type,expected_upstream_calls",
|
|
[
|
|
({"ServiceUnavailableErrorRetries": 0}, 503, litellm.ServiceUnavailableError, 1),
|
|
({"ServiceUnavailableErrorRetries": 1}, 503, litellm.ServiceUnavailableError, 2),
|
|
({"InternalServerErrorRetries": 0}, 500, litellm.InternalServerError, 1),
|
|
({"DefaultRetries": 0}, 502, litellm.BadGatewayError, 1),
|
|
({"DefaultRetries": 0, "ServiceUnavailableErrorRetries": 1}, 503, litellm.ServiceUnavailableError, 2),
|
|
({"ServiceUnavailableErrorRetries": 0}, 502, litellm.BadGatewayError, 3),
|
|
({"BadRequestErrorRetries": 2}, 400, litellm.BadRequestError, 3),
|
|
],
|
|
)
|
|
async def test_router_retry_policy_controls_upstream_attempt_count(
|
|
monkeypatch: pytest.MonkeyPatch, retry_policy, upstream_status, error_type, expected_upstream_calls
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://retry-policy.local/v1",
|
|
},
|
|
}
|
|
],
|
|
num_retries=2,
|
|
retry_policy=retry_policy,
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
upstream = respx_mock.post("https://retry-policy.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
upstream_status,
|
|
headers={"retry-after": "0"},
|
|
json={"error": {"message": "model is down", "type": "server_error"}},
|
|
)
|
|
)
|
|
with pytest.raises(error_type):
|
|
await router.acompletion(model="gpt-5.6", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert upstream.call_count == expected_upstream_calls
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"error_body,error_type",
|
|
[
|
|
({"message": "model is down", "type": "server_error"}, litellm.NotFoundError),
|
|
({"message": "Response with id 'resp_x' not found.", "type": "invalid_request_error"}, litellm.NotFoundError),
|
|
],
|
|
)
|
|
@pytest.mark.parametrize(
|
|
"retry_policy,expected_upstream_calls",
|
|
[
|
|
({"DefaultRetries": 3}, 4),
|
|
({"DefaultRetries": 3, "NotFoundErrorRetries": 0}, 1),
|
|
({"NotFoundErrorRetries": 2}, 3),
|
|
],
|
|
)
|
|
async def test_router_not_found_retries_governs_every_404_shape(
|
|
monkeypatch: pytest.MonkeyPatch, retry_policy, expected_upstream_calls, error_body, error_type
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://retry-policy.local/v1",
|
|
},
|
|
}
|
|
],
|
|
num_retries=2,
|
|
retry_policy=retry_policy,
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
upstream = respx_mock.post("https://retry-policy.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(404, headers={"retry-after": "0"}, json={"error": error_body})
|
|
)
|
|
with pytest.raises(error_type) as raised:
|
|
await router.acompletion(model="gpt-5.6", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert raised.value.status_code == 404
|
|
assert upstream.call_count == expected_upstream_calls
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_generic_call_keeps_the_deployment_name_of_an_azure_ai_model_on_an_azure_openai_host(monkeypatch):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "aoai-gpt",
|
|
"litellm_params": {
|
|
"model": "azure_ai/gpt-5.4-mini",
|
|
"api_base": "https://my-resource.openai.azure.com",
|
|
"api_key": "deployment-key",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
upstream = respx_mock.post(host="my-resource.openai.azure.com", path__regex=r"^/openai/.*responses$").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "resp_1",
|
|
"object": "response",
|
|
"created_at": 1,
|
|
"status": "completed",
|
|
"model": "gpt-5.4-mini",
|
|
"output": [
|
|
{
|
|
"type": "message",
|
|
"id": "msg_1",
|
|
"role": "assistant",
|
|
"status": "completed",
|
|
"content": [{"type": "output_text", "text": "hi", "annotations": []}],
|
|
}
|
|
],
|
|
},
|
|
)
|
|
)
|
|
await router.aresponses(model="aoai-gpt", input="hi")
|
|
|
|
assert json.loads(upstream.calls.last.request.content)["model"] == "gpt-5.4-mini"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"retry_policy,upstream_error",
|
|
[
|
|
(
|
|
{"BadRequestErrorRetries": 2},
|
|
{
|
|
"message": "This model's maximum context length is 16385 tokens",
|
|
"type": "invalid_request_error",
|
|
"code": "context_length_exceeded",
|
|
},
|
|
),
|
|
(
|
|
{"ContentPolicyViolationErrorRetries": 2},
|
|
{
|
|
"message": "Your request was rejected as a result of our safety system",
|
|
"type": "invalid_request_error",
|
|
"code": "content_policy_violation",
|
|
},
|
|
),
|
|
],
|
|
)
|
|
async def test_router_retry_policy_400_retries_on_sibling_deployment(
|
|
monkeypatch: pytest.MonkeyPatch, retry_policy, upstream_error
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://rejecting.local/v1",
|
|
"weight": 1,
|
|
},
|
|
"model_info": {"id": "rejecting"},
|
|
},
|
|
{
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://accepting.local/v1",
|
|
"weight": 0,
|
|
},
|
|
"model_info": {"id": "accepting"},
|
|
},
|
|
],
|
|
num_retries=2,
|
|
retry_policy=retry_policy,
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
rejecting = respx_mock.post("https://rejecting.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(400, json={"error": upstream_error})
|
|
)
|
|
accepting = respx_mock.post("https://accepting.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-lit-7036",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-5.6",
|
|
"choices": [
|
|
{"index": 0, "message": {"role": "assistant", "content": "hi back"}, "finish_reason": "stop"}
|
|
],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 2, "total_tokens": 3},
|
|
},
|
|
)
|
|
)
|
|
response = await router.acompletion(model="gpt-5.6", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert rejecting.call_count == 1
|
|
assert accepting.call_count == 1
|
|
assert response.choices[0].message.content == "hi back"
|
|
assert response._hidden_params["additional_headers"]["x-litellm-attempted-retries"] == 1
|
|
|
|
|
|
_UPSTREAM_400 = {"message": "upstream refused this request", "type": "invalid_request_error", "code": "bad_request"}
|
|
|
|
|
|
def _retry_skip_deployment(deployment_id, host, litellm_params=None, model_info=None):
|
|
return {
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": f"https://{host}.local/v1",
|
|
**(litellm_params or {}),
|
|
},
|
|
"model_info": {"id": deployment_id, **(model_info or {})},
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"value,expected",
|
|
[
|
|
(("first", "second"), ("first", "second")),
|
|
((), ()),
|
|
(("first", 7, None, "second"), ("first", "second")),
|
|
(None, ()),
|
|
(7, ()),
|
|
("first", ()),
|
|
(["first"], ()),
|
|
({"first": True}, ()),
|
|
(object(), ()),
|
|
],
|
|
)
|
|
def test_router_as_retry_skipped_deployment_ids_keeps_only_a_tuple_of_strings(value, expected):
|
|
from litellm.litellm_core_utils.routing_common_utils import as_retry_skipped_deployment_ids
|
|
|
|
assert as_retry_skipped_deployment_ids(value) == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"deployment_ids,skipped,expected",
|
|
[
|
|
(["rejecting", "sibling"], ("rejecting",), ["sibling"]),
|
|
(["rejecting"], ("rejecting",), ["rejecting"]),
|
|
(["rejecting", "sibling"], ("rejecting", "sibling"), ["rejecting", "sibling"]),
|
|
(["rejecting", "sibling"], (), ["rejecting", "sibling"]),
|
|
(["rejecting", "sibling"], None, ["rejecting", "sibling"]),
|
|
(["rejecting", "sibling"], ("absent",), ["rejecting", "sibling"]),
|
|
(["rejecting", "sibling"], 7, ["rejecting", "sibling"]),
|
|
(["rejecting", "sibling"], "rejecting", ["rejecting", "sibling"]),
|
|
(["rejecting", "sibling"], ["rejecting"], ["rejecting", "sibling"]),
|
|
(["rejecting", "sibling"], {"rejecting": True}, ["rejecting", "sibling"]),
|
|
(["rejecting", "sibling"], ("rejecting", 7), ["sibling"]),
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_router_healthy_deployments_keep_the_last_candidate_a_retry_skipped(deployment_ids, skipped, expected):
|
|
router = litellm.Router(
|
|
model_list=[_retry_skip_deployment(deployment_id, deployment_id) for deployment_id in deployment_ids],
|
|
disable_cooldowns=True,
|
|
)
|
|
request_kwargs = {"_retry_skipped_deployment_ids": skipped}
|
|
|
|
healthy_deployments = await router.async_get_healthy_deployments(model="gpt-5.6", request_kwargs=request_kwargs)
|
|
|
|
assert sorted(deployment["model_info"]["id"] for deployment in healthy_deployments) == sorted(expected)
|
|
assert "_retry_skipped_deployment_ids" not in request_kwargs
|
|
|
|
|
|
@pytest.mark.parametrize("client_supplied", [7, "rejecting", ["rejecting"], {"rejecting": True}, object()])
|
|
@pytest.mark.asyncio
|
|
async def test_router_retry_policy_400_keeps_upstream_error_when_a_client_forges_the_skip_list(
|
|
monkeypatch: pytest.MonkeyPatch, client_supplied
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = litellm.Router(
|
|
model_list=[_retry_skip_deployment("rejecting", "rejecting"), _retry_skip_deployment("sibling", "sibling")],
|
|
num_retries=2,
|
|
retry_policy={"BadRequestErrorRetries": 2},
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
with respx.mock as respx_mock:
|
|
respx_mock.post("https://rejecting.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(400, json={"error": _UPSTREAM_400})
|
|
)
|
|
respx_mock.post("https://sibling.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(400, json={"error": _UPSTREAM_400})
|
|
)
|
|
with pytest.raises(litellm.BadRequestError) as raised:
|
|
await router.acompletion(
|
|
model="gpt-5.6",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
_retry_skipped_deployment_ids=client_supplied,
|
|
)
|
|
|
|
assert "upstream refused this request" in str(raised.value)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_retry_policy_400_keeps_upstream_error_on_order_fallback_hop(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = litellm.Router(
|
|
model_list=[
|
|
_retry_skip_deployment("order1", "order1", litellm_params={"order": 1}),
|
|
_retry_skip_deployment("order2", "order2", litellm_params={"order": 2}),
|
|
],
|
|
num_retries=2,
|
|
retry_policy={"BadRequestErrorRetries": 2},
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
with respx.mock as respx_mock:
|
|
order1 = respx_mock.post("https://order1.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(400, json={"error": _UPSTREAM_400})
|
|
)
|
|
order2 = respx_mock.post("https://order2.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(400, json={"error": _UPSTREAM_400})
|
|
)
|
|
with pytest.raises(litellm.BadRequestError) as raised:
|
|
await router.acompletion(model="gpt-5.6", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert "upstream refused this request" in str(raised.value)
|
|
assert "No deployments available" not in str(raised.value)
|
|
assert order1.call_count >= 1
|
|
assert order2.call_count >= 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_retry_policy_400_keeps_upstream_error_when_tags_narrow_the_group(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = litellm.Router(
|
|
model_list=[
|
|
_retry_skip_deployment(
|
|
"tagged", "tagged", litellm_params={"tags": ["free"]}, model_info={"enable_tag_filtering": True}
|
|
),
|
|
_retry_skip_deployment("untagged", "untagged", model_info={"enable_tag_filtering": True}),
|
|
],
|
|
num_retries=2,
|
|
retry_policy={"BadRequestErrorRetries": 2},
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
with respx.mock as respx_mock:
|
|
tagged = respx_mock.post("https://tagged.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(400, json={"error": _UPSTREAM_400})
|
|
)
|
|
untagged = respx_mock.post("https://untagged.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(400, json={"error": _UPSTREAM_400})
|
|
)
|
|
with pytest.raises(litellm.BadRequestError) as raised:
|
|
await router.acompletion(
|
|
model="gpt-5.6",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
metadata={"tags": ["free"]},
|
|
)
|
|
|
|
assert "upstream refused this request" in str(raised.value)
|
|
assert "No deployments available" not in str(raised.value)
|
|
assert tagged.call_count == 3
|
|
assert untagged.call_count == 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_retry_policy_400_never_returns_to_a_deployment_that_already_refused(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = litellm.Router(
|
|
model_list=[
|
|
_retry_skip_deployment("first-refuser", "first-refuser", litellm_params={"weight": 1}),
|
|
_retry_skip_deployment("second-refuser", "second-refuser", litellm_params={"weight": 0}),
|
|
_retry_skip_deployment("accepting", "accepting", litellm_params={"weight": 0}),
|
|
],
|
|
num_retries=3,
|
|
retry_policy={"BadRequestErrorRetries": 3},
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
with respx.mock as respx_mock:
|
|
first = respx_mock.post("https://first-refuser.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(400, json={"error": _UPSTREAM_400})
|
|
)
|
|
second = respx_mock.post("https://second-refuser.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(400, json={"error": _UPSTREAM_400})
|
|
)
|
|
accepting = respx_mock.post("https://accepting.local/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-lit-7036",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-5.6",
|
|
"choices": [
|
|
{"index": 0, "message": {"role": "assistant", "content": "hi back"}, "finish_reason": "stop"}
|
|
],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 2, "total_tokens": 3},
|
|
},
|
|
)
|
|
)
|
|
response = await router.acompletion(model="gpt-5.6", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
# The weighted pick starts on the first refuser; which zero-weight deployment comes next is random,
|
|
# but no refuser is tried twice and the accepting one serves.
|
|
assert first.call_count == 1
|
|
assert second.call_count <= 1
|
|
assert accepting.call_count == 1
|
|
assert response.choices[0].message.content == "hi back"
|
|
|
|
|
|
_LIT_7114_CHAT_OK = {
|
|
"id": "chatcmpl-lit-7114",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-5.6",
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "hi back"}, "finish_reason": "stop"}],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 2, "total_tokens": 3},
|
|
}
|
|
_LIT_7114_EMBEDDING_OK = {
|
|
"object": "list",
|
|
"data": [{"object": "embedding", "index": 0, "embedding": [0.1]}],
|
|
"model": "text-embedding-3-large",
|
|
"usage": {"prompt_tokens": 1, "total_tokens": 1},
|
|
}
|
|
_LIT_7114_IMAGE_OK = {"created": 1, "data": [{"b64_json": "aGk="}]}
|
|
_LIT_7114_BATCH_OK = {
|
|
"id": "batch_lit_7114",
|
|
"object": "batch",
|
|
"endpoint": "/v1/chat/completions",
|
|
"input_file_id": "file-lit-7114",
|
|
"completion_window": "24h",
|
|
"status": "validating",
|
|
"created_at": 1,
|
|
}
|
|
|
|
|
|
class _PassthroughAdapter(CustomLogger):
|
|
def translate_completion_input_params(self, kwargs: ChatCompletionRequest) -> ChatCompletionRequest:
|
|
return ChatCompletionRequest(**kwargs)
|
|
|
|
def translate_completion_output_params(self, response: litellm.ModelResponse) -> litellm.ModelResponse:
|
|
return response
|
|
|
|
|
|
def _lit_7114_router(litellm_model: str) -> litellm.Router:
|
|
api_base_suffix: Final = "" if litellm_model.startswith("cohere/") else "/v1"
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": litellm_model,
|
|
"api_key": "sk-fake",
|
|
"api_base": f"https://{host}.local{api_base_suffix}",
|
|
"weight": weight,
|
|
},
|
|
"model_info": {"id": host},
|
|
}
|
|
for host, weight in (("rejecting", 1), ("accepting", 0))
|
|
],
|
|
num_retries=2,
|
|
retry_policy={"BadRequestErrorRetries": 2},
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
|
|
def _lit_7114_mock_upstreams(
|
|
respx_mock: respx.MockRouter, path: str, refusal_status: int, success_body: Mapping[str, object] | bytes
|
|
) -> tuple[respx.Route, respx.Route]:
|
|
ok_response: Final = (
|
|
httpx.Response(200, content=success_body)
|
|
if isinstance(success_body, bytes)
|
|
else httpx.Response(200, json=success_body)
|
|
)
|
|
rejecting: Final = respx_mock.post(f"https://rejecting.local{path}").mock(
|
|
return_value=httpx.Response(
|
|
refusal_status, json={"error": _UPSTREAM_400, "message": "upstream refused this request"}
|
|
)
|
|
)
|
|
accepting: Final = respx_mock.post(f"https://accepting.local{path}").mock(return_value=ok_response)
|
|
return rejecting, accepting
|
|
|
|
|
|
_LIT_7114_ASYNC_ENTRYPOINTS: Final[
|
|
Mapping[str, tuple[str, str, int, Mapping[str, object] | bytes, Callable[[litellm.Router], Awaitable[object]]]]
|
|
] = {
|
|
"aembedding": (
|
|
"openai/text-embedding-3-large",
|
|
"/v1/embeddings",
|
|
400,
|
|
_LIT_7114_EMBEDDING_OK,
|
|
lambda router: router.aembedding(model="gpt-5.6", input="hi"),
|
|
),
|
|
"aimage_generation": (
|
|
"openai/gpt-image-1",
|
|
"/v1/images/generations",
|
|
400,
|
|
_LIT_7114_IMAGE_OK,
|
|
lambda router: router.aimage_generation(model="gpt-5.6", prompt="a cat"),
|
|
),
|
|
"atext_completion": (
|
|
"text-completion-openai/gpt-3.5-turbo-instruct",
|
|
"/v1/completions",
|
|
400,
|
|
{"id": "c", "object": "text_completion", "created": 1, "model": "i", "choices": [{"text": "hi", "index": 0}]},
|
|
lambda router: router.atext_completion(model="gpt-5.6", prompt="hi"),
|
|
),
|
|
"aspeech": (
|
|
"openai/gpt-4o-mini-tts",
|
|
"/v1/audio/speech",
|
|
400,
|
|
b"RIFF",
|
|
lambda router: router.aspeech(model="gpt-5.6", input="hi", voice="alloy"),
|
|
),
|
|
"atranscription": (
|
|
"openai/gpt-4o-transcribe",
|
|
"/v1/audio/transcriptions",
|
|
400,
|
|
{"text": "hi"},
|
|
lambda router: router.atranscription(model="gpt-5.6", file=("hi.wav", b"RIFF", "audio/wav")),
|
|
),
|
|
"arerank": (
|
|
"cohere/rerank-v3.5",
|
|
"/v2/rerank",
|
|
400,
|
|
{"id": "r", "results": [{"index": 0, "relevance_score": 0.9}], "meta": {}},
|
|
lambda router: router.arerank(model="gpt-5.6", query="hi", documents=["hi"]),
|
|
),
|
|
"aadapter_completion": (
|
|
"openai/gpt-5.6",
|
|
"/v1/chat/completions",
|
|
400,
|
|
_LIT_7114_CHAT_OK,
|
|
lambda router: router.aadapter_completion(
|
|
adapter_id="lit-7114", model="gpt-5.6", messages=[{"role": "user", "content": "hi"}]
|
|
),
|
|
),
|
|
"acreate_batch": (
|
|
"openai/gpt-5.6",
|
|
"/v1/batches",
|
|
401,
|
|
_LIT_7114_BATCH_OK,
|
|
lambda router: router.acreate_batch(
|
|
model="gpt-5.6", completion_window="24h", endpoint="/v1/chat/completions", input_file_id="file-lit-7114"
|
|
),
|
|
),
|
|
"acancel_batch": (
|
|
"openai/gpt-5.6",
|
|
"/v1/batches/batch_lit_7114/cancel",
|
|
401,
|
|
{**_LIT_7114_BATCH_OK, "status": "cancelling"},
|
|
lambda router: router.acancel_batch(model="gpt-5.6", batch_id="batch_lit_7114"),
|
|
),
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("entrypoint", sorted(_LIT_7114_ASYNC_ENTRYPOINTS))
|
|
@pytest.mark.asyncio
|
|
async def test_router_retry_moves_off_the_refusing_deployment_on_every_async_entrypoint(
|
|
monkeypatch: pytest.MonkeyPatch, entrypoint: str
|
|
):
|
|
litellm_model, path, refusal_status, success_body, call = _LIT_7114_ASYNC_ENTRYPOINTS[entrypoint]
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
monkeypatch.setattr(litellm, "adapters", [{"id": "lit-7114", "adapter": _PassthroughAdapter()}])
|
|
router: Final = _lit_7114_router(litellm_model)
|
|
|
|
with respx.mock as respx_mock:
|
|
rejecting, accepting = _lit_7114_mock_upstreams(respx_mock, path, refusal_status, success_body)
|
|
response: Final = await call(router)
|
|
|
|
assert response is not None
|
|
assert rejecting.call_count == 1
|
|
assert accepting.call_count == 1
|
|
|
|
|
|
_LIT_7114_SYNC_ENTRYPOINTS: Final[
|
|
Mapping[str, tuple[str, str, Mapping[str, object], Callable[[litellm.Router], object]]]
|
|
] = {
|
|
"embedding": (
|
|
"openai/text-embedding-3-large",
|
|
"/v1/embeddings",
|
|
_LIT_7114_EMBEDDING_OK,
|
|
lambda router: router.embedding(model="gpt-5.6", input="hi"),
|
|
),
|
|
"image_generation": (
|
|
"openai/gpt-image-1",
|
|
"/v1/images/generations",
|
|
_LIT_7114_IMAGE_OK,
|
|
lambda router: router.image_generation(model="gpt-5.6", prompt="a cat"),
|
|
),
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("entrypoint", sorted(_LIT_7114_SYNC_ENTRYPOINTS))
|
|
def test_router_retry_policy_400_moves_off_the_refusing_deployment_on_every_sync_entrypoint(
|
|
monkeypatch: pytest.MonkeyPatch, entrypoint: str
|
|
):
|
|
litellm_model, path, success_body, call = _LIT_7114_SYNC_ENTRYPOINTS[entrypoint]
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _lit_7114_router(litellm_model)
|
|
|
|
with respx.mock as respx_mock:
|
|
rejecting, accepting = _lit_7114_mock_upstreams(respx_mock, path, 400, success_body)
|
|
response: Final = call(router)
|
|
|
|
assert response is not None
|
|
assert rejecting.call_count == 1
|
|
assert accepting.call_count == 1
|
|
|
|
|
|
_TRANSCRIPTION_AUDIO: Final = ("speech.wav", b"RIFF", "audio/wav")
|
|
_TRANSCRIPTION_API_BASE: Final = "https://transcriber.local/v1"
|
|
|
|
|
|
def _transcription_deployment(
|
|
model_name: str, deployment_id: str, litellm_model: str, weight: int
|
|
) -> DeploymentTypedDict:
|
|
return {
|
|
"model_name": model_name,
|
|
"litellm_params": {
|
|
"model": litellm_model,
|
|
"api_key": "sk-fake",
|
|
"api_base": _TRANSCRIPTION_API_BASE,
|
|
"weight": weight,
|
|
},
|
|
"model_info": {"id": deployment_id},
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("policy_scope", ["none", "router", "model_group", "request"])
|
|
@pytest.mark.parametrize(
|
|
"caller_credentials",
|
|
({}, {"api_key": "caller-key"}, {"api_base": _TRANSCRIPTION_API_BASE}, {"base_url": _TRANSCRIPTION_API_BASE}),
|
|
ids=("configured", "api_key", "api_base", "base_url"),
|
|
)
|
|
async def test_router_retries_a_transcription_off_a_deployment_whose_provider_cannot_transcribe(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
policy_scope: Literal["none", "router", "model_group", "request"],
|
|
caller_credentials: Mapping[str, str],
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
policy: Final = RetryPolicy(BadRequestErrorRetries=0)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_transcription_deployment("transcriber", "cannot-transcribe", "anthropic/claude-sonnet-5-5", weight=1),
|
|
_transcription_deployment("transcriber", "can-transcribe", "openai/gpt-transcribe", weight=0),
|
|
],
|
|
num_retries=1,
|
|
retry_policy=policy if policy_scope == "router" else None,
|
|
model_group_retry_policy={"transcriber": policy} if policy_scope == "model_group" else None,
|
|
disable_cooldowns=True,
|
|
)
|
|
request_kwargs: Final = {"model_group_retry_policy": {"transcriber": policy}} if policy_scope == "request" else {}
|
|
|
|
with respx.mock as respx_mock:
|
|
upstream: Final = respx_mock.post(f"{_TRANSCRIPTION_API_BASE}/audio/transcriptions").mock(
|
|
return_value=httpx.Response(200, json={"text": "hi"})
|
|
)
|
|
response: Final = await router.atranscription(
|
|
model="transcriber", file=_TRANSCRIPTION_AUDIO, **request_kwargs, **caller_credentials
|
|
)
|
|
|
|
assert response.text == "hi"
|
|
assert upstream.call_count == 1
|
|
|
|
|
|
@pytest.mark.parametrize("deployment_count", [1, 2, 3])
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"caller_credentials",
|
|
({}, {"api_key": "caller-key"}, {"api_base": _TRANSCRIPTION_API_BASE}, {"base_url": _TRANSCRIPTION_API_BASE}),
|
|
ids=("configured", "api_key", "api_base", "base_url"),
|
|
)
|
|
async def test_router_tries_each_deployment_that_cannot_transcribe_once_then_answers_400(
|
|
deployment_count: int, caller_credentials: Mapping[str, str]
|
|
) -> None:
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_transcription_deployment("transcriber", f"cannot-transcribe-{index}", "anthropic/claude-sonnet-5-5", 1)
|
|
for index in range(deployment_count)
|
|
],
|
|
num_retries=5,
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
with respx.mock as respx_mock, pytest.raises(litellm.UnsupportedEndpointError) as raised:
|
|
await router.atranscription(model="transcriber", file=_TRANSCRIPTION_AUDIO, **caller_credentials)
|
|
|
|
assert raised.value.status_code == 400
|
|
assert router.fail_calls["transcriber"] == deployment_count
|
|
assert not respx_mock.calls
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_falls_back_from_a_group_that_cannot_transcribe_when_unsupported_params_do_not(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
_transcription_deployment("chat-only", "cannot-transcribe", "anthropic/claude-sonnet-5-5", weight=1),
|
|
_transcription_deployment("transcriber", "can-transcribe", "openai/gpt-transcribe", weight=1),
|
|
],
|
|
fallbacks=[{"chat-only": ["transcriber"]}],
|
|
fallback_on_unsupported_params=False,
|
|
num_retries=0,
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
with respx.mock as respx_mock:
|
|
upstream: Final = respx_mock.post(f"{_TRANSCRIPTION_API_BASE}/audio/transcriptions").mock(
|
|
return_value=httpx.Response(200, json={"text": "hi"})
|
|
)
|
|
response: Final = await router.atranscription(model="chat-only", file=_TRANSCRIPTION_AUDIO)
|
|
|
|
assert response.text == "hi"
|
|
assert upstream.call_count == 1
|
|
|
|
|
|
def _make_failure_logging_obj():
|
|
return LiteLLMLogging(
|
|
model="gpt-5.6",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
stream=False,
|
|
call_type="acompletion",
|
|
start_time=datetime.now(),
|
|
litellm_call_id="lit-6960",
|
|
function_id="f",
|
|
)
|
|
|
|
|
|
async def _assert_router_failure_logging_is_coordinated(logging_obj, trigger, expected_exception):
|
|
"""The sync failure_handler must not start until async_failure_handler has finished on the shared logging_obj."""
|
|
events: list[str] = []
|
|
sync_done = threading.Event()
|
|
|
|
async def _async_failure(*args, **kwargs):
|
|
events.append("async_start")
|
|
await asyncio.sleep(0.05)
|
|
events.append("async_end")
|
|
|
|
def _sync_failure(*args, **kwargs):
|
|
events.append("sync_start")
|
|
sync_done.set()
|
|
|
|
with (
|
|
patch.object(logging_obj, "async_failure_handler", side_effect=_async_failure),
|
|
patch.object(logging_obj, "failure_handler", side_effect=_sync_failure),
|
|
patch.object(logging_obj, "_should_run_sync_failure_callbacks_for_async_calls", return_value=True),
|
|
):
|
|
with pytest.raises(expected_exception):
|
|
await trigger()
|
|
pending = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
|
|
await asyncio.gather(*pending)
|
|
assert await asyncio.to_thread(sync_done.wait, 5), "failure_handler never ran"
|
|
|
|
assert events == ["async_start", "async_end", "sync_start"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"hook_error",
|
|
[
|
|
litellm.RateLimitError(message="rpm exceeded", llm_provider="openai", model="gpt-5.6"),
|
|
RuntimeError("pre call check blew up"),
|
|
],
|
|
)
|
|
async def test_async_routing_strategy_pre_call_checks_failure_logging_is_coordinated(hook_error):
|
|
class _RaisingPreCallCheck(CustomLogger):
|
|
async def async_pre_call_check(self, deployment, parent_otel_span):
|
|
raise hook_error
|
|
|
|
router = litellm.Router(
|
|
model_list=[{"model_name": "gpt-5.6", "litellm_params": {"model": "openai/gpt-5.6", "api_key": "sk-fake"}}]
|
|
)
|
|
deployment = router.model_list[0]
|
|
logging_obj = _make_failure_logging_obj()
|
|
|
|
with patch.object(litellm, "callbacks", [_RaisingPreCallCheck()]): # test-quality-ok: router reads this global
|
|
await _assert_router_failure_logging_is_coordinated(
|
|
logging_obj,
|
|
lambda: router.async_routing_strategy_pre_call_checks(
|
|
deployment=deployment, parent_otel_span=None, logging_obj=logging_obj
|
|
),
|
|
type(hook_error),
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_callback_filter_deployments_failure_logging_is_coordinated():
|
|
class _RaisingFilter(CustomLogger):
|
|
async def async_filter_deployments(self, *args, **kwargs):
|
|
raise RuntimeError("filter blew up")
|
|
|
|
router = litellm.Router(
|
|
model_list=[{"model_name": "gpt-5.6", "litellm_params": {"model": "openai/gpt-5.6", "api_key": "sk-fake"}}]
|
|
)
|
|
logging_obj = _make_failure_logging_obj()
|
|
|
|
with patch.object(litellm, "callbacks", [_RaisingFilter()]): # test-quality-ok: router reads this global
|
|
await _assert_router_failure_logging_is_coordinated(
|
|
logging_obj,
|
|
lambda: router.async_callback_filter_deployments(
|
|
model="gpt-5.6",
|
|
healthy_deployments=router.model_list,
|
|
messages=None,
|
|
parent_otel_span=None,
|
|
request_kwargs={},
|
|
logging_obj=logging_obj,
|
|
),
|
|
RuntimeError,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_get_available_deployment_failure_logging_is_coordinated():
|
|
router = litellm.Router(
|
|
model_list=[{"model_name": "gpt-5.6", "litellm_params": {"model": "openai/gpt-5.6", "api_key": "sk-fake"}}]
|
|
)
|
|
logging_obj = _make_failure_logging_obj()
|
|
|
|
await _assert_router_failure_logging_is_coordinated(
|
|
logging_obj,
|
|
lambda: router.async_get_available_deployment(
|
|
model="model-that-is-not-configured",
|
|
request_kwargs={"litellm_logging_obj": logging_obj},
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
),
|
|
litellm.BadRequestError,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_get_available_deployment_for_pass_through_failure_logging_is_coordinated():
|
|
router = litellm.Router(
|
|
model_list=[{"model_name": "gpt-5.6", "litellm_params": {"model": "openai/gpt-5.6", "api_key": "sk-fake"}}]
|
|
)
|
|
logging_obj = _make_failure_logging_obj()
|
|
|
|
await _assert_router_failure_logging_is_coordinated(
|
|
logging_obj,
|
|
lambda: router.async_get_available_deployment_for_pass_through(
|
|
model="gpt-5.6",
|
|
request_kwargs={"litellm_logging_obj": logging_obj},
|
|
),
|
|
litellm.BadRequestError,
|
|
)
|
|
|
|
|
|
class _InFlightTracker:
|
|
def __init__(self) -> None:
|
|
self.current = 0
|
|
self.peak = 0
|
|
|
|
def enter(self) -> None:
|
|
self.current += 1
|
|
self.peak = max(self.peak, self.current)
|
|
|
|
def exit(self) -> None:
|
|
self.current -= 1
|
|
|
|
|
|
_SSE_CHUNKS: Final[tuple[bytes, ...]] = tuple(
|
|
b'data: {"id":"c","object":"chat.completion.chunk","created":1,"model":"gpt-5.6",'
|
|
b'"choices":[{"index":0,"delta":{"content":"x"},"finish_reason":null}]}\n\n'
|
|
for _ in range(5)
|
|
) + (
|
|
b'data: {"id":"c","object":"chat.completion.chunk","created":1,"model":"gpt-5.6",'
|
|
b'"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n',
|
|
)
|
|
|
|
|
|
class _CountingSSEStream(httpx.AsyncByteStream):
|
|
def __init__(self, tracker: _InFlightTracker) -> None:
|
|
self._tracker = tracker
|
|
self._in_flight = False
|
|
|
|
def _finish(self) -> None:
|
|
if self._in_flight:
|
|
self._in_flight = False
|
|
self._tracker.exit()
|
|
|
|
async def __aiter__(self):
|
|
self._in_flight = True
|
|
self._tracker.enter()
|
|
try:
|
|
for chunk in _SSE_CHUNKS:
|
|
await asyncio.sleep(0.02)
|
|
yield chunk
|
|
finally:
|
|
await self.aclose()
|
|
yield b"data: [DONE]\n\n"
|
|
|
|
async def aclose(self) -> None:
|
|
await asyncio.sleep(0.02)
|
|
self._finish()
|
|
|
|
|
|
def _max_parallel_router(max_parallel_requests: int) -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://max-parallel.local/v1",
|
|
"max_parallel_requests": max_parallel_requests,
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("stream", [False, True])
|
|
async def test_router_max_parallel_requests_admits_the_cap_and_rejects_the_rest_with_429(
|
|
monkeypatch: pytest.MonkeyPatch, stream: bool
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
tracker: Final = _InFlightTracker()
|
|
router: Final = _max_parallel_router(max_parallel_requests=2)
|
|
|
|
async def upstream(request: httpx.Request) -> httpx.Response:
|
|
if stream:
|
|
return httpx.Response(
|
|
200, headers={"content-type": "text/event-stream"}, stream=_CountingSSEStream(tracker)
|
|
)
|
|
tracker.enter()
|
|
await asyncio.sleep(0.05)
|
|
tracker.exit()
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "c",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-5.6",
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "x"}, "finish_reason": "stop"}],
|
|
},
|
|
)
|
|
|
|
async def one_call() -> str:
|
|
try:
|
|
response = await router.acompletion(
|
|
model="gpt-5.6", messages=[{"role": "user", "content": "hi"}], stream=stream
|
|
)
|
|
except litellm.RateLimitError as e:
|
|
return f"rejected:{e.status_code}"
|
|
if stream:
|
|
async for _ in response:
|
|
pass
|
|
return "ok"
|
|
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
route: Final = respx_mock.post("https://max-parallel.local/v1/chat/completions").mock(side_effect=upstream)
|
|
outcomes: Final = await asyncio.wait_for(asyncio.gather(*(one_call() for _ in range(10))), timeout=10)
|
|
|
|
assert outcomes.count("ok") == 2
|
|
assert outcomes.count("rejected:429") == 8
|
|
assert route.call_count == 2
|
|
assert tracker.peak == 2
|
|
assert tracker.current == 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_max_parallel_requests_slot_held_until_stream_closed_then_released(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
tracker: Final = _InFlightTracker()
|
|
router: Final = _max_parallel_router(max_parallel_requests=1)
|
|
|
|
with respx.mock() as respx_mock:
|
|
respx_mock.post("https://max-parallel.local/v1/chat/completions").mock(
|
|
side_effect=lambda request: httpx.Response(
|
|
200, headers={"content-type": "text/event-stream"}, stream=_CountingSSEStream(tracker)
|
|
)
|
|
)
|
|
first: Final = await router.acompletion(
|
|
model="gpt-5.6", messages=[{"role": "user", "content": "hi"}], stream=True
|
|
)
|
|
await first.__anext__()
|
|
|
|
async def second_call() -> None:
|
|
second = await router.acompletion(
|
|
model="gpt-5.6", messages=[{"role": "user", "content": "hi"}], stream=True
|
|
)
|
|
async for _ in second:
|
|
pass
|
|
|
|
assert tracker.current == 1
|
|
with pytest.raises(litellm.RateLimitError) as while_streaming:
|
|
await second_call()
|
|
assert while_streaming.value.status_code == 429
|
|
await first.aclose()
|
|
await asyncio.wait_for(second_call(), timeout=2)
|
|
|
|
assert tracker.peak == 1
|
|
assert tracker.current == 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_max_parallel_requests_overflow_is_429_without_cooldown_or_provider_call(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://max-parallel.local/v1",
|
|
"max_parallel_requests": 1,
|
|
},
|
|
"model_info": {"id": "capped-deployment"},
|
|
},
|
|
{
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://max-parallel-sibling.local/v1",
|
|
},
|
|
"model_info": {"id": "sibling-deployment"},
|
|
},
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
async def upstream(request: httpx.Request) -> httpx.Response:
|
|
await asyncio.sleep(0.2)
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "c",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-5.6",
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "x"}, "finish_reason": "stop"}],
|
|
},
|
|
)
|
|
|
|
with respx.mock(assert_all_called=False) as respx_mock:
|
|
route: Final = respx_mock.post("https://max-parallel.local/v1/chat/completions").mock(side_effect=upstream)
|
|
sibling_route: Final = respx_mock.post("https://max-parallel-sibling.local/v1/chat/completions").mock(
|
|
side_effect=upstream
|
|
)
|
|
results: Final = await asyncio.wait_for(
|
|
asyncio.gather(
|
|
*(
|
|
router.acompletion(model="capped-deployment", messages=[{"role": "user", "content": "hi"}])
|
|
for _ in range(3)
|
|
),
|
|
return_exceptions=True,
|
|
),
|
|
timeout=10,
|
|
)
|
|
|
|
rejected: Final = [r for r in results if isinstance(r, BaseException)]
|
|
assert len(rejected) == 2 and len(results) == 3
|
|
assert all(isinstance(r, litellm.RateLimitError) and r.status_code == 429 for r in rejected)
|
|
assert all("capped-deployment" in r.message and "max_parallel_requests=1" in r.message for r in rejected)
|
|
assert route.call_count == 1
|
|
assert sibling_route.call_count == 0
|
|
assert await _async_get_cooldown_deployments(ctx=router._core_ctx, parent_otel_span=None) == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_embedding_path_rejects_past_max_parallel_requests_without_orphan_coroutines(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "embed",
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-3-small",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://max-parallel-embed.local/v1",
|
|
"max_parallel_requests": 1,
|
|
},
|
|
"model_info": {"id": "embed-capped-deployment"},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
async def upstream(request: httpx.Request) -> httpx.Response:
|
|
await asyncio.sleep(0.2)
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"object": "list",
|
|
"data": [{"object": "embedding", "index": 0, "embedding": [0.1, 0.2]}],
|
|
"model": "text-embedding-3-small",
|
|
"usage": {"prompt_tokens": 1, "total_tokens": 1},
|
|
},
|
|
)
|
|
|
|
# Reap earlier tests' garbage first so only this test's coroutines are recorded
|
|
gc.collect()
|
|
with respx.mock() as respx_mock, warnings.catch_warnings(record=True) as caught:
|
|
warnings.simplefilter("always")
|
|
route: Final = respx_mock.post("https://max-parallel-embed.local/v1/embeddings").mock(side_effect=upstream)
|
|
results: Final = await asyncio.wait_for(
|
|
asyncio.gather(
|
|
*(router.aembedding(model="embed", input=["hi"]) for _ in range(3)),
|
|
return_exceptions=True,
|
|
),
|
|
timeout=10,
|
|
)
|
|
gc.collect()
|
|
|
|
rejected: Final = [r for r in results if isinstance(r, BaseException)]
|
|
assert len(rejected) == 2 and len(results) == 3
|
|
assert all(isinstance(r, litellm.RateLimitError) and r.status_code == 429 for r in rejected)
|
|
assert all("embed-capped-deployment" in r.message for r in rejected)
|
|
assert route.call_count == 1
|
|
assert [str(w.message) for w in caught if "never awaited" in str(w.message)] == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_max_parallel_requests_overflow_takes_the_ordinary_429_fallback_path(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.6",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://max-parallel-primary.local/v1",
|
|
"max_parallel_requests": 1,
|
|
},
|
|
"model_info": {"id": "capped-primary-deployment"},
|
|
},
|
|
{
|
|
"model_name": "gpt-5.6-fallback",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-fake",
|
|
"api_base": "https://max-parallel-fallback.local/v1",
|
|
},
|
|
"model_info": {"id": "fallback-deployment"},
|
|
},
|
|
],
|
|
fallbacks=[{"gpt-5.6": ["gpt-5.6-fallback"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
async def upstream(request: httpx.Request) -> httpx.Response:
|
|
await asyncio.sleep(0.2)
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "c",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-5.6",
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "x"}, "finish_reason": "stop"}],
|
|
},
|
|
)
|
|
|
|
with respx.mock() as respx_mock:
|
|
primary: Final = respx_mock.post("https://max-parallel-primary.local/v1/chat/completions").mock(
|
|
side_effect=upstream
|
|
)
|
|
fallback: Final = respx_mock.post("https://max-parallel-fallback.local/v1/chat/completions").mock(
|
|
side_effect=upstream
|
|
)
|
|
results: Final = await asyncio.wait_for(
|
|
asyncio.gather(
|
|
*(router.acompletion(model="gpt-5.6", messages=[{"role": "user", "content": "hi"}]) for _ in range(3))
|
|
),
|
|
timeout=10,
|
|
)
|
|
|
|
assert len(results) == 3
|
|
assert primary.call_count == 1
|
|
assert fallback.call_count == 2
|
|
assert await _async_get_cooldown_deployments(ctx=router._core_ctx, parent_otel_span=None) == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_deployment_drop_params_string_true_is_honored(monkeypatch):
|
|
from litellm import Router
|
|
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5-nano",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5-nano",
|
|
"api_key": "sk-fake",
|
|
"temperature": 1,
|
|
"reasoning_effort": "minimal",
|
|
"drop_params": "true",
|
|
"mock_response": "Hello, world!",
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
deployment = router.get_deployment_by_model_group_name(model_group_name="gpt-5-nano")
|
|
assert deployment is not None
|
|
assert deployment.litellm_params.drop_params is True
|
|
|
|
response = await router.acompletion(
|
|
model="gpt-5-nano",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
temperature=0.1,
|
|
)
|
|
assert response.choices[0].message.content == "Hello, world!"
|
|
|
|
|
|
@pytest.mark.parametrize("value", ["ture", "enabled"])
|
|
def test_router_warns_when_a_deployment_drop_params_string_is_not_a_flag(value, caplog):
|
|
with caplog.at_level(logging.WARNING, logger="LiteLLM Router"):
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5-nano",
|
|
"litellm_params": {"model": "openai/gpt-5-nano", "api_key": "sk-fake", "drop_params": value},
|
|
}
|
|
]
|
|
)
|
|
|
|
deployment = router.get_deployment_by_model_group_name(model_group_name="gpt-5-nano")
|
|
assert deployment is not None
|
|
assert deployment.litellm_params.drop_params == value
|
|
assert f"model=gpt-5-nano drop_params={value!r} is not a flag value, treating it as unset" in caplog.text
|
|
|
|
|
|
@pytest.mark.parametrize("value", [True, "true", "off", None])
|
|
def test_router_stays_quiet_when_a_deployment_drop_params_is_a_flag(value, caplog):
|
|
with caplog.at_level(logging.WARNING, logger="LiteLLM Router"):
|
|
Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5-nano",
|
|
"litellm_params": {"model": "openai/gpt-5-nano", "api_key": "sk-fake", "drop_params": value},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert "is not a flag value" not in caplog.text
|
|
|
|
|
|
def test_get_candidate_model_ids_for_route_covers_model_name_and_pattern():
|
|
"""
|
|
get_candidate_model_ids_for_route resolves a route the way the router does, so a
|
|
pre-call check can tell a genuine cross-group route from same-group unavailability.
|
|
A concrete model group returns its member ids; a wildcard/pattern deployment is
|
|
included for a concrete model it matches, which the bare model_name index misses.
|
|
The unprefixed-name case must resolve through get_deployments_by_pattern (which retries
|
|
the provider-qualified form), not a bare pattern_router.route that only sees the literal
|
|
name. Regression guard for the LIT-7195 tier-change discriminator's team/pattern gaps.
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "grp",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-a", "api_base": "https://x.invalid"},
|
|
"model_info": {"id": "dep-a"},
|
|
},
|
|
{
|
|
"model_name": "grp",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-b", "api_base": "https://x.invalid"},
|
|
"model_info": {"id": "dep-b"},
|
|
},
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {"model": "openai/*", "api_key": "sk-c", "api_base": "https://x.invalid"},
|
|
"model_info": {"id": "dep-wild"},
|
|
},
|
|
]
|
|
)
|
|
|
|
assert router.get_candidate_model_ids_for_route(model="grp") == frozenset({"dep-a", "dep-b"})
|
|
assert "dep-wild" in router.get_candidate_model_ids_for_route(model="openai/gpt-4o-some-new-model")
|
|
# unprefixed name whose provider resolves to openai: only get_deployments_by_pattern's
|
|
# provider-qualified retry matches "openai/*"; a bare route() on the literal name misses it
|
|
assert "dep-wild" in router.get_candidate_model_ids_for_route(model="gpt-5")
|
|
|
|
|
|
def test_cached_model_info_lookups_match_uncached_and_reset_on_model_list_change():
|
|
def deployment(max_output_tokens: int) -> Deployment:
|
|
return Deployment(
|
|
model_name="grp",
|
|
litellm_params=LiteLLM_Params(model="openai/gpt-4o", api_key="sk-a"),
|
|
model_info=ModelInfo(id="dep-a", max_output_tokens=max_output_tokens),
|
|
)
|
|
|
|
router = Router(model_list=[deployment(100).model_dump()])
|
|
|
|
assert router.cached_model_group_info("grp") == router.get_model_group_info("grp")
|
|
first = router.cached_deployment_model_info("dep-a", "openai/gpt-4o")
|
|
assert first == router.get_deployment_model_info(model_id="dep-a", model_name="openai/gpt-4o")
|
|
assert router.cached_deployment_model_info("dep-a", "openai/gpt-4o") is first
|
|
|
|
router.upsert_deployment(deployment(200))
|
|
|
|
assert router.cached_deployment_model_info("dep-a", "openai/gpt-4o")["max_output_tokens"] == 200
|
|
assert router.cached_model_group_info("grp").max_output_tokens == 200
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_model_name_colliding_with_a_deployment_id_still_load_balances_the_group():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5-nano",
|
|
"litellm_params": {"model": "openai/gpt-5-nano", "api_key": "k", "weight": 0, "mock_response": "A"},
|
|
"model_info": {"id": "gpt-5-nano"},
|
|
},
|
|
{
|
|
"model_name": "gpt-5-nano",
|
|
"litellm_params": {"model": "openai/gpt-5-mini", "api_key": "k", "weight": 1, "mock_response": "B"},
|
|
"model_info": {"id": "gpt-5-mini-dep"},
|
|
},
|
|
],
|
|
routing_strategy="simple-shuffle",
|
|
)
|
|
|
|
by_group = await router.acompletion(model="gpt-5-nano", messages=[{"role": "user", "content": "hi"}])
|
|
by_id = await router.acompletion(model="gpt-5-mini-dep", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert by_group._hidden_params["model_id"] == "gpt-5-mini-dep"
|
|
assert by_group.choices[0].message.content == "B"
|
|
assert by_id._hidden_params["model_id"] == "gpt-5-mini-dep"
|
|
|
|
|
|
def test_sync_completion_routes_a_model_name_colliding_with_a_deployment_id_by_group():
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5-nano",
|
|
"litellm_params": {"model": "openai/gpt-5-nano", "api_key": "k", "weight": 0, "mock_response": "A"},
|
|
"model_info": {"id": "gpt-5-nano"},
|
|
},
|
|
{
|
|
"model_name": "gpt-5-nano",
|
|
"litellm_params": {"model": "openai/gpt-5-mini", "api_key": "k", "weight": 1, "mock_response": "B"},
|
|
"model_info": {"id": "gpt-5-mini-dep"},
|
|
},
|
|
],
|
|
routing_strategy="simple-shuffle",
|
|
)
|
|
|
|
by_group = router.completion(model="gpt-5-nano", messages=[{"role": "user", "content": "hi"}])
|
|
assert by_group._hidden_params["model_id"] == "gpt-5-mini-dep"
|
|
assert by_group.choices[0].message.content == "B"
|
|
|
|
by_id = router.completion(model="gpt-5-mini-dep", messages=[{"role": "user", "content": "hi"}])
|
|
assert by_id._hidden_params["model_id"] == "gpt-5-mini-dep"
|
|
|
|
|
|
class TestMemberAutoRouterInference:
|
|
@pytest.fixture(autouse=True)
|
|
def runtime(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
from litellm.proxy import proxy_server
|
|
from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache
|
|
|
|
self.cache = UserApiKeyCache()
|
|
self.team = LiteLLM_TeamTable(
|
|
team_id="router-team",
|
|
models=["member-router", "permitted-model"],
|
|
members_with_roles=[Member(user_id="router-member", role="user")],
|
|
)
|
|
self.actor = UserAPIKeyAuth(
|
|
user_id="router-member",
|
|
team_id="router-team",
|
|
user_role=LitellmUserRoles.INTERNAL_USER,
|
|
models=["member-router", "permitted-model"],
|
|
api_key="test-key-hash",
|
|
config={"timeout": 60},
|
|
)
|
|
self.database = SimpleNamespace(
|
|
db=SimpleNamespace(
|
|
litellm_teamtable=SimpleNamespace(find_unique=AsyncMock(return_value=self.team)),
|
|
litellm_teammembership=SimpleNamespace(find_unique=AsyncMock(return_value=None)),
|
|
litellm_accessgrouptable=SimpleNamespace(find_unique=AsyncMock()),
|
|
)
|
|
)
|
|
monkeypatch.setattr(proxy_server, "user_api_key_cache", self.cache)
|
|
monkeypatch.setattr(proxy_server, "prisma_client", self.database)
|
|
|
|
@staticmethod
|
|
def _marker(*, member: bool = True, classifier: bool = False) -> dict[str, object]:
|
|
target: Final = "permitted-model" if member else "restricted-model"
|
|
return {
|
|
"model_name": "model_name_router-team_member-router",
|
|
"litellm_params": {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_default_model": target,
|
|
"complexity_router_config": {
|
|
"tiers": dict.fromkeys(("SIMPLE", "MEDIUM", "COMPLEX", "REASONING"), target),
|
|
"adaptive": False,
|
|
**({"classifier_type": "llm", "classifier_llm_config": {"model": target}} if classifier else {}),
|
|
},
|
|
"tags": ["member" if member else "admin"],
|
|
"timeout": 13.0 if member else 29.0,
|
|
},
|
|
"model_info": {
|
|
"team_id": "router-team",
|
|
"team_public_model_name": "member-router",
|
|
"member_auto_router": member,
|
|
},
|
|
}
|
|
|
|
@classmethod
|
|
def _router(cls, *markers: dict[str, object]) -> Router:
|
|
return Router(
|
|
model_list=[
|
|
*(markers or (cls._marker(),)),
|
|
{
|
|
"model_name": "permitted-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "test-key",
|
|
"api_base": "https://api.openai.com/v1",
|
|
},
|
|
},
|
|
{"model_name": "restricted-model", "litellm_params": {"model": "openai/gpt-4o", "api_key": "test-key"}},
|
|
]
|
|
)
|
|
|
|
def _request(
|
|
self,
|
|
*,
|
|
actor: UserAPIKeyAuth | None = None,
|
|
metadata_name: str = "metadata",
|
|
tag: str = "member",
|
|
) -> dict[str, object]:
|
|
from litellm.proxy.litellm_pre_call_utils import LiteLLMProxyRequestSetup
|
|
|
|
return LiteLLMProxyRequestSetup.add_user_api_key_auth_to_request_metadata(
|
|
data={
|
|
metadata_name: {"tags": [tag]},
|
|
**(
|
|
{"metadata": {"user_api_key_auth": {"user_role": "proxy_admin"}}}
|
|
if metadata_name == "litellm_metadata"
|
|
else {}
|
|
),
|
|
},
|
|
user_api_key_dict=actor or self.actor,
|
|
_metadata_variable_name=metadata_name,
|
|
)
|
|
|
|
async def _route(
|
|
self,
|
|
router: Router,
|
|
request: dict[str, object] | None = None,
|
|
model: str = "member-router",
|
|
) -> PreRoutingHookResponse:
|
|
response: Final = await router.async_pre_routing_hook(
|
|
model=model,
|
|
request_kwargs=request if request is not None else self._request(),
|
|
messages=[{"role": "user", "content": "Hello"}],
|
|
)
|
|
assert response is not None
|
|
return response
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("metadata_name", ("metadata", "litellm_metadata"))
|
|
async def test_cached_roster_revocation_blocks_classifier_and_session_rebinding(
|
|
self,
|
|
metadata_name: str,
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
from litellm.proxy.auth.auth_checks import delete_cache_team_object
|
|
|
|
monkeypatch.setenv("DISABLE_AIOHTTP_TRANSPORT", "True")
|
|
router: Final = self._router(self._marker(classifier=True))
|
|
classify: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").respond(
|
|
200,
|
|
json={
|
|
"id": "classifier",
|
|
"object": "chat.completion",
|
|
"created": 0,
|
|
"model": "gpt-4o-mini",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"message": {"content": '{"tier":"SIMPLE"}', "role": "assistant"},
|
|
"finish_reason": "stop",
|
|
}
|
|
],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
},
|
|
)
|
|
request: Final = {
|
|
**self._request(metadata_name=metadata_name),
|
|
"proxy_server_request": {
|
|
"headers": {
|
|
"x-claude-code-session-id": "member-router-session",
|
|
"x-app": "cli",
|
|
}
|
|
},
|
|
}
|
|
first: Final = await self._route(router, request)
|
|
assert first.model == "permitted-model" and first.routing_decision is not None
|
|
assert first.routing_decision["cause"] == "llm_classifier"
|
|
assert (await self._route(router, request)).model == "permitted-model"
|
|
assert self.database.db.litellm_teamtable.find_unique.await_count == 1
|
|
assert self.database.db.litellm_teammembership.find_unique.await_count == 1
|
|
self.database.db.litellm_teamtable.find_unique.return_value = self.team.model_copy(
|
|
update={"members_with_roles": []}
|
|
)
|
|
await delete_cache_team_object(
|
|
team_id=self.team.team_id,
|
|
team_alias=None,
|
|
user_api_key_cache=self.cache,
|
|
proxy_logging_obj=None,
|
|
)
|
|
with pytest.raises(HTTPException, match="no longer a member"):
|
|
await self._route(router, request)
|
|
rebound: Final = {
|
|
**request,
|
|
"proxy_server_request": {
|
|
"headers": {
|
|
"x-claude-code-session-id": "member-router-session",
|
|
"x-app": "cli",
|
|
"x-claude-code-agent-id": "subagent",
|
|
}
|
|
},
|
|
}
|
|
with pytest.raises(HTTPException, match="no longer a member"):
|
|
await self._route(router, rebound, model="restricted-model")
|
|
assert classify.call_count == 2
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("state", ("forged", "blocked", "deleted", "unavailable", "empty-user"))
|
|
async def test_member_router_fails_closed(self, state: str, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
from litellm.proxy import proxy_server
|
|
|
|
request: Final = (
|
|
{
|
|
"metadata": {
|
|
"user_api_key_team_id": "router-team",
|
|
"user_api_key_auth": {
|
|
"team_id": "router-team",
|
|
"user_role": "proxy_admin",
|
|
},
|
|
}
|
|
}
|
|
if state == "forged"
|
|
else self._request(
|
|
actor=self.actor.model_copy(
|
|
update={"user_id": ""} if state == "empty-user" else {},
|
|
)
|
|
)
|
|
)
|
|
self.database.db.litellm_teamtable.find_unique.return_value = (
|
|
None if state == "deleted" else self.team.model_copy(update={"blocked": state == "blocked"})
|
|
)
|
|
if state == "unavailable":
|
|
monkeypatch.setattr(proxy_server, "prisma_client", None)
|
|
with pytest.raises(HTTPException) as error:
|
|
await self._route(self._router(), request)
|
|
assert error.value.status_code == (503 if state == "unavailable" else 403)
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"user_id,role", [(None, LitellmUserRoles.INTERNAL_USER), ("admin", LitellmUserRoles.PROXY_ADMIN)]
|
|
)
|
|
async def test_service_key_and_admin_preserve_runtime_access(
|
|
self, user_id: str | None, role: LitellmUserRoles
|
|
) -> None:
|
|
assert (
|
|
await self._route(
|
|
self._router(),
|
|
self._request(
|
|
actor=self.actor.model_copy(update={"user_id": user_id, "user_role": role}),
|
|
),
|
|
)
|
|
).model == "permitted-model"
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("ceiling", ("team", "key", "member", "organization", "project"))
|
|
async def test_runtime_dependency_ceilings_use_cached_auth_state(self, ceiling: str) -> None:
|
|
from litellm.models.budget import LiteLLM_BudgetTable
|
|
from litellm.models.organization import LiteLLM_OrganizationTable
|
|
from litellm.models.team_membership import LiteLLM_TeamMembership
|
|
from litellm.proxy._types import LiteLLM_ProjectTableCachedObj
|
|
from litellm.proxy.common_utils.user_api_key_cache import team_membership_reservation_cache_key
|
|
|
|
self.database.db.litellm_teamtable.find_unique.return_value = self.team.model_copy(
|
|
update={
|
|
"models": ["member-router"] if ceiling == "team" else self.team.models,
|
|
"organization_id": "router-org" if ceiling == "organization" else None,
|
|
}
|
|
)
|
|
if ceiling == "member":
|
|
await self.cache.async_set_cache(
|
|
key=team_membership_reservation_cache_key(user_id="router-member", team_id="router-team"),
|
|
value=LiteLLM_TeamMembership(
|
|
user_id="router-member",
|
|
team_id="router-team",
|
|
litellm_budget_table=LiteLLM_BudgetTable(allowed_models=["restricted-model"]),
|
|
),
|
|
model_type=LiteLLM_TeamMembership,
|
|
)
|
|
elif ceiling == "organization":
|
|
await self.cache.async_set_cache(
|
|
key="org_id:router-org",
|
|
value=LiteLLM_OrganizationTable(
|
|
organization_id="router-org",
|
|
budget_id="org-budget",
|
|
created_by="admin",
|
|
updated_by="admin",
|
|
models=["restricted-model"],
|
|
),
|
|
model_type=LiteLLM_OrganizationTable,
|
|
)
|
|
elif ceiling == "project":
|
|
await self.cache.async_set_cache(
|
|
key="project_id:router-project",
|
|
value=LiteLLM_ProjectTableCachedObj(
|
|
project_id="router-project",
|
|
team_id="router-team",
|
|
models=["restricted-model"],
|
|
),
|
|
model_type=LiteLLM_ProjectTableCachedObj,
|
|
)
|
|
with pytest.raises(ProxyException, match="is not available for this API key"):
|
|
await self._route(
|
|
self._router(),
|
|
self._request(
|
|
actor=self.actor.model_copy(
|
|
update={
|
|
"models": ["member-router"] if ceiling == "key" else self.actor.models,
|
|
"project_id": "router-project" if ceiling == "project" else None,
|
|
}
|
|
)
|
|
),
|
|
)
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("group_owner", ("team", "key"))
|
|
async def test_access_group_grants_are_cached_and_revoked(self, group_owner: str) -> None:
|
|
from litellm.proxy.common_utils.auth_cache_invalidation_pubsub import evict_and_broadcast
|
|
|
|
group: Final = LiteLLM_AccessGroupTable(
|
|
access_group_id="router-group",
|
|
access_group_name="Router targets",
|
|
access_model_names=["permitted-model"],
|
|
)
|
|
self.database.db.litellm_accessgrouptable.find_unique.return_value = group
|
|
self.database.db.litellm_teamtable.find_unique.return_value = self.team.model_copy(
|
|
update={
|
|
"models": ["member-router"] if group_owner == "team" else self.team.models,
|
|
"access_group_ids": ["router-group"] if group_owner == "team" else [],
|
|
}
|
|
)
|
|
request: Final = self._request(
|
|
actor=self.actor.model_copy(
|
|
update={
|
|
"models": ["member-router"] if group_owner == "key" else self.actor.models,
|
|
"access_group_ids": ["router-group"] if group_owner == "key" else [],
|
|
}
|
|
)
|
|
)
|
|
router: Final = self._router()
|
|
assert (await self._route(router, request)).model == "permitted-model"
|
|
assert (await self._route(router, request)).model == "permitted-model"
|
|
assert self.database.db.litellm_accessgrouptable.find_unique.await_count == 1
|
|
self.database.db.litellm_accessgrouptable.find_unique.return_value = group.model_copy(
|
|
update={"access_model_names": []}
|
|
)
|
|
await evict_and_broadcast(cache_keys=("access_group_id:router-group",), user_api_key_cache=self.cache)
|
|
with pytest.raises(ProxyException, match="is not available for this API key"):
|
|
await self._route(router, request)
|
|
assert self.database.db.litellm_accessgrouptable.find_unique.await_count == 2
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_sdk_router_does_not_import_proxy_dependencies(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
router: Final = self._router(self._marker(member=False))
|
|
monkeypatch.setitem(sys.modules, "fastapi", None)
|
|
monkeypatch.delitem(sys.modules, "litellm.proxy.auth.auto_router_checks", raising=False)
|
|
assert (
|
|
await self._route(router, {"metadata": {"user_api_key_team_id": "router-team"}})
|
|
).model == "restricted-model"
|
|
|
|
|
|
def test_access_windows_invalid_timezone_fails_router_construction():
|
|
with pytest.raises(ValueError, match=r"gpt-4o-ptu.*access_windows"):
|
|
Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4o-ptu",
|
|
"litellm_params": {"model": "gpt-4o", "mock_response": "x"},
|
|
"model_info": {
|
|
"access_windows": [
|
|
{
|
|
"start": "22:00",
|
|
"end": "06:00",
|
|
"timezone": "Mars/Olympus",
|
|
"team_ids": ["team-a"],
|
|
}
|
|
]
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
|
|
def _scheduled_router(timeout: float) -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "sched-model",
|
|
"litellm_params": {"model": "openai/sched-model", "api_key": "sk-fake", "mock_response": "hi"},
|
|
"model_info": {"id": "sched-deployment"},
|
|
}
|
|
],
|
|
timeout=timeout,
|
|
)
|
|
|
|
|
|
async def _send_scheduled_chat(router: Router, priority: int) -> object:
|
|
return await router.acompletion(
|
|
model="sched-model", messages=[{"role": "user", "content": "hi"}], priority=priority
|
|
)
|
|
|
|
|
|
async def _send_scheduled_text(router: Router, priority: int) -> object:
|
|
return await router.atext_completion(model="sched-model", prompt="hi", priority=priority)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"send", [_send_scheduled_chat, _send_scheduled_text], ids=["schedule_acompletion", "schedule_factory"]
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_admitted_prioritized_request_does_not_block_later_request_during_cooldown(
|
|
send: Callable[[Router, int], Awaitable[object]],
|
|
):
|
|
from litellm.types.router import RouterRateLimitError
|
|
|
|
router: Final = _scheduled_router(timeout=1)
|
|
await send(router, 1)
|
|
_cool_down(router, "sched-deployment")
|
|
|
|
with pytest.raises(RouterRateLimitError, match="cooldown"):
|
|
await send(router, 2)
|
|
|
|
|
|
@pytest.mark.parametrize("stop_waiting", ["cancel", "timeout"])
|
|
@pytest.mark.asyncio
|
|
async def test_prioritized_request_leaves_queue_when_it_stops_waiting(stop_waiting: Literal["cancel", "timeout"]):
|
|
router: Final = _scheduled_router(timeout=0.5)
|
|
_cool_down(router, "sched-deployment")
|
|
await router.scheduler.add_request(FlowItem(priority=0, request_id="head-of-queue", model_name="sched-model"))
|
|
waiting: Final = asyncio.create_task(_send_scheduled_chat(router, 5))
|
|
await asyncio.sleep(0.05)
|
|
assert len(await router.scheduler.get_queue("sched-model")) == 2
|
|
|
|
if stop_waiting == "cancel":
|
|
waiting.cancel()
|
|
with pytest.raises(asyncio.CancelledError if stop_waiting == "cancel" else litellm.Timeout):
|
|
await waiting
|
|
|
|
assert await router.scheduler.get_queue("sched-model") == [(0, "head-of-queue")]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_ptu_share_holders_prioritized_request_is_admitted_while_its_shared_deployment_is_healthy(monkeypatch):
|
|
"""The scheduler has to look the group's health up as the calling team; looked up with no team a
|
|
shared PTU deployment looks unhealthy, so the share holder waits behind the queue until it times out."""
|
|
monkeypatch.setenv("LITELLM_ENABLE_PTU_COST_ATTRIBUTION", "true")
|
|
router: Final = Router(
|
|
model_list=[_shared_ptu_deployment("sched-ptu", "shared-ptu", mock_response="hi")],
|
|
timeout=0.5,
|
|
)
|
|
await router.scheduler.add_request(FlowItem(priority=0, request_id="head-of-queue", model_name="sched-ptu"))
|
|
|
|
response: Final = await router.acompletion(
|
|
model="sched-ptu",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
priority=5,
|
|
metadata={"user_api_key_team_id": "team-a"},
|
|
)
|
|
|
|
assert response.choices[0].message.content == "hi"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bare_model_group_served_by_wildcard_deployment_uses_provider_prefixed_fallback_key() -> None:
|
|
"""Claude Code sends the bare "claude-sonnet-4-6" to /v1/messages; routing serves it through the
|
|
"anthropic/*" wildcard, so a fallback keyed the way that wildcard is written ("anthropic/claude-sonnet-4-6",
|
|
which is what the Admin UI offers) must catch the failure instead of surfacing the provider error."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "anthropic/*",
|
|
"litellm_params": {
|
|
"model": "anthropic/*",
|
|
"api_key": "sk-fake",
|
|
"mock_response": "litellm.InternalServerError",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "openai/gpt-5.5-pro",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.5-pro",
|
|
"api_key": "sk-fake",
|
|
"mock_response": "served by the fallback",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"anthropic/claude-sonnet-4-6": ["openai/gpt-5.5-pro"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
result = await router.aanthropic_messages(
|
|
model="claude-sonnet-4-6",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
max_tokens=64,
|
|
)
|
|
|
|
assert result["content"][0]["text"] == "served by the fallback"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bare_model_group_served_by_wildcard_deployment_uses_provider_prefixed_context_window_fallback_key() -> (
|
|
None
|
|
):
|
|
"""The context-window chain is keyed the same way the ordinary chain is, so a key spelled like the
|
|
wildcard deployment ("anthropic/claude-sonnet-4-6") must catch the bare group's context-window error too."""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "anthropic/*",
|
|
"litellm_params": {
|
|
"model": "anthropic/*",
|
|
"api_key": "sk-fake",
|
|
"mock_response": "litellm.ContextWindowExceededError",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "openai/gpt-5.5-pro",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.5-pro",
|
|
"api_key": "sk-fake",
|
|
"mock_response": "served by the context window fallback",
|
|
},
|
|
},
|
|
],
|
|
context_window_fallbacks=[{"anthropic/claude-sonnet-4-6": ["openai/gpt-5.5-pro"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
result = await router.aanthropic_messages(
|
|
model="claude-sonnet-4-6",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
max_tokens=64,
|
|
)
|
|
|
|
assert result["content"][0]["text"] == "served by the context window fallback"
|
|
|
|
|
|
def _shared_ptu_model_list() -> list:
|
|
return [
|
|
{
|
|
"model_name": "gpt-4.1-ptu",
|
|
"litellm_params": {"model": "gpt-4.1", "mock_response": "shared"},
|
|
"model_info": {
|
|
"id": "shared-deployment",
|
|
"base_model": "azure/gpt-4.1",
|
|
"ptu_count": 50,
|
|
"cost_per_ptu_per_hour": 1.0,
|
|
"ptu_effective_from": "2026-01-01T00:00:00Z",
|
|
"ptu_shares": {"team-a": 30, "team-b": 20},
|
|
},
|
|
},
|
|
{
|
|
"model_name": "gpt-4.1-ptu",
|
|
"litellm_params": {"model": "gpt-4.1", "mock_response": "open"},
|
|
"model_info": {"id": "open-deployment"},
|
|
},
|
|
]
|
|
|
|
|
|
def test_team_ptu_ceiling_follows_shares_moved_on_the_live_model_list():
|
|
router: Final = Router(model_list=_shared_ptu_model_list()[:1])
|
|
team_a_before: Final = router.team_ptu_ceiling("team-a", "gpt-4.1-ptu")
|
|
assert router.team_ptu_ceiling("team-c", "gpt-4.1-ptu") is None
|
|
|
|
moved: Final = _shared_ptu_model_list()[0]
|
|
moved["model_info"]["ptu_shares"] = {"team-a": 10, "team-c": 40}
|
|
router.upsert_deployment(Deployment(**moved))
|
|
|
|
team_a_after: Final = router.team_ptu_ceiling("team-a", "gpt-4.1-ptu")
|
|
team_c_after: Final = router.team_ptu_ceiling("team-c", "gpt-4.1-ptu")
|
|
assert team_a_before is not None and team_a_after is not None and team_c_after is not None
|
|
assert team_a_before.tpm_limit == 3 * team_a_after.tpm_limit
|
|
assert team_c_after.tpm_limit == 4 * team_a_after.tpm_limit
|
|
|
|
router.delete_deployment(id="shared-deployment")
|
|
assert router.team_ptu_ceiling("team-a", "gpt-4.1-ptu") is None
|
|
|
|
|
|
def test_a_shared_ptu_deployment_whose_shares_do_not_add_up_is_refused_at_registration(monkeypatch):
|
|
monkeypatch.setenv("LITELLM_ENABLE_PTU_COST_ATTRIBUTION", "True")
|
|
model_list = _shared_ptu_model_list()[:1]
|
|
model_list[0]["model_info"]["ptu_shares"] = {"team-a": 30}
|
|
with pytest.raises(ValueError, match=r"gpt-4\.1-ptu.*30 of 50 allocated"):
|
|
Router(model_list=model_list)
|
|
|
|
|
|
def test_a_shared_ptu_deployment_with_a_fractional_count_is_refused_at_registration(monkeypatch):
|
|
monkeypatch.setenv("LITELLM_ENABLE_PTU_COST_ATTRIBUTION", "True")
|
|
model_list = _shared_ptu_model_list()[:1]
|
|
model_list[0]["model_info"]["ptu_count"] = 50.5
|
|
with pytest.raises(ValueError, match="ptu_count"):
|
|
Router(model_list=model_list)
|
|
|
|
|
|
@pytest.mark.parametrize(("flag", "warned"), [("True", True), ("", False)])
|
|
def test_an_unsized_shared_ptu_deployment_is_warned_about_at_registration_only_with_the_feature_on(
|
|
monkeypatch, caplog, flag, warned
|
|
):
|
|
monkeypatch.setenv("LITELLM_ENABLE_PTU_COST_ATTRIBUTION", flag)
|
|
model_list = _shared_ptu_model_list()[:1]
|
|
model_list[0]["litellm_params"]["model"] = "azure/my-ptu-deployment"
|
|
del model_list[0]["model_info"]["base_model"]
|
|
with caplog.at_level(logging.WARNING, logger="LiteLLM Router"):
|
|
Router(model_list=model_list)
|
|
assert ("'gpt-4.1-ptu'" in caplog.text and "base_model" in caplog.text) is warned
|
|
|
|
|
|
def test_a_config_entry_declaring_shares_without_terms_is_refused_even_while_the_feature_is_off(monkeypatch):
|
|
"""The split is enforced with the flag off, so its shape is checked with the flag off too: a
|
|
malformed one is refused at boot instead of being honoured or ignored without a word."""
|
|
monkeypatch.delenv("LITELLM_ENABLE_PTU_COST_ATTRIBUTION", raising=False)
|
|
model_list = _shared_ptu_model_list()[:1]
|
|
model_list[0]["model_info"] = {"id": "shared-deployment", "ptu_shares": {"team-a": 30, "team-b": 20}}
|
|
with pytest.raises(ValueError, match=r"ptu_count and cost_per_ptu_per_hour are required when ptu_shares is set"):
|
|
Router(model_list=model_list)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"verdict",
|
|
[
|
|
GuardrailRaisedException(guardrail_name="chunk-scanner", message="blocked"),
|
|
HTTPException(status_code=403, detail={"error": "blocked", "guardrail_name": "chunk-scanner"}),
|
|
ModifyResponseException(message="blocked", model="primary", request_data={}, guardrail_name="chunk-scanner"),
|
|
],
|
|
)
|
|
async def test_a_guardrail_verdict_is_neither_retried_nor_fallen_back(verdict: Exception) -> None:
|
|
async def fake_acompletion(**kwargs):
|
|
if kwargs["metadata"]["model_group"] == "primary":
|
|
raise verdict
|
|
return litellm.ModelResponse(choices=[{"message": {"role": "assistant", "content": "ok"}}])
|
|
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "primary", "litellm_params": {"model": "openai/primary-model", "api_key": "fake-key"}},
|
|
{"model_name": "primary", "litellm_params": {"model": "openai/primary-sibling", "api_key": "fake-key"}},
|
|
{"model_name": "fb1", "litellm_params": {"model": "openai/fb1-model", "api_key": "fake-key"}},
|
|
],
|
|
fallbacks=[{"primary": ["fb1"]}],
|
|
num_retries=2,
|
|
)
|
|
|
|
with patch("litellm.acompletion", side_effect=fake_acompletion) as mock_acompletion:
|
|
with pytest.raises(type(verdict)):
|
|
await router.acompletion(model="primary", messages=[{"role": "user", "content": "hi"}])
|
|
|
|
assert [c.kwargs["metadata"]["model_group"] for c in mock_acompletion.call_args_list] == ["primary"]
|
|
|
|
|
|
def test_get_deployment_credentials_with_provider_preserves_anthropic_wif_params():
|
|
"""
|
|
Test that get_deployment_credentials_with_provider preserves a litellm_params-configured
|
|
Anthropic workload identity federation setup (both the legacy token_file fields and the
|
|
Phase 1 internal_issuer/keycloak identity-source fields) so files/batches/passthrough
|
|
deployments using WIF do not silently fall back to a missing credential.
|
|
"""
|
|
wif_params = {
|
|
"anthropic_federation_rule_id": "fdrl_deployment",
|
|
"anthropic_organization_id": "org-deployment",
|
|
"anthropic_identity_source": "keycloak",
|
|
"anthropic_keycloak_token_url": "https://keycloak.internal.example/realms/r/protocol/openid-connect/token",
|
|
"anthropic_keycloak_client_id": "litellm",
|
|
"anthropic_keycloak_client_secret_ref": "oidc/env/KEYCLOAK_CLIENT_SECRET",
|
|
}
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "anthropic-wif-model",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-sonnet-4-5",
|
|
**wif_params,
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="anthropic-wif-model")
|
|
|
|
assert credentials is not None
|
|
for key, value in wif_params.items():
|
|
assert credentials.get(key) == value, key
|
|
|
|
|
|
def test_router_keeps_wif_secret_pointers_unresolved(monkeypatch):
|
|
monkeypatch.setenv("WIF_TEST_KC_SECRET", "kc-secret")
|
|
monkeypatch.setenv("WIF_TEST_FDRL", "fdrl_from_env")
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude-wif",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-haiku-4-5",
|
|
"anthropic_federation_rule_id": "os.environ/WIF_TEST_FDRL",
|
|
"anthropic_identity_source": "keycloak",
|
|
"anthropic_keycloak_token_url": "https://keycloak.example/token",
|
|
"anthropic_keycloak_client_id": "litellm",
|
|
"anthropic_keycloak_client_secret_ref": "os.environ/WIF_TEST_KC_SECRET",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
litellm_params = router.get_model_list()[0]["litellm_params"]
|
|
|
|
assert litellm_params["anthropic_federation_rule_id"] == "fdrl_from_env"
|
|
assert litellm_params["anthropic_keycloak_client_secret_ref"] == "os.environ/WIF_TEST_KC_SECRET"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_failed_call_still_counts_one_rpm():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-group",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "fake", "mock_response": "hi"},
|
|
"model_info": {"id": "dep-1"},
|
|
}
|
|
]
|
|
)
|
|
await router.async_deployment_callback_on_failure(
|
|
kwargs={
|
|
"call_type": "acompletion",
|
|
"litellm_params": {
|
|
"metadata": {"deployment": "openai/gpt-4o", "model_group": "gpt-group"},
|
|
"model_info": {"id": "dep-1"},
|
|
},
|
|
},
|
|
completion_response=None,
|
|
start_time=None,
|
|
end_time=None,
|
|
)
|
|
|
|
assert await _usage_by_kind(router, "gpt-group") == {"rpm": 1}
|
|
|
|
|
|
async def _usage_by_kind(router: Router, group: str) -> dict[str, int]:
|
|
tpm, rpm = await router.health().usage(group)
|
|
return {kind: value for kind, value in (("tpm", tpm), ("rpm", rpm)) if value is not None}
|
|
|
|
|
|
async def _usage_settles(router: Router, group: str, kinds: int) -> dict[str, int]:
|
|
async with asyncio.timeout(5):
|
|
while len(await _usage_by_kind(router, group)) < kinds:
|
|
await asyncio.sleep(0.01)
|
|
return await _usage_by_kind(router, group)
|
|
|
|
|
|
@pytest.fixture
|
|
def v2_span_exporter(monkeypatch):
|
|
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
|
|
|
|
from litellm.integrations.otel import OpenTelemetryV2Config
|
|
from litellm.integrations.otel.logger import OpenTelemetryV2
|
|
from litellm.integrations.otel.plumbing import providers
|
|
from litellm.proxy import proxy_server
|
|
|
|
config = OpenTelemetryV2Config(exporter="in_memory")
|
|
exporter = InMemorySpanExporter()
|
|
logger = OpenTelemetryV2(config=config, tracer_provider=providers.build_tracer_provider(config, exporter=exporter))
|
|
monkeypatch.setattr(proxy_server, "open_telemetry_logger", logger)
|
|
return exporter
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_deployment_selection_runs_inside_a_route_phase_named_after_the_model_group(v2_span_exporter):
|
|
"""Picking a deployment opens ``route {model_group}`` (the requested group, not the deployment
|
|
it picks) under the server span, and the cooldown reads it issues run inside it, so their Redis
|
|
spans nest there instead of lying flat under the request."""
|
|
from opentelemetry.sdk.trace import TracerProvider
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-group",
|
|
"litellm_params": {"model": "openai/gpt-5.4-mini", "api_key": "fake", "mock_response": "a"},
|
|
"model_info": {"id": "dep-a"},
|
|
},
|
|
{
|
|
"model_name": "gpt-group",
|
|
"litellm_params": {"model": "openai/gpt-5.4", "api_key": "fake", "mock_response": "b"},
|
|
"model_info": {"id": "dep-b"},
|
|
},
|
|
]
|
|
)
|
|
with TracerProvider().get_tracer("test").start_as_current_span("POST /v1/chat/completions") as server_span:
|
|
deployment = await router.async_get_available_deployment(model="gpt-group", request_kwargs={})
|
|
|
|
assert deployment["model_info"]["id"] in {"dep-a", "dep-b"}
|
|
(route_span,) = v2_span_exporter.get_finished_spans()
|
|
assert route_span.name == "route gpt-group"
|
|
assert route_span.parent is not None and route_span.parent.span_id == server_span.get_span_context().span_id
|
|
assert route_span.end_time is not None
|
|
|
|
|
|
class TestAutoRouterTraceProvenance:
|
|
@pytest.fixture
|
|
def _tracing(self, monkeypatch: pytest.MonkeyPatch) -> "tuple[OpenTelemetryV2, InMemorySpanExporter]":
|
|
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
|
|
from litellm.integrations.otel import OpenTelemetryV2Config
|
|
from litellm.integrations.otel.logger import OpenTelemetryV2
|
|
from litellm.integrations.otel.plumbing import providers
|
|
from litellm.proxy import proxy_server
|
|
|
|
config: Final = OpenTelemetryV2Config(exporter="in_memory")
|
|
exporter: Final = InMemorySpanExporter()
|
|
logger: Final = OpenTelemetryV2(
|
|
config=config, tracer_provider=providers.build_tracer_provider(config, exporter=exporter)
|
|
)
|
|
monkeypatch.setattr(proxy_server, "open_telemetry_logger", logger)
|
|
return logger, exporter
|
|
|
|
@staticmethod
|
|
def _marker(tag: str, target: str = "answer") -> dict[str, object]:
|
|
return {
|
|
"model_name": "traced-router",
|
|
"model_info": {"id": f"definition-{tag}", "updated_at": "2026-10-01T12:00:00+00:00"},
|
|
"litellm_params": {
|
|
"model": "auto_router/complexity_router",
|
|
"tags": [tag],
|
|
"complexity_router_config": {
|
|
"tiers": dict.fromkeys(("SIMPLE", "MEDIUM", "COMPLEX", "REASONING"), target),
|
|
},
|
|
},
|
|
}
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_direct_classifier_and_plugin_prelude_never_inherit_parent_decisions(
|
|
self, _tracing: "tuple[OpenTelemetryV2, InMemorySpanExporter]", monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
logger, exporter = _tracing
|
|
completed: Final = asyncio.Event()
|
|
|
|
class ObservedClassifier:
|
|
prelude: Mapping[str, object] = {}
|
|
|
|
async def run(self, context: RoutingContext) -> RoutingContext:
|
|
self.prelude = dict(context.metadata)
|
|
return context
|
|
|
|
async def classify(self, context: RoutingContext) -> str:
|
|
await litellm.acompletion(
|
|
model="openai/gpt-5.6-luna",
|
|
api_key="test",
|
|
mock_response="SIMPLE",
|
|
messages=[{"role": "user", "content": "choose a tier"}],
|
|
metadata={**context.metadata, "internal_call_origin": "autorouter_classifier"},
|
|
)
|
|
return "SIMPLE"
|
|
|
|
class CallbackComplete(CustomLogger):
|
|
async def async_log_success_event(
|
|
self, kwargs: Mapping[str, object], response_obj: object, start_time: datetime, end_time: datetime
|
|
) -> None:
|
|
completed.set()
|
|
|
|
monkeypatch.setattr(litellm, "_async_success_callback", [logger, CallbackComplete()])
|
|
classifier: Final = ObservedClassifier()
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
**self._marker("blue"),
|
|
"litellm_params": {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_config": {
|
|
"classifier_type": "custom",
|
|
"classifier_plugin": classifier,
|
|
"tiers": {"SIMPLE": "answer"},
|
|
"session_affinity": False,
|
|
"deployment_affinity": False,
|
|
},
|
|
},
|
|
},
|
|
{"model_name": "answer", "litellm_params": {"model": "openai/gpt-5.6-luna", "api_key": "test"}},
|
|
],
|
|
plugins=[classifier],
|
|
)
|
|
stale: Final = {"routing_decision": {"router_model_name": "previous"}, "_autorouter_baseline_route": "old"}
|
|
request: Final = {"metadata": {**stale}, "litellm_metadata": {**stale, "user_api_key_team_id": "team"}}
|
|
with logger.tracer.start_as_current_span("request") as root:
|
|
set_request_root_span(root)
|
|
await router.async_get_available_deployment(
|
|
model="traced-router", request_kwargs=request, messages=[{"role": "user", "content": "hello"}]
|
|
)
|
|
await completed.wait()
|
|
classifier_span: Final = next(
|
|
span
|
|
for span in exporter.get_finished_spans()
|
|
if (span.attributes or {}).get("litellm.request.purpose") == "autorouter_classifier"
|
|
)
|
|
assert not any(key.startswith("litellm.routing.") for key in classifier_span.attributes)
|
|
assert "routing_decision" not in classifier.prelude
|
|
assert "_autorouter_baseline_route" not in classifier.prelude
|
|
assert classifier.prelude["user_api_key_team_id"] == "team"
|
|
assert "routing_decision" not in request["metadata"]
|
|
assert "_autorouter_baseline_route" not in request["metadata"]
|
|
assert request["litellm_metadata"]["routing_decision"]["cause"] == "classifier_plugin"
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("build_fails", [False, True])
|
|
async def test_semantic_internal_failure_keeps_phase_identity_without_parent_retry_attribution(
|
|
self,
|
|
_tracing: "tuple[OpenTelemetryV2, InMemorySpanExporter]",
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
respx_mock: respx.MockRouter,
|
|
build_fails: bool,
|
|
) -> None:
|
|
logger, exporter = _tracing
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
query_statuses: Final = iter((503, 200))
|
|
|
|
def embedding_response(request: httpx.Request) -> httpx.Response:
|
|
status: Final = (
|
|
400
|
|
if build_fails
|
|
else (next(query_statuses) if json.loads(request.content)["input"] == ["query-retry"] else 200)
|
|
)
|
|
return httpx.Response(
|
|
status,
|
|
headers={"x-should-retry": "false"},
|
|
json={
|
|
"object": "list",
|
|
"model": "text-embedding-3-small",
|
|
"data": [{"object": "embedding", "index": 0, "embedding": [1.0, 0.0]}],
|
|
"usage": {"prompt_tokens": 1, "total_tokens": 1},
|
|
},
|
|
)
|
|
|
|
upstream: Final = respx_mock.post("https://embedding.example/v1/embeddings").mock(
|
|
side_effect=embedding_response
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "semantic",
|
|
"model_info": {"id": "semantic-definition"},
|
|
"litellm_params": {
|
|
"model": "auto_router/semantic-router",
|
|
"auto_router_default_model": "answer",
|
|
"auto_router_embedding_model": "embed",
|
|
"auto_router_config": json.dumps({"routes": [{"name": "answer", "utterances": ["hello"]}]}),
|
|
},
|
|
},
|
|
*(
|
|
{
|
|
"model_name": "embed",
|
|
"model_info": {"id": f"embed-{index}"},
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-3-small",
|
|
"api_base": "https://embedding.example/v1",
|
|
"api_key": "test",
|
|
"max_retries": 0,
|
|
},
|
|
}
|
|
for index in range(2)
|
|
),
|
|
{"model_name": "answer", "litellm_params": {"model": "openai/gpt-5.6-luna", "api_key": "test"}},
|
|
],
|
|
num_retries=1,
|
|
retry_after=0,
|
|
)
|
|
stale: Final = {"routing_decision": {"router_model_name": "previous"}, "_autorouter_baseline_route": "old"}
|
|
request: Final = {"metadata": {**stale}, "litellm_metadata": {**stale}}
|
|
with logger.tracer.start_as_current_span("request") as root:
|
|
set_request_root_span(root)
|
|
call: Final = router.async_get_available_deployment(
|
|
model="semantic", request_kwargs=request, messages=[{"role": "user", "content": "query-retry"}]
|
|
)
|
|
if build_fails:
|
|
with pytest.raises(ValueError, match="API call failed"):
|
|
await call
|
|
else:
|
|
await call
|
|
spans: Final = {span.name: span for span in exporter.get_finished_spans()}
|
|
assert spans["route semantic"].attributes["litellm.routing.router_config_id"] == "semantic-definition"
|
|
assert not any(event.name == "litellm.routing.retry" for event in spans["request"].events)
|
|
if build_fails:
|
|
assert all(
|
|
"routing_decision" not in bucket and "_autorouter_baseline_route" not in bucket
|
|
for bucket in (request["metadata"], request["litellm_metadata"])
|
|
)
|
|
else:
|
|
assert sum(json.loads(call.request.content)["input"] == ["query-retry"] for call in upstream.calls) == 2
|
|
assert request["litellm_metadata"]["routing_decision"]["cause"] == "semantic_match"
|
|
assert sum(span.name == "route embed" for span in exporter.get_finished_spans()) == 2
|
|
assert any(event.name == "litellm.request.deployment_selected" for event in spans["request"].events)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_same_alias_and_tags_across_strategy_families_keep_selected_definition(
|
|
self, _tracing: "tuple[OpenTelemetryV2, InMemorySpanExporter]"
|
|
) -> None:
|
|
logger, exporter = _tracing
|
|
router: Final = Router(
|
|
model_list=[
|
|
self._marker("blue"),
|
|
{
|
|
"model_name": "traced-router",
|
|
"model_info": {"id": "definition-semantic"},
|
|
"litellm_params": {
|
|
"model": "auto_router/semantic-router",
|
|
"tags": ["blue"],
|
|
"auto_router_config": json.dumps({"routes": [{"name": "answer", "utterances": ["hello"]}]}),
|
|
"auto_router_default_model": "answer",
|
|
"auto_router_embedding_model": "embed",
|
|
},
|
|
},
|
|
]
|
|
)
|
|
with logger.start_phase_span("route traced-router"):
|
|
response: Final = await router.async_pre_routing_hook(
|
|
model="traced-router", request_kwargs={"metadata": {"tags": ["blue"]}}
|
|
)
|
|
assert response is None
|
|
span: Final = next(span for span in exporter.get_finished_spans() if span.name == "route traced-router")
|
|
assert span.attributes is not None
|
|
assert span.attributes["litellm.routing.router_config_id"] == router.model_list[1]["model_info"]["id"]
|
|
assert span.attributes["litellm.routing.router_type"] == "semantic"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_semantic_team_alias_keeps_registered_identity_after_routing_and_retry(
|
|
self,
|
|
_tracing: "tuple[OpenTelemetryV2, InMemorySpanExporter]",
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
respx_mock: respx.MockRouter,
|
|
) -> None:
|
|
logger, exporter = _tracing
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
respx_mock.post("https://embedding.example/v1/embeddings").respond(
|
|
200,
|
|
json={
|
|
"object": "list",
|
|
"model": "text-embedding-3-small",
|
|
"data": [{"object": "embedding", "index": 0, "embedding": [1.0, 0.0]}],
|
|
"usage": {"prompt_tokens": 1, "total_tokens": 1},
|
|
},
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "internal-semantic-router",
|
|
"model_info": {
|
|
"id": "semantic-definition",
|
|
"team_id": "trace-team",
|
|
"team_public_model_name": "public-router",
|
|
},
|
|
"litellm_params": {
|
|
"model": "auto_router/semantic-router",
|
|
"auto_router_config": json.dumps({"routes": [{"name": "answer", "utterances": ["hello"]}]}),
|
|
"auto_router_default_model": "answer",
|
|
"auto_router_embedding_model": "embed",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "embed",
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-3-small",
|
|
"api_base": "https://embedding.example/v1",
|
|
"api_key": "test",
|
|
},
|
|
},
|
|
{"model_name": "answer", "litellm_params": {"model": "openai/gpt-5.6-luna", "api_key": "test"}},
|
|
]
|
|
)
|
|
request: Final = {"metadata": {"user_api_key_team_id": "trace-team"}}
|
|
with logger.tracer.start_as_current_span("request"):
|
|
await router.async_get_available_deployment(
|
|
model="public-router", request_kwargs=request, messages=[{"role": "user", "content": "hello"}]
|
|
)
|
|
router.log_retry({**request, "model": "answer"}, ValueError("retry"))
|
|
decision: Final = request["metadata"]["routing_decision"]
|
|
assert decision["router_model_name"] == router.model_list[0]["model_name"]
|
|
assert decision["router_config_id"] == router.model_list[0]["model_info"]["id"]
|
|
assert decision["cause"] == "semantic_match"
|
|
spans: Final = {span.name: span for span in exporter.get_finished_spans()}
|
|
assert (
|
|
spans["route public-router"].attributes["litellm.routing.router_model_name"]
|
|
== decision["router_model_name"]
|
|
)
|
|
assert (
|
|
spans["route public-router"].attributes["litellm.routing.router_config_id"] == decision["router_config_id"]
|
|
)
|
|
retry: Final = next(event for event in spans["request"].events if event.name == "litellm.routing.retry")
|
|
assert retry.attributes["litellm.routing.router_model_name"] == decision["router_model_name"]
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("passthrough", [False, True])
|
|
async def test_attempt_spans_keep_selected_config_and_plain_fallback_clears_it(
|
|
self, _tracing: "tuple[OpenTelemetryV2, InMemorySpanExporter]", passthrough: bool
|
|
) -> None:
|
|
logger, exporter = _tracing
|
|
router: Final = Router(
|
|
model_list=[
|
|
self._marker("blue"),
|
|
self._marker("green"),
|
|
{
|
|
"model_name": "answer",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6-luna",
|
|
"api_key": "test",
|
|
"use_in_pass_through": True,
|
|
"tags": ["blue", "green"],
|
|
},
|
|
},
|
|
],
|
|
enable_tag_filtering=True,
|
|
)
|
|
request: Final = {"metadata": {"tags": ["green"]}, "litellm_metadata": {"tags": ["green"]}}
|
|
pick: Final = (
|
|
router.async_get_available_deployment_for_pass_through
|
|
if passthrough
|
|
else router.async_get_available_deployment
|
|
)
|
|
with logger.tracer.start_as_current_span("request"):
|
|
await pick(model="traced-router", request_kwargs=request, messages=[{"role": "user", "content": "hello"}])
|
|
decision: Final = request["litellm_metadata"]["routing_decision"]
|
|
assert decision["router_config_id"] == "definition-green"
|
|
assert decision["router_config_updated_at"] == "2026-10-01T12:00:00+00:00"
|
|
assert (
|
|
decision["router_config_fingerprint"]
|
|
== router.complexity_routers["traced-router"][1].definition_fingerprint
|
|
)
|
|
assert "routing_decision" not in request["metadata"]
|
|
router.log_retry({**request, "model": "answer"}, ValueError("private-error-text"))
|
|
await pick(model="answer", request_kwargs=request, messages=[{"role": "user", "content": "hello"}])
|
|
assert "routing_decision" not in request["litellm_metadata"]
|
|
spans: Final = {span.name: span for span in exporter.get_finished_spans()}
|
|
assert spans["route traced-router"].attributes["litellm.routing.router_config_id"] == "definition-green"
|
|
assert spans["route traced-router"].attributes["litellm.routing.routed_model"] == "answer"
|
|
assert not any(key.startswith("litellm.routing.") for key in spans["route answer"].attributes)
|
|
retry: Final = next(event for event in spans["request"].events if event.name == "litellm.routing.retry")
|
|
assert retry.attributes["error.type"] == "ValueError"
|
|
assert retry.attributes["litellm.retry.count"] == 1
|
|
assert "private-error-text" not in str(retry.attributes)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_retry_records_name_the_over_tpm_refusal_as_its_logs_do(
|
|
self, _tracing: "tuple[OpenTelemetryV2, InMemorySpanExporter]"
|
|
) -> None:
|
|
logger, exporter = _tracing
|
|
router: Final = Router(
|
|
model_list=[
|
|
self._marker("blue"),
|
|
{"model_name": "answer", "litellm_params": {"model": "openai/gpt-5.6-luna", "api_key": "test"}},
|
|
],
|
|
)
|
|
request: Final = {"metadata": {}}
|
|
refusal: Final = RequestOverDeploymentTpmError(message="over tpm", model="answer", llm_provider="openai")
|
|
with logger.tracer.start_as_current_span("request"):
|
|
await router.async_get_available_deployment(
|
|
model="traced-router", request_kwargs=request, messages=[{"role": "user", "content": "hello"}]
|
|
)
|
|
router.log_retry({**request, "model": "answer"}, refusal)
|
|
spans: Final = {span.name: span for span in exporter.get_finished_spans()}
|
|
retry: Final = next(event for event in spans["request"].events if event.name == "litellm.routing.retry")
|
|
assert retry.attributes["error.type"] == refusal.logged_error_class
|
|
assert request["metadata"]["previous_models"][-1]["exception_type"] == refusal.logged_error_class
|
|
|
|
|
|
@pytest.fixture()
|
|
def _vcr_outcome_gate(request, vcr):
|
|
install_live_call_probe(request, vcr)
|
|
yield
|
|
record_vcr_outcome(request, vcr)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def isolate_litellm_state():
|
|
"""
|
|
Per-function isolation fixture.
|
|
|
|
Resets litellm globals to their true defaults before each test and
|
|
restores them afterward, so tests don't leak side effects.
|
|
Works safely under pytest-xdist parallel execution.
|
|
"""
|
|
original_state = {}
|
|
for attr in (
|
|
"callbacks",
|
|
"success_callback",
|
|
"failure_callback",
|
|
"_async_success_callback",
|
|
"_async_failure_callback",
|
|
):
|
|
if hasattr(litellm, attr):
|
|
val = getattr(litellm, attr)
|
|
original_state[attr] = val.copy() if val else []
|
|
for attr in ("pre_call_rules", "post_call_rules"):
|
|
if hasattr(litellm, attr):
|
|
val = getattr(litellm, attr)
|
|
original_state[attr] = val.copy() if val else []
|
|
for attr in _SCALAR_DEFAULTS:
|
|
if hasattr(litellm, attr):
|
|
original_state[attr] = getattr(litellm, attr)
|
|
if hasattr(litellm, "in_memory_llm_clients_cache"):
|
|
litellm.in_memory_llm_clients_cache.flush_cache()
|
|
for attr in (
|
|
"callbacks",
|
|
"success_callback",
|
|
"failure_callback",
|
|
"_async_success_callback",
|
|
"_async_failure_callback",
|
|
"pre_call_rules",
|
|
"post_call_rules",
|
|
):
|
|
if hasattr(litellm, attr):
|
|
setattr(litellm, attr, [])
|
|
for attr, default_val in _SCALAR_DEFAULTS.items():
|
|
if hasattr(litellm, attr):
|
|
setattr(litellm, attr, default_val)
|
|
yield
|
|
asyncio.run(GLOBAL_LOGGING_WORKER.clear_queue())
|
|
if hasattr(litellm, "in_memory_llm_clients_cache"):
|
|
litellm.in_memory_llm_clients_cache.flush_cache()
|
|
for attr, original_value in original_state.items():
|
|
if hasattr(litellm, attr):
|
|
setattr(litellm, attr, original_value)
|
|
publish(litellm.model_cost)
|
|
|
|
|
|
_SCALAR_DEFAULTS = {
|
|
"num_retries": getattr(litellm, "num_retries", None),
|
|
"num_retries_per_request": getattr(litellm, "num_retries_per_request", None),
|
|
"request_timeout": getattr(litellm, "request_timeout", None),
|
|
"set_verbose": getattr(litellm, "set_verbose", False),
|
|
"cache": getattr(litellm, "cache", None),
|
|
"allowed_fails": getattr(litellm, "allowed_fails", 3),
|
|
"default_fallbacks": getattr(litellm, "default_fallbacks", None),
|
|
"enable_azure_ad_token_refresh": getattr(litellm, "enable_azure_ad_token_refresh", None),
|
|
"tag_budget_config": getattr(litellm, "tag_budget_config", None),
|
|
"model_cost": getattr(litellm, "model_cost", None),
|
|
"token_counter": getattr(litellm, "token_counter", None),
|
|
"disable_aiohttp_transport": getattr(litellm, "disable_aiohttp_transport", False),
|
|
"force_ipv4": getattr(litellm, "force_ipv4", False),
|
|
"drop_params": getattr(litellm, "drop_params", None),
|
|
"modify_params": getattr(litellm, "modify_params", False),
|
|
"api_base": getattr(litellm, "api_base", None),
|
|
"api_key": getattr(litellm, "api_key", None),
|
|
}
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def setup_and_teardown():
|
|
"""
|
|
Module-scoped setup. Reloads litellm only in single-process mode
|
|
(skipped under xdist to avoid cross-worker interference).
|
|
"""
|
|
import litellm
|
|
|
|
worker_id = os.environ.get("PYTEST_XDIST_WORKER", None)
|
|
if worker_id is None:
|
|
importlib.reload(litellm)
|
|
try:
|
|
if hasattr(litellm, "proxy") and hasattr(litellm.proxy, "proxy_server"):
|
|
import litellm.proxy.proxy_server
|
|
|
|
importlib.reload(litellm.proxy.proxy_server)
|
|
except Exception as e:
|
|
print(f"Error reloading litellm.proxy.proxy_server: {e}")
|
|
if hasattr(litellm, "in_memory_llm_clients_cache"):
|
|
litellm.in_memory_llm_clients_cache.flush_cache()
|
|
yield
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@patch("azure.identity.get_bearer_token_provider")
|
|
@patch("azure.identity.ClientSecretCredential")
|
|
def test_router_init_azure_service_principal_with_secret_with_environment_variables(
|
|
mocked_credential: MagicMock,
|
|
mocked_get_bearer_token_provider: MagicMock,
|
|
monkeypatch,
|
|
) -> None:
|
|
"""
|
|
Test router initialization and sample completion using Azure Service Principal with Secret authentication workflow,
|
|
having provided the (mocked) credentials in environment variables and not provided any API key.
|
|
|
|
To allow for local testing without real credentials, first must mock Azure SDK authentication functions
|
|
and environment variables.
|
|
"""
|
|
monkeypatch.delenv("AZURE_AI_API_KEY", raising=False)
|
|
monkeypatch.delenv("AZURE_OPENAI_API_KEY", raising=False)
|
|
monkeypatch.delenv("AZURE_API_KEY", raising=False)
|
|
litellm.enable_azure_ad_token_refresh = True
|
|
# mock the token provider function
|
|
mocked_func_generating_token = MagicMock(return_value="test_token")
|
|
mocked_get_bearer_token_provider.return_value = mocked_func_generating_token
|
|
|
|
# set environment variables with mocked credentials using monkeypatch
|
|
# so both common_utils._resolve_env_var and get_azure_ad_token_provider see them
|
|
monkeypatch.setenv("AZURE_CLIENT_ID", "test_client_id")
|
|
monkeypatch.setenv("AZURE_CLIENT_SECRET", "test_client_secret")
|
|
monkeypatch.setenv("AZURE_TENANT_ID", "test_tenant_id")
|
|
|
|
# define the model list
|
|
model_list = [
|
|
{
|
|
# test case for Azure Service Principal with Secret authentication
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {
|
|
# checkout there is no api_key here -
|
|
# AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID environment variables should be used instead
|
|
"model": "gpt-4o",
|
|
"base_model": "gpt-4o",
|
|
"api_base": "test_api_base",
|
|
"api_version": "2024-01-01-preview",
|
|
"custom_llm_provider": "azure",
|
|
},
|
|
"model_info": {"mode": "completion"},
|
|
},
|
|
]
|
|
|
|
# initialize the router
|
|
router = Router(model_list=model_list)
|
|
|
|
# # first check if environment variables were used at all
|
|
# mocked_environ.assert_called()
|
|
# # then check if the client was initialized with the correct environment variables
|
|
# mocked_credential.assert_called_with(
|
|
# **{
|
|
# "client_id": environment_variables_expected_to_use["AZURE_CLIENT_ID"],
|
|
# "client_secret": environment_variables_expected_to_use[
|
|
# "AZURE_CLIENT_SECRET"
|
|
# ],
|
|
# "tenant_id": environment_variables_expected_to_use["AZURE_TENANT_ID"],
|
|
# }
|
|
# )
|
|
# # check if the token provider was called at all
|
|
# mocked_get_bearer_token_provider.assert_called()
|
|
# # then check if the token provider was initialized with the mocked credential
|
|
# for call_args in mocked_get_bearer_token_provider.call_args_list:
|
|
# assert call_args.args[0] == mocked_credential.return_value
|
|
# # however, at this point token should not be fetched yet
|
|
# mocked_func_generating_token.assert_not_called()
|
|
|
|
# now let's try to make a completion call
|
|
deployment = model_list[0]
|
|
model = deployment["model_name"]
|
|
messages = [{"role": "user", "content": f"write a one sentence poem {time.time()}?"}]
|
|
with pytest.raises(APIConnectionError):
|
|
# of course, it will raise an error, because URL is mocked
|
|
router.completion(model=model, messages=messages, temperature=1) # type: ignore
|
|
|
|
# finally verify if the mocked token was used by Azure SDK
|
|
mocked_func_generating_token.assert_called()
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
def test_update_kwargs_before_fallbacks_unit_test():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_key": "bad-key",
|
|
"api_version": os.getenv("AZURE_API_VERSION"),
|
|
"api_base": os.getenv("AZURE_AI_API_BASE"),
|
|
},
|
|
}
|
|
],
|
|
)
|
|
|
|
kwargs = {"messages": [{"role": "user", "content": "write 1 sentence poem"}]}
|
|
|
|
router._update_kwargs_before_fallbacks(
|
|
model="gpt-3.5-turbo",
|
|
kwargs=kwargs,
|
|
)
|
|
|
|
assert kwargs["litellm_trace_id"] is not None
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
def test_router_get_model_info_wildcard_routes():
|
|
os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"
|
|
litellm.model_cost = litellm.get_model_cost_map(url="")
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini/*",
|
|
"litellm_params": {"model": "gemini/*"},
|
|
"model_info": {"id": 1},
|
|
},
|
|
]
|
|
)
|
|
model_info = router.get_router_model_info(deployment=None, received_model_name="gemini/gemini-2.5-flash", id="1")
|
|
print(model_info)
|
|
assert model_info is not None
|
|
assert model_info["tpm"] is not None
|
|
assert model_info["rpm"] is not None
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.flaky(retries=3, delay=1)
|
|
async def test_router_get_model_group_usage_wildcard_routes():
|
|
os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"
|
|
litellm.model_cost = litellm.get_model_cost_map(url="")
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini/*",
|
|
"litellm_params": {"model": "gemini/*"},
|
|
"model_info": {"id": 1},
|
|
},
|
|
]
|
|
)
|
|
|
|
resp = await router.acompletion(
|
|
model="gemini/gemini-2.5-flash",
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
mock_response="Hello, I'm good.",
|
|
)
|
|
print(resp)
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
tpm, rpm = await router.get_model_group_usage(model_group="gemini/gemini-2.5-flash")
|
|
|
|
assert tpm is not None, "tpm is None"
|
|
assert rpm is not None, "rpm is None"
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.asyncio
|
|
async def test_call_router_callbacks_on_success():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini/*",
|
|
"litellm_params": {"model": "gemini/*"},
|
|
"model_info": {"id": 1},
|
|
},
|
|
]
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="gemini/gemini-2.5-flash",
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
mock_response="Hello, I'm good.",
|
|
)
|
|
assert await _usage_settles(router, "gemini/gemini-2.5-flash", kinds=2) == {
|
|
"tpm": response.usage.total_tokens,
|
|
"rpm": 1,
|
|
}
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.serial
|
|
@pytest.mark.asyncio
|
|
async def test_call_router_callbacks_on_failure():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini/*",
|
|
"litellm_params": {"model": "gemini/*"},
|
|
"model_info": {"id": 1},
|
|
},
|
|
]
|
|
)
|
|
|
|
with pytest.raises(litellm.RateLimitError):
|
|
await router.acompletion(
|
|
model="gemini/gemini-2.5-flash",
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
mock_response="litellm.RateLimitError",
|
|
num_retries=0,
|
|
)
|
|
assert await _usage_settles(router, "gemini/gemini-2.5-flash", kinds=1) == {"rpm": 1}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rate_limit_error_callback():
|
|
"""
|
|
Assert a callback is hit, if a model group starts hitting rate limit errors
|
|
|
|
Relevant issue: https://github.com/BerriAI/litellm/issues/4096
|
|
"""
|
|
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLogging
|
|
|
|
customHandler = CompletionCustomHandler()
|
|
litellm.callbacks = [customHandler]
|
|
litellm.success_callback = []
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "my-test-gpt",
|
|
"litellm_params": {
|
|
"model": "gpt-5-mini",
|
|
"mock_response": "litellm.RateLimitError",
|
|
},
|
|
}
|
|
],
|
|
allowed_fails=2,
|
|
num_retries=0,
|
|
)
|
|
|
|
litellm_logging_obj = LiteLLMLogging(
|
|
model="my-test-gpt",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
stream=False,
|
|
call_type="acompletion",
|
|
litellm_call_id="1234",
|
|
start_time=datetime.now(),
|
|
function_id="1234",
|
|
)
|
|
|
|
try:
|
|
_ = await router.acompletion(
|
|
model="my-test-gpt",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
with patch.object(customHandler, "log_model_group_rate_limit_error", new=AsyncMock()) as mock_client:
|
|
print(f"customHandler.log_model_group_rate_limit_error: {customHandler.log_model_group_rate_limit_error}")
|
|
|
|
try:
|
|
_ = await router.acompletion(
|
|
model="my-test-gpt",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
litellm_logging_obj=litellm_logging_obj,
|
|
)
|
|
except (litellm.RateLimitError, ValueError):
|
|
pass
|
|
|
|
await asyncio.sleep(3)
|
|
mock_client.assert_called_once()
|
|
|
|
assert "original_model_group" in mock_client.call_args.kwargs
|
|
assert mock_client.call_args.kwargs["original_model_group"] == "my-test-gpt"
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.asyncio
|
|
async def test_router_model_group_headers():
|
|
os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"
|
|
litellm.model_cost = litellm.get_model_cost_map(url="")
|
|
from litellm.types.utils import OPENAI_RESPONSE_HEADERS
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini/*",
|
|
"litellm_params": {"model": "gemini/*"},
|
|
"model_info": {"id": 1},
|
|
}
|
|
]
|
|
)
|
|
|
|
for _ in range(2):
|
|
resp = await router.acompletion(
|
|
model="gemini/gemini-2.5-flash",
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
mock_response="Hello, I'm good.",
|
|
)
|
|
await asyncio.sleep(1)
|
|
|
|
assert resp._hidden_params["additional_headers"]["x-litellm-model-group"] == "gemini/gemini-2.5-flash"
|
|
|
|
assert "x-ratelimit-remaining-requests" in resp._hidden_params["additional_headers"]
|
|
assert "x-ratelimit-remaining-tokens" in resp._hidden_params["additional_headers"]
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.asyncio
|
|
async def test_get_remaining_model_group_usage():
|
|
os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"
|
|
litellm.model_cost = litellm.get_model_cost_map(url="")
|
|
from litellm.types.utils import OPENAI_RESPONSE_HEADERS
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini/*",
|
|
"litellm_params": {"model": "gemini/*"},
|
|
"model_info": {"id": 1},
|
|
}
|
|
]
|
|
)
|
|
for _ in range(2):
|
|
resp = await router.acompletion(
|
|
model="gemini/gemini-2.5-flash",
|
|
messages=[{"role": "user", "content": "Hello, how are you?"}],
|
|
mock_response="Hello, I'm good.",
|
|
)
|
|
assert "x-ratelimit-remaining-tokens" in resp._hidden_params["additional_headers"]
|
|
assert "x-ratelimit-remaining-requests" in resp._hidden_params["additional_headers"]
|
|
await asyncio.sleep(1)
|
|
|
|
remaining_usage = await router.get_remaining_model_group_usage(model_group="gemini/gemini-2.5-flash")
|
|
assert remaining_usage is not None
|
|
assert "x-ratelimit-remaining-requests" in remaining_usage
|
|
assert "x-ratelimit-remaining-tokens" in remaining_usage
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.parametrize(
|
|
"potential_access_group, expected_result",
|
|
[("gemini-models", True), ("gemini-models-2", False), ("gemini/*", False)],
|
|
)
|
|
def test_router_get_model_access_groups(potential_access_group, expected_result):
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini/*",
|
|
"litellm_params": {"model": "gemini/*"},
|
|
"model_info": {"id": 1, "access_groups": ["gemini-models"]},
|
|
},
|
|
]
|
|
)
|
|
access_groups = router.is_model_access_group_for_wildcard_route(model_access_group=potential_access_group)
|
|
assert access_groups == expected_result
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
def test_router_redis_cache():
|
|
router = Router(model_list=[{"model_name": "gemini/*", "litellm_params": {"model": "gemini/*"}}])
|
|
|
|
redis_cache = redis_cache_double()
|
|
|
|
router.update_redis_cache(cache=redis_cache)
|
|
|
|
assert router.cache.redis_cache == redis_cache
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
async def test_router_clientside_credential_not_reused_by_other_callers(respx_mock, monkeypatch: pytest.MonkeyPatch):
|
|
"""End-to-end regression test for LIT-7811.
|
|
|
|
One caller's request-scoped api_key must never leak into a later, unrelated
|
|
caller's request. Before the fix, the router registered the caller-supplied
|
|
credential as a second, permanent deployment for the shared model group, so
|
|
plain follow-up calls with no override of their own could be load-balanced
|
|
onto it and reach the provider with the first caller's key.
|
|
"""
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
|
|
route = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-1",
|
|
"object": "chat.completion",
|
|
"created": 0,
|
|
"model": "gpt-4o",
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "hi"}, "finish_reason": "stop"}],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
},
|
|
)
|
|
)
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "shared-model",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "configured-key"},
|
|
"model_info": {"id": "configured-deployment"},
|
|
}
|
|
]
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="shared-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
api_key="alternate-tenant-key",
|
|
)
|
|
assert route.calls[-1].request.headers["authorization"] == "Bearer alternate-tenant-key"
|
|
|
|
# The forwarded credential must never become a routable deployment for the
|
|
# model group other callers share.
|
|
assert [d["model_info"]["id"] for d in router.get_model_list(model_name="shared-model")] == [
|
|
"configured-deployment"
|
|
]
|
|
|
|
for _ in range(20):
|
|
await router.acompletion(
|
|
model="shared-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
)
|
|
|
|
used_auth_headers = {call.request.headers["authorization"] for call in route.calls[1:]}
|
|
assert used_auth_headers == {"Bearer configured-key"}
|
|
|
|
|
|
def _caused_by_an_entra_rejection(error: litellm.AuthenticationError) -> litellm.AuthenticationError:
|
|
from litellm.llms.azure.common_utils import EntraRejectionError
|
|
|
|
try:
|
|
raise error from EntraRejectionError(status_code=401, message=error.message)
|
|
except litellm.AuthenticationError as raised:
|
|
return raised
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.parametrize("caller_credential", ["client_secret", "azure_password", "azure_ad_token"])
|
|
async def test_router_caller_entra_credential_rejection_leaves_the_shared_deployment_routable(caller_credential):
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "entra-model",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1",
|
|
"api_base": "https://litellm-qa.openai.azure.com",
|
|
"api_version": "2025-04-01-preview",
|
|
"tenant_id": "configured-tenant",
|
|
"client_id": "configured-client",
|
|
"client_secret": "configured-secret",
|
|
},
|
|
"model_info": {"id": "configured-deployment"},
|
|
}
|
|
]
|
|
)
|
|
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response=_caused_by_an_entra_rejection(
|
|
litellm.AuthenticationError(
|
|
message="Failed to get Azure AD token: AADSTS7000215: Invalid client secret provided.",
|
|
llm_provider="azure",
|
|
model="gpt-4.1",
|
|
)
|
|
),
|
|
**{caller_credential: "not-the-credential"},
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response="hi",
|
|
)
|
|
|
|
assert response.choices[0].message.content == "hi"
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.parametrize(
|
|
"caller_field",
|
|
[
|
|
pytest.param({"client_secret": "environment-secret"}, id="environment_secret"),
|
|
pytest.param({"client_id": "environment-client"}, id="environment_client_id"),
|
|
pytest.param({"azure_scope": "https://cognitiveservices.azure.com/.default"}, id="default_scope"),
|
|
pytest.param({"client_secret": ""}, id="blank_secret"),
|
|
],
|
|
)
|
|
async def test_router_caller_entra_field_that_changes_the_deployment_credential_leaves_it_routable(
|
|
caller_field, monkeypatch
|
|
):
|
|
monkeypatch.setenv("AZURE_CLIENT_SECRET", "environment-secret")
|
|
monkeypatch.setenv("AZURE_CLIENT_ID", "environment-client")
|
|
monkeypatch.delenv("AZURE_SCOPE", raising=False)
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "entra-model",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1",
|
|
"api_base": "https://litellm-qa.openai.azure.com",
|
|
"api_version": "2025-04-01-preview",
|
|
"tenant_id": "configured-tenant",
|
|
"client_id": "configured-client",
|
|
"client_secret": "configured-secret",
|
|
"azure_scope": "api://configured-app/.default",
|
|
},
|
|
"model_info": {"id": "configured-deployment"},
|
|
}
|
|
]
|
|
)
|
|
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response=_caused_by_an_entra_rejection(
|
|
litellm.AuthenticationError(
|
|
message="Failed to get Azure AD token: AADSTS7000215: Invalid client secret provided.",
|
|
llm_provider="azure",
|
|
model="gpt-4.1",
|
|
)
|
|
),
|
|
**caller_field,
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response="hi",
|
|
)
|
|
|
|
assert response.choices[0].message.content == "hi"
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.parametrize(
|
|
("deployment_secret", "router_default_secret"),
|
|
[
|
|
pytest.param({"client_secret": "configured-secret"}, None, id="deployment"),
|
|
pytest.param({}, {"client_secret": "configured-secret"}, id="router_default_litellm_params"),
|
|
],
|
|
)
|
|
async def test_router_configured_entra_credential_rejection_cools_down_the_deployment(
|
|
deployment_secret, router_default_secret
|
|
):
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "entra-model",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1",
|
|
"api_base": "https://litellm-qa.openai.azure.com",
|
|
"api_version": "2025-04-01-preview",
|
|
"tenant_id": "configured-tenant",
|
|
"client_id": "configured-client",
|
|
**deployment_secret,
|
|
},
|
|
"model_info": {"id": "configured-deployment"},
|
|
}
|
|
],
|
|
default_litellm_params=router_default_secret,
|
|
)
|
|
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response=litellm.AuthenticationError(
|
|
message="Failed to get Azure AD token: AADSTS7000222: The provided client secret keys are expired.",
|
|
llm_provider="azure",
|
|
model="gpt-4.1",
|
|
),
|
|
)
|
|
|
|
with pytest.raises(litellm.RouterRateLimitError) as excinfo:
|
|
await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response="hi",
|
|
)
|
|
|
|
assert excinfo.value.all_deployments_in_cooldown is True
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.parametrize(
|
|
"caller_fields",
|
|
[
|
|
pytest.param({"azure_scope": "https://cognitiveservices.azure.com/.default"}, id="default_scope"),
|
|
pytest.param({"azure_username": ""}, id="empty_username"),
|
|
pytest.param({"client_secret": "environment-secret"}, id="environment_secret"),
|
|
],
|
|
)
|
|
async def test_router_caller_entra_field_that_changes_nothing_still_cools_down_a_rejected_deployment(
|
|
caller_fields, monkeypatch
|
|
):
|
|
monkeypatch.delenv("AZURE_SCOPE", raising=False)
|
|
monkeypatch.setenv("AZURE_CLIENT_SECRET", "environment-secret")
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "entra-model",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1",
|
|
"api_base": "https://litellm-qa.openai.azure.com",
|
|
"api_version": "2025-04-01-preview",
|
|
"tenant_id": "configured-tenant",
|
|
"client_id": "configured-client",
|
|
},
|
|
"model_info": {"id": "configured-deployment"},
|
|
}
|
|
]
|
|
)
|
|
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response=_caused_by_an_entra_rejection(
|
|
litellm.AuthenticationError(
|
|
message="Failed to get Azure AD token: AADSTS7000215: Invalid client secret provided.",
|
|
llm_provider="azure",
|
|
model="gpt-4.1",
|
|
)
|
|
),
|
|
**caller_fields,
|
|
)
|
|
|
|
with pytest.raises(litellm.RouterRateLimitError) as excinfo:
|
|
await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response="hi",
|
|
)
|
|
|
|
assert excinfo.value.all_deployments_in_cooldown is True
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
async def test_router_caller_repeating_the_secret_an_empty_router_default_selects_still_cools_down(monkeypatch):
|
|
monkeypatch.setenv("AZURE_CLIENT_SECRET", "environment-secret")
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "entra-model",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1",
|
|
"api_base": "https://litellm-qa.openai.azure.com",
|
|
"api_version": "2025-04-01-preview",
|
|
"tenant_id": "configured-tenant",
|
|
"client_id": "configured-client",
|
|
"client_secret": "configured-secret",
|
|
},
|
|
"model_info": {"id": "configured-deployment"},
|
|
}
|
|
],
|
|
default_litellm_params={"client_secret": ""},
|
|
)
|
|
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response=_caused_by_an_entra_rejection(
|
|
litellm.AuthenticationError(
|
|
message="Failed to get Azure AD token: AADSTS7000215: Invalid client secret provided.",
|
|
llm_provider="azure",
|
|
model="gpt-4.1",
|
|
)
|
|
),
|
|
client_secret="environment-secret",
|
|
)
|
|
|
|
with pytest.raises(litellm.RouterRateLimitError) as excinfo:
|
|
await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response="hi",
|
|
)
|
|
|
|
assert excinfo.value.all_deployments_in_cooldown is True
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
async def test_router_caller_entra_field_keeps_the_shared_deployment():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "shared-model",
|
|
"litellm_params": {"model": "openai/gpt-4.1-mini", "api_key": "configured-key"},
|
|
"model_info": {"id": "shared-deployment"},
|
|
}
|
|
]
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="shared-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response="hi",
|
|
client_id="caller-client",
|
|
)
|
|
|
|
assert response._hidden_params["model_id"] == "shared-deployment"
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.parametrize(
|
|
("litellm_params", "provider"),
|
|
[
|
|
pytest.param({"model": "openai/gpt-4.1-mini", "api_key": "revoked-key"}, "openai", id="openai"),
|
|
pytest.param(
|
|
{
|
|
"model": "azure/gpt-4.1",
|
|
"api_base": "https://litellm-qa.openai.azure.com",
|
|
"api_version": "2025-04-01-preview",
|
|
"api_key": "revoked-key",
|
|
},
|
|
"azure",
|
|
id="azure_api_key",
|
|
),
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("caller_entra_field", ["client_id", "azure_scope"])
|
|
async def test_router_auth_failure_entra_did_not_cause_cools_down_despite_a_caller_entra_field(
|
|
litellm_params, provider, caller_entra_field
|
|
):
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "shared-model",
|
|
"litellm_params": litellm_params,
|
|
"model_info": {"id": "shared-deployment"},
|
|
}
|
|
]
|
|
)
|
|
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await router.acompletion(
|
|
model="shared-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response=litellm.AuthenticationError(
|
|
message="Incorrect API key provided.", llm_provider=provider, model=litellm_params["model"]
|
|
),
|
|
**{caller_entra_field: "caller-value"},
|
|
)
|
|
|
|
with pytest.raises(litellm.RouterRateLimitError) as excinfo:
|
|
await router.acompletion(
|
|
model="shared-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
mock_response="hi",
|
|
)
|
|
|
|
assert excinfo.value.all_deployments_in_cooldown is True
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
async def test_router_fallback_auth_failure_after_an_entra_rejection_still_cools_down_the_fallback(monkeypatch):
|
|
from azure.core.exceptions import ClientAuthenticationError
|
|
|
|
def rejected_token() -> str:
|
|
raise ClientAuthenticationError(message="AADSTS7000215: Invalid client secret provided.")
|
|
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "entra-model",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1",
|
|
"api_base": "https://litellm-qa.openai.azure.com",
|
|
"api_version": "2025-04-01-preview",
|
|
"azure_ad_token_provider": rejected_token,
|
|
},
|
|
"model_info": {"id": "entra-deployment"},
|
|
},
|
|
{
|
|
"model_name": "key-model",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1",
|
|
"api_base": "https://litellm-qa-fallback.openai.azure.com",
|
|
"api_version": "2025-04-01-preview",
|
|
"api_key": "revoked-key",
|
|
},
|
|
"model_info": {"id": "key-deployment"},
|
|
},
|
|
],
|
|
fallbacks=[{"entra-model": ["key-model"]}],
|
|
)
|
|
|
|
with respx.mock:
|
|
fallback_route: Final = respx.post(url__startswith="https://litellm-qa-fallback.openai.azure.com").mock(
|
|
return_value=httpx.Response(
|
|
401, json={"error": {"code": "401", "message": "Access denied due to invalid subscription key."}}
|
|
)
|
|
)
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await router.acompletion(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
client_secret="not-the-credential",
|
|
)
|
|
|
|
with pytest.raises(litellm.RouterRateLimitError) as excinfo:
|
|
await router.acompletion(
|
|
model="key-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
)
|
|
|
|
assert fallback_route.call_count == 1
|
|
assert excinfo.value.all_deployments_in_cooldown is True
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
@pytest.mark.parametrize(
|
|
"stream_request",
|
|
[
|
|
pytest.param(
|
|
lambda router, **caller_fields: router.acompletion(
|
|
model="entra-model", messages=[{"role": "user", "content": "hi"}], stream=True, **caller_fields
|
|
),
|
|
id="chat_completions",
|
|
),
|
|
pytest.param(
|
|
lambda router, **caller_fields: router.aanthropic_messages(
|
|
model="entra-model",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
max_tokens=5,
|
|
stream=True,
|
|
**caller_fields,
|
|
),
|
|
id="messages",
|
|
),
|
|
],
|
|
)
|
|
async def test_router_caller_entra_rejection_on_a_stream_leaves_the_shared_deployment_routable(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
stream_request: Callable[..., Awaitable[object]],
|
|
) -> None:
|
|
from azure.core.exceptions import ClientAuthenticationError
|
|
|
|
def rejected_token() -> str:
|
|
raise ClientAuthenticationError(message="AADSTS7000215: Invalid client secret provided.")
|
|
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "entra-model",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1",
|
|
"api_base": "https://litellm-qa.openai.azure.com",
|
|
"api_version": "2025-04-01-preview",
|
|
"azure_ad_token_provider": rejected_token,
|
|
},
|
|
"model_info": {"id": "entra-deployment"},
|
|
}
|
|
]
|
|
)
|
|
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await stream_request(router, client_secret="not-the-credential")
|
|
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await stream_request(router)
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
def test_router_get_deployment_credentials():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini/*",
|
|
"litellm_params": {"model": "gemini/*", "api_key": "123"},
|
|
"model_info": {"id": "1"},
|
|
}
|
|
]
|
|
)
|
|
credentials = router.get_deployment_credentials(model_id="1")
|
|
assert credentials is not None
|
|
assert credentials["api_key"] == "123"
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
def test_router_get_deployment_credentials_with_provider():
|
|
"""
|
|
Test that get_deployment_credentials_with_provider returns credentials with provider info.
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4o",
|
|
"litellm_params": {
|
|
"model": "gpt-4o",
|
|
"api_key": "sk-test-123",
|
|
"api_base": "https://api.openai.com/v1",
|
|
},
|
|
"model_info": {"id": "openai-deployment-1"},
|
|
},
|
|
{
|
|
"model_name": "claude-3",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-3-sonnet",
|
|
"api_key": "sk-ant-123",
|
|
},
|
|
"model_info": {"id": "anthropic-deployment-1"},
|
|
},
|
|
]
|
|
)
|
|
|
|
# Test getting credentials by model_id
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="openai-deployment-1")
|
|
assert credentials is not None
|
|
assert credentials["api_key"] == "sk-test-123"
|
|
assert credentials["custom_llm_provider"] == "openai"
|
|
assert credentials["api_base"] == "https://api.openai.com/v1"
|
|
|
|
# Test getting credentials by model_group_name (model_name)
|
|
credentials2 = router.get_deployment_credentials_with_provider(model_id="claude-3")
|
|
assert credentials2 is not None
|
|
assert credentials2["api_key"] == "sk-ant-123"
|
|
assert credentials2["custom_llm_provider"] == "anthropic"
|
|
|
|
# Test with non-existent model
|
|
credentials3 = router.get_deployment_credentials_with_provider(model_id="non-existent")
|
|
assert credentials3 is None
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
def test_router_get_deployment_credentials_with_provider_wildcard():
|
|
"""
|
|
Test that get_deployment_credentials_with_provider handles wildcard patterns.
|
|
|
|
When a model like openai/gpt-4o is requested and the config has openai/*,
|
|
the method should resolve the wildcard pattern and return credentials.
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {
|
|
"model": "openai/*",
|
|
"api_key": "sk-wildcard-123",
|
|
"api_base": "https://api.openai.com/v1",
|
|
},
|
|
"model_info": {"id": "openai-wildcard-deployment"},
|
|
},
|
|
{
|
|
"model_name": "anthropic/*",
|
|
"litellm_params": {
|
|
"model": "anthropic/*",
|
|
"api_key": "sk-ant-wildcard-456",
|
|
},
|
|
"model_info": {"id": "anthropic-wildcard-deployment"},
|
|
},
|
|
]
|
|
)
|
|
|
|
# Test wildcard pattern matching for OpenAI
|
|
credentials = router.get_deployment_credentials_with_provider(model_id="openai/gpt-4o")
|
|
assert credentials is not None
|
|
assert credentials["api_key"] == "sk-wildcard-123"
|
|
assert credentials["custom_llm_provider"] == "openai"
|
|
assert credentials["api_base"] == "https://api.openai.com/v1"
|
|
|
|
# Test wildcard pattern matching for Anthropic
|
|
credentials2 = router.get_deployment_credentials_with_provider(model_id="anthropic/claude-3-opus")
|
|
assert credentials2 is not None
|
|
assert credentials2["api_key"] == "sk-ant-wildcard-456"
|
|
assert credentials2["custom_llm_provider"] == "anthropic"
|
|
|
|
# Test with non-matching model
|
|
credentials3 = router.get_deployment_credentials_with_provider(model_id="vertex_ai/gemini-pro")
|
|
assert credentials3 is None
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate", "isolate_litellm_state", "setup_and_teardown")
|
|
def test_router_get_deployment_model_info():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gemini/*",
|
|
"litellm_params": {"model": "gemini/*"},
|
|
"model_info": {"id": "1"},
|
|
}
|
|
]
|
|
)
|
|
model_info = router.get_deployment_model_info(model_id="1", model_name="gemini/gemini-1.5-flash")
|
|
assert model_info is not None
|
|
|
|
|
|
@pytest.fixture()
|
|
def _vcr_outcome_gate_router_unit(request, vcr):
|
|
install_live_call_probe(request, vcr)
|
|
yield
|
|
record_vcr_outcome(request, vcr)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def setup_and_teardown_router_unit():
|
|
"""
|
|
This fixture reloads litellm before every function. To speed up testing by removing callbacks being chained.
|
|
"""
|
|
from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER
|
|
|
|
asyncio.run(GLOBAL_LOGGING_WORKER.clear_queue())
|
|
importlib.reload(litellm)
|
|
try:
|
|
if hasattr(litellm, "proxy") and hasattr(litellm.proxy, "proxy_server"):
|
|
importlib.reload(litellm.proxy.proxy_server)
|
|
except Exception as e:
|
|
print(f"Error reloading litellm.proxy.proxy_server: {e}")
|
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
asyncio.set_event_loop(loop)
|
|
print(litellm)
|
|
yield
|
|
loop.close()
|
|
asyncio.set_event_loop(None)
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate_router_unit", "setup_and_teardown_router_unit")
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_deployment_not_mutated():
|
|
"""
|
|
Test async completion doesn't mutate deployment when .copy() is removed.
|
|
|
|
Optimization: Remove deployment["litellm_params"].copy() in _acompletion
|
|
since data is only read and spread into input_kwargs dict.
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5",
|
|
"litellm_params": {
|
|
"model": "gpt-5-mini",
|
|
"api_key": "test-key",
|
|
"temperature": 0.7,
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
deployment_before = router.get_deployment_by_model_group_name("gpt-3.5")
|
|
assert deployment_before is not None
|
|
original_params = deployment_before.litellm_params.model_dump()
|
|
|
|
with patch("litellm.acompletion", new_callable=AsyncMock) as mock_acompletion:
|
|
from litellm import ModelResponse
|
|
|
|
mock_acompletion.return_value = ModelResponse(
|
|
id="test",
|
|
choices=[{"message": {"role": "assistant", "content": "test"}, "index": 0}],
|
|
model="gpt-5-mini",
|
|
usage={"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30},
|
|
)
|
|
|
|
try:
|
|
await router.acompletion(
|
|
model="gpt-3.5",
|
|
messages=[{"role": "user", "content": "test"}],
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# Critical: Deployment params must be unchanged
|
|
deployment_after = router.get_deployment_by_model_group_name("gpt-3.5")
|
|
assert deployment_after is not None
|
|
assert deployment_after.litellm_params.model_dump() == original_params
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate_router_unit", "setup_and_teardown_router_unit")
|
|
def test_completion_deployment_not_mutated():
|
|
"""
|
|
Test sync completion doesn't mutate deployment when .copy() is removed.
|
|
|
|
Optimization: Remove deployment["litellm_params"].copy() in _completion
|
|
since data is only read and spread into input_kwargs dict.
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5",
|
|
"litellm_params": {
|
|
"model": "gpt-5-mini",
|
|
"api_key": "test-key",
|
|
"max_tokens": 100,
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
deployment_before = router.get_deployment_by_model_group_name("gpt-3.5")
|
|
assert deployment_before is not None
|
|
original_params = deployment_before.litellm_params.model_dump()
|
|
|
|
with patch("litellm.completion", new_callable=Mock) as mock_completion:
|
|
from litellm import ModelResponse
|
|
|
|
mock_completion.return_value = ModelResponse(
|
|
id="test",
|
|
choices=[{"message": {"role": "assistant", "content": "test"}, "index": 0}],
|
|
model="gpt-5-mini",
|
|
usage={"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30},
|
|
)
|
|
|
|
try:
|
|
router.completion(
|
|
model="gpt-3.5",
|
|
messages=[{"role": "user", "content": "test"}],
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# Critical: Deployment params must be unchanged
|
|
deployment_after = router.get_deployment_by_model_group_name("gpt-3.5")
|
|
assert deployment_after is not None
|
|
assert deployment_after.litellm_params.model_dump() == original_params
|
|
|
|
|
|
class NoItemsAliasDict(dict):
|
|
def items(self):
|
|
raise AssertionError("Unexpected full alias iteration via items()")
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate_router_unit", "setup_and_teardown_router_unit")
|
|
def test_get_model_list_from_model_alias_should_not_iterate_for_non_alias_lookup():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5-mini",
|
|
"litellm_params": {"model": "gpt-5-mini"},
|
|
}
|
|
],
|
|
model_group_alias={"alias-1": "gpt-5.5"},
|
|
)
|
|
# Assigning model_group_alias copies the value (a changeset detaches it), so the tripwire goes
|
|
# straight onto the core, where the lookup reads it.
|
|
router._core._core_model_group_alias = NoItemsAliasDict({f"alias-{idx}": "gpt-5.5" for idx in range(200)})
|
|
|
|
model_alias_list = router.get_model_list_from_model_alias(model_name="gpt-5-mini")
|
|
assert model_alias_list == []
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate_router_unit", "setup_and_teardown_router_unit")
|
|
def test_map_team_model_should_not_iterate_aliases_for_non_alias_team_model_name():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5-mini",
|
|
"litellm_params": {"model": "gpt-5-mini"},
|
|
"model_info": {
|
|
"team_id": "team-1",
|
|
"team_public_model_name": "team-model",
|
|
},
|
|
}
|
|
],
|
|
model_group_alias={"alias-1": "gpt-5.5"},
|
|
)
|
|
# Assigning model_group_alias copies the value (a changeset detaches it), so the tripwire goes
|
|
# straight onto the core, where the lookup reads it.
|
|
router._core._core_model_group_alias = NoItemsAliasDict({f"alias-{idx}": "gpt-5.5" for idx in range(200)})
|
|
|
|
# map_team_model should return the public name unchanged (not the internal UUID name)
|
|
# so the router can find all sibling deployments via team_id filtering
|
|
result = router.map_team_model(team_model_name="team-model", team_id="team-1")
|
|
assert result == "team-model", f"Expected public name 'team-model', got {result}"
|
|
|
|
|
|
def _team_deployment(
|
|
public_name: str,
|
|
litellm_model: str,
|
|
team_id: str = "team-a",
|
|
deployment_id: str | None = None,
|
|
) -> DeploymentTypedDict:
|
|
return {
|
|
"model_name": f"internal-{team_id}-{public_name}",
|
|
"litellm_params": {"model": litellm_model},
|
|
"model_info": {
|
|
"id": deployment_id or f"{team_id}-{public_name}",
|
|
"team_id": team_id,
|
|
"team_public_model_name": public_name,
|
|
},
|
|
}
|
|
|
|
|
|
_LANGGRAPH_AGENT: Final[DeploymentTypedDict] = _team_deployment("a-langgraph-agent", "langgraph/repro-agent")
|
|
_BEDROCK_CLAUDE: Final[DeploymentTypedDict] = _team_deployment(
|
|
"a-bedrock-claude", "bedrock/anthropic.claude-sonnet-4-6"
|
|
)
|
|
_OPENAI_GPT: Final[DeploymentTypedDict] = _team_deployment("a-gpt", "openai/gpt-4o-mini")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"team_models, custom_llm_provider, expected",
|
|
[
|
|
pytest.param([_LANGGRAPH_AGENT, _BEDROCK_CLAUDE], "bedrock", "a-bedrock-claude", id="match-is-second"),
|
|
pytest.param([_BEDROCK_CLAUDE, _LANGGRAPH_AGENT], "bedrock", "a-bedrock-claude", id="match-is-first"),
|
|
pytest.param([_LANGGRAPH_AGENT, _OPENAI_GPT], "bedrock", None, id="no-match-returns-none"),
|
|
pytest.param([_LANGGRAPH_AGENT, _BEDROCK_CLAUDE], None, "a-langgraph-agent", id="no-provider-keeps-first"),
|
|
pytest.param(
|
|
[_team_deployment("b-bedrock-claude", "bedrock/anthropic.claude-sonnet-4-6", team_id="team-b")],
|
|
"bedrock",
|
|
None,
|
|
id="other-teams-match-ignored",
|
|
),
|
|
pytest.param(
|
|
[
|
|
_team_deployment("shared", "langgraph/repro-agent", deployment_id="shared-langgraph"),
|
|
_team_deployment(
|
|
"shared",
|
|
"bedrock/anthropic.claude-sonnet-4-6",
|
|
deployment_id="shared-bedrock",
|
|
),
|
|
],
|
|
"bedrock",
|
|
"shared-bedrock",
|
|
id="mixed-public-name-langgraph-first",
|
|
),
|
|
pytest.param(
|
|
[
|
|
_team_deployment(
|
|
"shared",
|
|
"bedrock/anthropic.claude-sonnet-4-6",
|
|
deployment_id="shared-bedrock",
|
|
),
|
|
_team_deployment("shared", "langgraph/repro-agent", deployment_id="shared-langgraph"),
|
|
],
|
|
"bedrock",
|
|
"shared-bedrock",
|
|
id="mixed-public-name-bedrock-first",
|
|
),
|
|
pytest.param(
|
|
[
|
|
_team_deployment("shared", "bedrock/anthropic.claude-sonnet-4-6", deployment_id="bedrock-one"),
|
|
_team_deployment("shared", "bedrock/anthropic.claude-sonnet-4-6", deployment_id="bedrock-two"),
|
|
],
|
|
"bedrock",
|
|
"shared",
|
|
id="same-provider-public-name-keeps-load-balancing",
|
|
),
|
|
],
|
|
)
|
|
def test_map_team_model_without_model_picks_deployment_serving_the_requested_provider(
|
|
team_models: list[DeploymentTypedDict], custom_llm_provider: str | None, expected: str | None
|
|
) -> None:
|
|
"""A model-less vector store call routed through the team's first deployment, so a langgraph
|
|
team model made a Bedrock vector store search fail with "not supported for langgraph"."""
|
|
router = Router(model_list=team_models)
|
|
|
|
result = router.map_team_model(None, "team-a", custom_llm_provider=custom_llm_provider)
|
|
|
|
assert result == expected, (
|
|
f"team models {[m['litellm_params']['model'] for m in team_models]}, provider {custom_llm_provider}"
|
|
)
|
|
|
|
|
|
def test_map_team_model_with_explicit_model_ignores_requested_provider() -> None:
|
|
router = Router(model_list=[_LANGGRAPH_AGENT, _BEDROCK_CLAUDE])
|
|
|
|
result = router.map_team_model("a-langgraph-agent", "team-a", custom_llm_provider="bedrock")
|
|
|
|
assert result == "a-langgraph-agent", (
|
|
"an explicit model is the caller's choice and must not be re-picked by provider"
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate_router_unit", "setup_and_teardown_router_unit")
|
|
class TestPreCallChecksOptimization:
|
|
"""
|
|
Verify that using list() instead of deepcopy() doesn't break behavior.
|
|
|
|
If these tests fail, the optimization should be reverted.
|
|
"""
|
|
|
|
def test_no_mutation_of_input_list(self):
|
|
"""
|
|
Verify the input list is never modified by _pre_call_checks.
|
|
|
|
The function uses list() instead of deepcopy for performance.
|
|
This is safe because it only filters items, never modifies them.
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5-mini",
|
|
"litellm_params": {"model": "gpt-5-mini", "api_key": "sk-test"},
|
|
"model_info": {"id": "test-1"},
|
|
},
|
|
{
|
|
"model_name": "gpt-5-mini",
|
|
"litellm_params": {"model": "gpt-5.5", "api_key": "sk-test2"},
|
|
"model_info": {"id": "test-2"},
|
|
},
|
|
],
|
|
set_verbose=False,
|
|
enable_pre_call_checks=True,
|
|
)
|
|
|
|
deployments = router.get_model_list(model_name="gpt-5-mini")
|
|
assert deployments is not None
|
|
|
|
# Capture the original state
|
|
original_length = len(deployments)
|
|
original_deployment_ids = [id(d) for d in deployments]
|
|
original_litellm_params_ids = [id(d["litellm_params"]) for d in deployments]
|
|
snapshot = copy.deepcopy(deployments)
|
|
|
|
# Call the function under test
|
|
router._pre_call_checks(
|
|
model="gpt-5-mini",
|
|
healthy_deployments=deployments,
|
|
messages=[{"role": "user", "content": "test"}],
|
|
)
|
|
|
|
# Verify nothing changed:
|
|
# 1. Same number of items
|
|
assert len(deployments) == original_length, "List length changed!"
|
|
# 2. Same deployment objects (not replaced with copies)
|
|
assert [id(d) for d in deployments] == original_deployment_ids, "Deployment dicts replaced!"
|
|
# 3. Same nested objects (not replaced with copies)
|
|
assert [id(d["litellm_params"]) for d in deployments] == original_litellm_params_ids, "Nested dicts replaced!"
|
|
# 4. Same values (catches any mutation)
|
|
assert deployments == snapshot, "Values were mutated!"
|
|
|
|
def test_filtering_still_works(self):
|
|
"""
|
|
Verify that filtering works correctly while preserving the original list.
|
|
|
|
Scenario: Send a message too long for one deployment but fine for another.
|
|
Expected: Filtered result excludes the small deployment, but original list is unchanged.
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "test",
|
|
"litellm_params": {"model": "gpt-5-mini", "api_key": "sk-test"},
|
|
"model_info": {"id": "small", "max_input_tokens": 50},
|
|
},
|
|
{
|
|
"model_name": "test",
|
|
"litellm_params": {"model": "gpt-5.5", "api_key": "sk-test"},
|
|
"model_info": {"id": "large", "max_input_tokens": 10000},
|
|
},
|
|
],
|
|
set_verbose=False,
|
|
enable_pre_call_checks=True,
|
|
)
|
|
|
|
deployments = router.get_model_list(model_name="test")
|
|
assert deployments is not None
|
|
|
|
# Save references to the original deployment objects
|
|
original_small_deployment = deployments[0] # max_input_tokens=50
|
|
original_large_deployment = deployments[1] # max_input_tokens=10000
|
|
|
|
# Send a long message (100 words) that exceeds 50 tokens but fits in 10000 tokens
|
|
filtered = router._pre_call_checks(
|
|
model="test",
|
|
healthy_deployments=deployments,
|
|
messages=[{"role": "user", "content": " ".join(["word"] * 100)}],
|
|
)
|
|
|
|
# Verify the filtered result only contains the large deployment
|
|
assert len(filtered) == 1, f"Expected 1 deployment after filtering, got {len(filtered)}"
|
|
assert filtered[0]["model_info"]["id"] == "large", "Wrong deployment kept after filtering"
|
|
|
|
# Verify the original list still has both deployments
|
|
assert len(deployments) == 2, f"Original list was modified! Expected 2, got {len(deployments)}"
|
|
assert deployments[0] is original_small_deployment, "First deployment object replaced!"
|
|
assert deployments[1] is original_large_deployment, "Second deployment object replaced!"
|
|
assert deployments[0].get("model_info", {}).get("id") == "small", "First deployment ID changed!"
|
|
assert deployments[1].get("model_info", {}).get("id") == "large", "Second deployment ID changed!"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|
|
|
|
|
|
@pytest.fixture
|
|
def router():
|
|
"""Create a router with a mock deployment"""
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.5",
|
|
"litellm_params": {
|
|
"model": "gpt-5.5",
|
|
"api_key": "fake-key",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate_router_unit", "setup_and_teardown_router_unit")
|
|
@pytest.mark.asyncio
|
|
async def test_router_acancel_batch(router):
|
|
"""Test that router.acancel_batch() calls litellm.acancel_batch with correct params"""
|
|
mock_response = MagicMock()
|
|
mock_response.id = "batch_123"
|
|
mock_response.status = "cancelled"
|
|
|
|
with patch.object(litellm, "acancel_batch", new_callable=AsyncMock) as mock_cancel:
|
|
mock_cancel.return_value = mock_response
|
|
|
|
# This tests that the router method exists and can be called
|
|
# The actual API call is mocked
|
|
response = await router.acancel_batch(
|
|
model="gpt-5.5",
|
|
batch_id="batch_123",
|
|
)
|
|
|
|
# Verify the mock was called
|
|
assert mock_cancel.called
|
|
assert response.id == "batch_123"
|
|
assert response.status == "cancelled"
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate_router_unit", "setup_and_teardown_router_unit")
|
|
@pytest.mark.asyncio
|
|
async def test_router_acancel_batch_resolves_credential_name():
|
|
litellm.credential_list = [
|
|
CredentialItem(
|
|
credential_name="openai-test-credential",
|
|
credential_info={"custom_llm_provider": "openai"},
|
|
credential_values={"api_key": "resolved-openai-key"},
|
|
)
|
|
]
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-5.5",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.5",
|
|
"litellm_credential_name": "openai-test-credential",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
mock_response = MagicMock()
|
|
mock_response.id = "batch_123"
|
|
mock_response.status = "cancelled"
|
|
|
|
try:
|
|
with patch.object(litellm, "acancel_batch", new_callable=AsyncMock) as mock_cancel:
|
|
mock_cancel.return_value = mock_response
|
|
|
|
await router.acancel_batch(
|
|
model="gpt-5.5",
|
|
batch_id="batch_123",
|
|
)
|
|
|
|
call_kwargs = mock_cancel.call_args.kwargs
|
|
assert call_kwargs["api_key"] == "resolved-openai-key"
|
|
assert "litellm_credential_name" not in call_kwargs
|
|
finally:
|
|
litellm.credential_list = []
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate_router_unit", "setup_and_teardown_router_unit")
|
|
class TestRouterEmbeddingHeaders:
|
|
"""Test that embedding methods properly propagate headers from router configuration."""
|
|
|
|
def test_embedding_propagates_default_litellm_params(self):
|
|
"""
|
|
Test that embedding calls properly propagate default_litellm_params including headers.
|
|
|
|
This is the main fix - ensuring that headers set in default_litellm_params
|
|
are included in the embedding request.
|
|
"""
|
|
custom_headers = {"X-Custom-Header": "test-value", "X-API-Version": "v2"}
|
|
|
|
model_list = [
|
|
{
|
|
"model_name": "text-embedding-3-small",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "fake-key",
|
|
},
|
|
}
|
|
]
|
|
|
|
# Create router with default_litellm_params containing headers
|
|
router = Router(
|
|
model_list=model_list,
|
|
default_litellm_params={
|
|
"headers": custom_headers,
|
|
"metadata": {"test_key": "test_value"},
|
|
},
|
|
)
|
|
|
|
with patch("litellm.embedding") as mock_litellm_embedding:
|
|
mock_litellm_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2, 0.3]}])
|
|
|
|
router.embedding(model="text-embedding-3-small", input=["test input"])
|
|
|
|
# Verify that litellm.embedding was called with the headers
|
|
mock_litellm_embedding.assert_called_once()
|
|
call_kwargs = mock_litellm_embedding.call_args[1]
|
|
|
|
# Check that headers were included
|
|
assert "headers" in call_kwargs
|
|
assert call_kwargs["headers"] == custom_headers
|
|
|
|
# Check that metadata was properly set up
|
|
assert "metadata" in call_kwargs
|
|
assert "model_group" in call_kwargs["metadata"]
|
|
assert call_kwargs["metadata"]["model_group"] == "text-embedding-3-small"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_aembedding_propagates_default_litellm_params(self):
|
|
"""
|
|
Test that async embedding calls properly propagate default_litellm_params including headers.
|
|
"""
|
|
custom_headers = {"X-Custom-Header": "test-value", "X-API-Version": "v2"}
|
|
|
|
model_list = [
|
|
{
|
|
"model_name": "text-embedding-3-small",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "fake-key",
|
|
},
|
|
}
|
|
]
|
|
|
|
# Create router with default_litellm_params containing headers
|
|
router = Router(
|
|
model_list=model_list,
|
|
default_litellm_params={
|
|
"headers": custom_headers,
|
|
"metadata": {"test_key": "test_value"},
|
|
},
|
|
)
|
|
|
|
with patch("litellm.aembedding", new_callable=AsyncMock) as mock_litellm_aembedding:
|
|
mock_litellm_aembedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2, 0.3]}])
|
|
|
|
await router.aembedding(model="text-embedding-3-small", input=["test input"])
|
|
|
|
# Verify that litellm.aembedding was called with the headers
|
|
mock_litellm_aembedding.assert_called_once()
|
|
call_kwargs = mock_litellm_aembedding.call_args[1]
|
|
|
|
# Check that headers were included
|
|
assert "headers" in call_kwargs
|
|
assert call_kwargs["headers"] == custom_headers
|
|
|
|
# Check that metadata was properly set up
|
|
assert "metadata" in call_kwargs
|
|
assert "model_group" in call_kwargs["metadata"]
|
|
assert call_kwargs["metadata"]["model_group"] == "text-embedding-3-small"
|
|
|
|
def test_embedding_metadata_includes_model_group(self):
|
|
"""
|
|
Test that embedding calls include model_group in metadata.
|
|
|
|
The _update_kwargs_before_fallbacks method should set this up.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "test-embedding-model",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "fake-key",
|
|
},
|
|
}
|
|
]
|
|
|
|
router = Router(model_list=model_list)
|
|
|
|
with patch("litellm.embedding") as mock_litellm_embedding:
|
|
mock_litellm_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2, 0.3]}])
|
|
|
|
router.embedding(model="test-embedding-model", input=["test input"])
|
|
|
|
call_kwargs = mock_litellm_embedding.call_args[1]
|
|
|
|
# Verify metadata contains model_group
|
|
assert "metadata" in call_kwargs
|
|
assert "model_group" in call_kwargs["metadata"]
|
|
assert call_kwargs["metadata"]["model_group"] == "test-embedding-model"
|
|
|
|
def test_embedding_sets_num_retries_from_router(self):
|
|
"""
|
|
Test that embedding calls inherit num_retries from router configuration.
|
|
|
|
This is set by _update_kwargs_before_fallbacks.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "text-embedding-3-small",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "fake-key",
|
|
},
|
|
}
|
|
]
|
|
|
|
# Create router with num_retries set
|
|
router = Router(model_list=model_list, num_retries=3)
|
|
|
|
with patch("litellm.embedding") as mock_litellm_embedding:
|
|
mock_litellm_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2, 0.3]}])
|
|
|
|
router.embedding(model="text-embedding-3-small", input=["test input"])
|
|
|
|
# Verify num_retries was not set in the call (it's handled by function_with_fallbacks)
|
|
# The important thing is that it was set in kwargs before being passed to function_with_fallbacks
|
|
# We verify this indirectly by checking that _update_kwargs_before_fallbacks was called
|
|
mock_litellm_embedding.assert_called_once()
|
|
|
|
def test_embedding_sets_litellm_trace_id(self):
|
|
"""
|
|
Test that embedding calls include a litellm_trace_id.
|
|
|
|
This is generated and set by _update_kwargs_before_fallbacks.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "text-embedding-3-small",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "fake-key",
|
|
},
|
|
}
|
|
]
|
|
|
|
router = Router(model_list=model_list)
|
|
|
|
with patch("litellm.embedding") as mock_litellm_embedding:
|
|
mock_litellm_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2, 0.3]}])
|
|
|
|
router.embedding(model="text-embedding-3-small", input=["test input"])
|
|
|
|
call_kwargs = mock_litellm_embedding.call_args[1]
|
|
|
|
# Verify litellm_trace_id was set
|
|
assert "litellm_trace_id" in call_kwargs
|
|
assert isinstance(call_kwargs["litellm_trace_id"], str)
|
|
assert len(call_kwargs["litellm_trace_id"]) > 0
|
|
|
|
def test_embedding_consistency_with_completion(self):
|
|
"""
|
|
Test that embedding and completion methods handle kwargs similarly.
|
|
|
|
Both should call _update_kwargs_before_fallbacks to ensure consistent behavior.
|
|
"""
|
|
custom_headers = {"X-Test": "value"}
|
|
|
|
model_list = [
|
|
{
|
|
"model_name": "gpt-5-mini",
|
|
"litellm_params": {
|
|
"model": "gpt-5-mini",
|
|
"api_key": "fake-key",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "text-embedding-3-small",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "fake-key",
|
|
},
|
|
},
|
|
]
|
|
|
|
router = Router(model_list=model_list, default_litellm_params={"headers": custom_headers})
|
|
|
|
# Test completion
|
|
with patch("litellm.completion") as mock_completion:
|
|
mock_completion.return_value = MagicMock()
|
|
|
|
router.completion(model="gpt-5-mini", messages=[{"role": "user", "content": "test"}])
|
|
|
|
completion_kwargs = mock_completion.call_args[1]
|
|
|
|
# Test embedding
|
|
with patch("litellm.embedding") as mock_embedding:
|
|
mock_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2, 0.3]}])
|
|
|
|
router.embedding(model="text-embedding-3-small", input=["test input"])
|
|
|
|
embedding_kwargs = mock_embedding.call_args[1]
|
|
|
|
# Both should have headers from default_litellm_params
|
|
assert "headers" in completion_kwargs
|
|
assert "headers" in embedding_kwargs
|
|
assert completion_kwargs["headers"] == custom_headers
|
|
assert embedding_kwargs["headers"] == custom_headers
|
|
|
|
# Both should have metadata with model_group
|
|
assert "metadata" in completion_kwargs
|
|
assert "metadata" in embedding_kwargs
|
|
assert "model_group" in completion_kwargs["metadata"]
|
|
assert "model_group" in embedding_kwargs["metadata"]
|
|
|
|
# Both should have litellm_trace_id
|
|
assert "litellm_trace_id" in completion_kwargs
|
|
assert "litellm_trace_id" in embedding_kwargs
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Run a simple test
|
|
test = TestRouterEmbeddingHeaders()
|
|
test.test_embedding_calls_update_kwargs_before_fallbacks()
|
|
test.test_embedding_propagates_default_litellm_params()
|
|
test.test_embedding_metadata_includes_model_group()
|
|
test.test_embedding_sets_litellm_trace_id()
|
|
test.test_embedding_consistency_with_completion()
|
|
print("All tests passed!") # noqa: T201
|
|
|
|
QUERY_VECTOR = [0.5, -0.25, 0.125]
|
|
|
|
OPENAI_EMBEDDINGS_URL = "https://api.openai.com/v1/embeddings"
|
|
|
|
STORE_EMBEDDINGS_URL = "https://embedding.example/v1/embeddings"
|
|
|
|
|
|
def _mock_embedding_route(respx_mock: respx.MockRouter, url: str) -> respx.Route:
|
|
return respx_mock.post(url).mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"object": "list",
|
|
"data": [{"object": "embedding", "index": 0, "embedding": QUERY_VECTOR}],
|
|
"model": "text-embedding-3-small",
|
|
"usage": {"prompt_tokens": 2, "total_tokens": 2},
|
|
},
|
|
)
|
|
)
|
|
|
|
|
|
def _sent(route: respx.Route, index: int) -> tuple[str, str, list[str]]:
|
|
request = route.calls[index].request
|
|
body = json.loads(request.read())
|
|
return request.headers["authorization"], body["model"], body["input"]
|
|
|
|
|
|
def _alias_router() -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "team-alias",
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-3-small",
|
|
"api_key": "deployment-key",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("_vcr_outcome_gate_router_unit", "setup_and_teardown_router_unit")
|
|
class TestRouterEmbeddingIntegration:
|
|
"""Integration tests for embedding with router configuration."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_vector_store_embedding_executors_cover_sdk_and_router_paths(
|
|
self, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
|
|
openai_route = _mock_embedding_route(respx_mock, OPENAI_EMBEDDINGS_URL)
|
|
store_route = _mock_embedding_route(respx_mock, STORE_EMBEDDINGS_URL)
|
|
sdk_executor = LiteLLMVectorStoreEmbeddingExecutor()
|
|
|
|
sync_response = sdk_executor.embed("openai/text-embedding-3-small", "sync", {"api_key": "explicit"})
|
|
async_response = await sdk_executor.aembed("openai/text-embedding-3-small", "async", {"api_key": "explicit"})
|
|
|
|
assert sync_response.data[0]["embedding"] == QUERY_VECTOR
|
|
assert async_response.data[0]["embedding"] == QUERY_VECTOR
|
|
assert _sent(openai_route, 0) == ("Bearer explicit", "text-embedding-3-small", ["sync"])
|
|
assert _sent(openai_route, 1) == ("Bearer explicit", "text-embedding-3-small", ["async"])
|
|
|
|
explicit_config = {
|
|
"api_base": "https://embedding.example/v1",
|
|
"api_key": "store-key",
|
|
"metadata": {
|
|
"configured": True,
|
|
"user_api_key_team_id": "untrusted-team",
|
|
},
|
|
"model": "untrusted-model",
|
|
}
|
|
mock_router = MagicMock()
|
|
mock_router.embedding.return_value = sync_response
|
|
router_executor = RouterVectorStoreEmbeddingExecutor(
|
|
router=mock_router,
|
|
metadata={"user_api_key_team_id": "team-a"},
|
|
)
|
|
assert router_executor.embed("team-alias", "query", explicit_config) is sync_response
|
|
mock_router.embedding.assert_called_once_with(
|
|
model="team-alias",
|
|
input=["query"],
|
|
api_base="https://embedding.example/v1",
|
|
api_key="store-key",
|
|
metadata={"configured": True, "user_api_key_team_id": "team-a"},
|
|
)
|
|
|
|
alias_executor = RouterVectorStoreEmbeddingExecutor(
|
|
router=_alias_router(),
|
|
metadata={"user_api_key_team_id": "team-a"},
|
|
)
|
|
sync_alias = alias_executor.embed("team-alias", "sync query", explicit_config)
|
|
async_alias = await alias_executor.aembed("team-alias", "async query", explicit_config)
|
|
|
|
assert sync_alias.data[0]["embedding"] == QUERY_VECTOR
|
|
assert async_alias.data[0]["embedding"] == QUERY_VECTOR
|
|
assert openai_route.call_count == 2
|
|
assert _sent(store_route, 0) == ("Bearer store-key", "text-embedding-3-small", ["sync query"])
|
|
assert _sent(store_route, 1) == ("Bearer store-key", "text-embedding-3-small", ["async query"])
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_executor_falls_back_to_sdk_for_models_the_router_does_not_serve(
|
|
self, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
|
|
store_route = _mock_embedding_route(respx_mock, STORE_EMBEDDINGS_URL)
|
|
executor = RouterVectorStoreEmbeddingExecutor(
|
|
router=_alias_router(),
|
|
metadata={"user_api_key_team_id": "team-a"},
|
|
)
|
|
inline_config = {"api_base": "https://embedding.example/v1", "api_key": "store-key"}
|
|
|
|
sync_response = executor.embed("openai/text-embedding-3-large", "sync query", inline_config)
|
|
async_response = await executor.aembed("openai/text-embedding-3-large", "async query", inline_config)
|
|
|
|
assert sync_response.data[0]["embedding"] == QUERY_VECTOR
|
|
assert async_response.data[0]["embedding"] == QUERY_VECTOR
|
|
assert _sent(store_route, 0) == ("Bearer store-key", "text-embedding-3-large", ["sync query"])
|
|
assert _sent(store_route, 1) == ("Bearer store-key", "text-embedding-3-large", ["async query"])
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_executor_embeds_unserved_models_through_the_sdk(
|
|
self, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
monkeypatch.setenv("OPENAI_API_KEY", "env-key")
|
|
openai_route = _mock_embedding_route(respx_mock, OPENAI_EMBEDDINGS_URL)
|
|
executor = RouterVectorStoreEmbeddingExecutor(
|
|
router=_alias_router(),
|
|
metadata={"user_api_key_team_id": "team-a"},
|
|
)
|
|
|
|
sync_response = executor.embed("text-embedding-3-large", "sync query", {})
|
|
async_response = await executor.aembed("text-embedding-3-large", "async query", {})
|
|
|
|
assert sync_response.data[0]["embedding"] == QUERY_VECTOR
|
|
assert async_response.data[0]["embedding"] == QUERY_VECTOR
|
|
assert _sent(openai_route, 0) == ("Bearer env-key", "text-embedding-3-large", ["sync query"])
|
|
assert _sent(openai_route, 1) == ("Bearer env-key", "text-embedding-3-large", ["async query"])
|
|
|
|
def test_router_executor_routes_deployment_model_names_through_the_router(
|
|
self, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
|
|
openai_route = _mock_embedding_route(respx_mock, OPENAI_EMBEDDINGS_URL)
|
|
executor = RouterVectorStoreEmbeddingExecutor(router=_alias_router(), metadata={})
|
|
|
|
response = executor.embed("openai/text-embedding-3-small", "query", {})
|
|
|
|
assert response.data[0]["embedding"] == QUERY_VECTOR
|
|
assert _sent(openai_route, 0) == ("Bearer deployment-key", "text-embedding-3-small", ["query"])
|
|
|
|
def test_embedding_with_deployment_specific_headers(self):
|
|
"""
|
|
Test that deployment-specific headers are propagated.
|
|
|
|
This simulates a scenario where different deployments have
|
|
different header requirements (e.g., different API versions).
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "embedding-deployment-1",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "key-1",
|
|
"headers": {"X-Deployment": "deployment-1"},
|
|
},
|
|
},
|
|
{
|
|
"model_name": "embedding-deployment-2",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "key-2",
|
|
"headers": {"X-Deployment": "deployment-2"},
|
|
},
|
|
},
|
|
]
|
|
|
|
router = Router(model_list=model_list)
|
|
|
|
# Test first deployment
|
|
with patch("litellm.embedding") as mock_embedding:
|
|
mock_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2]}])
|
|
|
|
router.embedding(model="embedding-deployment-1", input=["test"])
|
|
|
|
call_kwargs = mock_embedding.call_args[1]
|
|
assert call_kwargs["api_key"] == "key-1"
|
|
|
|
# Test second deployment
|
|
with patch("litellm.embedding") as mock_embedding:
|
|
mock_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2]}])
|
|
|
|
router.embedding(model="embedding-deployment-2", input=["test"])
|
|
|
|
call_kwargs = mock_embedding.call_args[1]
|
|
assert call_kwargs["api_key"] == "key-2"
|
|
|
|
def test_embedding_with_router_and_deployment_headers_merge(self):
|
|
"""
|
|
Test that router-level headers are propagated.
|
|
|
|
When no request headers are provided, router default headers should be used.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "test-embedding",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "test-key",
|
|
},
|
|
}
|
|
]
|
|
|
|
router = Router(
|
|
model_list=model_list,
|
|
default_litellm_params={
|
|
"headers": {
|
|
"X-Router-Header": "router-value",
|
|
"X-Common-Header": "router-common",
|
|
}
|
|
},
|
|
)
|
|
|
|
# Test: No request headers - router headers should be used
|
|
with patch("litellm.embedding") as mock_embedding:
|
|
mock_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2]}])
|
|
|
|
router.embedding(
|
|
model="test-embedding",
|
|
input=["test"],
|
|
)
|
|
|
|
call_kwargs = mock_embedding.call_args[1]
|
|
|
|
# Router headers should be present
|
|
assert "headers" in call_kwargs
|
|
assert call_kwargs["headers"]["X-Router-Header"] == "router-value"
|
|
assert call_kwargs["headers"]["X-Common-Header"] == "router-common"
|
|
|
|
def test_embedding_metadata_propagation(self):
|
|
"""
|
|
Test that metadata is properly set up and propagated.
|
|
|
|
This is important for logging, tracking, and debugging.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "test-embedding",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "test-key",
|
|
},
|
|
}
|
|
]
|
|
|
|
router = Router(
|
|
model_list=model_list,
|
|
default_litellm_params={"metadata": {"environment": "test", "service": "embedding-service"}},
|
|
)
|
|
|
|
with patch("litellm.embedding") as mock_embedding:
|
|
mock_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2]}])
|
|
|
|
router.embedding(
|
|
model="test-embedding",
|
|
input=["test"],
|
|
metadata={"request_id": "req-123"}, # Additional metadata from request
|
|
)
|
|
|
|
call_kwargs = mock_embedding.call_args[1]
|
|
|
|
# Check metadata contains all expected fields
|
|
assert "metadata" in call_kwargs
|
|
metadata = call_kwargs["metadata"]
|
|
|
|
# From _update_kwargs_before_fallbacks
|
|
assert "model_group" in metadata
|
|
assert metadata["model_group"] == "test-embedding"
|
|
|
|
# From default_litellm_params
|
|
assert "environment" in metadata
|
|
assert metadata["environment"] == "test"
|
|
assert "service" in metadata
|
|
assert metadata["service"] == "embedding-service"
|
|
|
|
# From request
|
|
assert "request_id" in metadata
|
|
assert metadata["request_id"] == "req-123"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_embedding_with_multiple_retries(self):
|
|
"""
|
|
Test that async embedding properly uses num_retries from router config.
|
|
|
|
This ensures the fix works with the retry mechanism.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "test-embedding",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "test-key",
|
|
},
|
|
}
|
|
]
|
|
|
|
router = Router(model_list=model_list, num_retries=2)
|
|
|
|
with patch("litellm.aembedding", new_callable=AsyncMock) as mock_aembedding:
|
|
mock_aembedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2]}])
|
|
|
|
await router.aembedding(model="test-embedding", input=["test"])
|
|
|
|
# The call should succeed
|
|
mock_aembedding.assert_called_once()
|
|
|
|
def test_embedding_with_timeout_from_router(self):
|
|
"""
|
|
Test that timeout settings from router config are propagated.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "test-embedding",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "test-key",
|
|
},
|
|
}
|
|
]
|
|
|
|
router = Router(model_list=model_list, timeout=30.0)
|
|
|
|
with patch("litellm.embedding") as mock_embedding:
|
|
mock_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2]}])
|
|
|
|
router.embedding(model="test-embedding", input=["test"])
|
|
|
|
call_kwargs = mock_embedding.call_args[1]
|
|
|
|
# Timeout should be set from router config
|
|
assert "timeout" in call_kwargs
|
|
assert call_kwargs["timeout"] == 30.0
|
|
|
|
def test_embedding_with_multiple_deployments_load_balancing(self):
|
|
"""
|
|
Test that headers are correctly propagated when router load balances
|
|
between multiple deployments.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "shared-embedding-model",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "key-1",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "shared-embedding-model",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "key-2",
|
|
},
|
|
},
|
|
]
|
|
|
|
router = Router(
|
|
model_list=model_list,
|
|
default_litellm_params={"headers": {"X-Shared-Header": "shared-value"}},
|
|
)
|
|
|
|
# Make multiple calls and verify headers are always present
|
|
for i in range(5):
|
|
with patch("litellm.embedding") as mock_embedding:
|
|
mock_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2]}])
|
|
|
|
router.embedding(model="shared-embedding-model", input=[f"test {i}"])
|
|
|
|
call_kwargs = mock_embedding.call_args[1]
|
|
|
|
# Headers should always be present regardless of which deployment is chosen
|
|
assert "headers" in call_kwargs
|
|
assert call_kwargs["headers"]["X-Shared-Header"] == "shared-value"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_embedding_with_fallback_configuration(self):
|
|
"""
|
|
Test that headers are propagated correctly when using fallback models.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "primary-embedding",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "primary-key",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "fallback-embedding",
|
|
"litellm_params": {
|
|
"model": "text-embedding-3-small",
|
|
"api_key": "fallback-key",
|
|
},
|
|
},
|
|
]
|
|
|
|
router = Router(
|
|
model_list=model_list,
|
|
fallbacks=[{"primary-embedding": ["fallback-embedding"]}],
|
|
default_litellm_params={"headers": {"X-Fallback-Test": "test-value"}},
|
|
)
|
|
|
|
# Simulate primary failing, fallback succeeding
|
|
with patch("litellm.aembedding", new_callable=AsyncMock) as mock_aembedding:
|
|
call_count = 0
|
|
|
|
async def side_effect(*args, **kwargs):
|
|
nonlocal call_count
|
|
call_count += 1
|
|
if call_count == 1:
|
|
# First call (primary) fails
|
|
raise Exception("Primary failed")
|
|
else:
|
|
# Second call (fallback) succeeds
|
|
return MagicMock(data=[{"embedding": [0.1, 0.2]}])
|
|
|
|
mock_aembedding.side_effect = side_effect
|
|
|
|
await router.aembedding(model="primary-embedding", input=["test"])
|
|
|
|
# Both calls should have headers
|
|
assert mock_aembedding.call_count == 2
|
|
|
|
# Check that both calls had headers
|
|
for call_obj in mock_aembedding.call_args_list:
|
|
call_kwargs = call_obj[1]
|
|
assert "headers" in call_kwargs
|
|
assert call_kwargs["headers"]["X-Fallback-Test"] == "test-value"
|
|
|
|
def test_embedding_with_custom_provider_headers(self):
|
|
"""
|
|
Test that provider-specific headers are correctly propagated.
|
|
|
|
Some providers require specific headers for API versioning, features, etc.
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "azure-embedding",
|
|
"litellm_params": {
|
|
"model": "azure/text-embedding-3-small",
|
|
"api_key": "azure-key",
|
|
"api_base": "https://example.openai.azure.com",
|
|
"api_version": "2024-02-01",
|
|
},
|
|
}
|
|
]
|
|
|
|
router = Router(
|
|
model_list=model_list,
|
|
default_litellm_params={"headers": {"X-Custom-Azure-Header": "azure-value"}},
|
|
)
|
|
|
|
with patch("litellm.embedding") as mock_embedding:
|
|
mock_embedding.return_value = MagicMock(data=[{"embedding": [0.1, 0.2]}])
|
|
|
|
router.embedding(model="azure-embedding", input=["test"])
|
|
|
|
call_kwargs = mock_embedding.call_args[1]
|
|
|
|
# Verify Azure-specific params are present
|
|
assert call_kwargs["api_base"] == "https://example.openai.azure.com"
|
|
assert call_kwargs["api_version"] == "2024-02-01"
|
|
|
|
# Verify custom headers are present
|
|
assert "headers" in call_kwargs
|
|
assert call_kwargs["headers"]["X-Custom-Azure-Header"] == "azure-value"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Run tests
|
|
pytest.main([__file__, "-v"])
|
|
|
|
|
|
_PRIORITY_CHAT_COMPLETION: Final = {
|
|
"id": "chatcmpl-priority",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-5.4-nano",
|
|
"choices": [{"index": 0, "finish_reason": "stop", "message": {"role": "assistant", "content": "pong"}}],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
}
|
|
_PRIORITY_MESSAGES: Final = [{"role": "user", "content": "Reply with the single word pong"}]
|
|
|
|
|
|
def _priority_router(default_priority: int | None) -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "qa-chat",
|
|
"litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "sk-priority-wire"},
|
|
}
|
|
],
|
|
default_priority=default_priority,
|
|
)
|
|
|
|
|
|
def _priority_provider(respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch) -> respx.Route:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
return respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=httpx.Response(200, json=_PRIORITY_CHAT_COMPLETION)
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("default_priority", "request_kwargs", "queued_priority"),
|
|
[
|
|
(3, {}, 3),
|
|
(3, {"priority": None}, 3),
|
|
(3, {"priority": 2}, 2),
|
|
(None, {"priority": 2}, 2),
|
|
(None, {"priority": 0}, 0),
|
|
(None, {"priority": "1"}, 1),
|
|
(None, {"priority": True}, 1),
|
|
(3, {"priority": 1.0}, 1),
|
|
(3, {"priority": "high", "drop_params": True}, 3),
|
|
],
|
|
)
|
|
async def test_acompletion_queues_the_resolved_priority_exactly_once(
|
|
default_priority: int | None, request_kwargs: dict[str, object], queued_priority: int
|
|
) -> None:
|
|
router: Final = _priority_router(default_priority)
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.acompletion(
|
|
model="qa-chat", messages=_PRIORITY_MESSAGES, mock_response="pong", **request_kwargs
|
|
)
|
|
assert add_request.await_count == 1
|
|
assert add_request.await_args.kwargs["request"].priority == queued_priority
|
|
assert response._hidden_params["additional_headers"]["x-litellm-request-prioritization-used"] is True
|
|
|
|
|
|
async def test_acompletion_without_any_priority_never_enters_the_scheduler() -> None:
|
|
router: Final = _priority_router(None)
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.acompletion(model="qa-chat", messages=_PRIORITY_MESSAGES, mock_response="pong")
|
|
assert add_request.await_count == 0
|
|
assert "x-litellm-request-prioritization-used" not in response._hidden_params.get("additional_headers", {})
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("default_priority", "request_kwargs"), [(3, {}), (None, {"priority": 3})], ids=["default", "explicit"]
|
|
)
|
|
async def test_a_request_pinned_to_a_deployment_id_is_served_while_an_earlier_one_waits_in_the_queue(
|
|
default_priority: int | None, request_kwargs: dict[str, object]
|
|
) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "qa-chat",
|
|
"litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "sk-priority-wire"},
|
|
"model_info": {"id": "qa-chat-deployment"},
|
|
}
|
|
],
|
|
default_priority=default_priority,
|
|
)
|
|
await router.scheduler.add_request(FlowItem(priority=0, request_id="ahead", model_name="qa-chat-deployment"))
|
|
response: Final = await asyncio.wait_for(
|
|
router.acompletion(
|
|
model="qa-chat-deployment", messages=_PRIORITY_MESSAGES, mock_response="pong", **request_kwargs
|
|
),
|
|
timeout=5,
|
|
)
|
|
assert response.choices[0].message.content == "pong"
|
|
assert response._hidden_params["additional_headers"]["x-litellm-request-prioritization-used"] is True
|
|
|
|
|
|
@pytest.mark.parametrize("priority", ["high", [1], 1.5])
|
|
async def test_acompletion_rejects_a_non_integer_priority_before_the_provider_sees_it(
|
|
priority: object, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
provider: Final = _priority_provider(respx_mock, monkeypatch)
|
|
router: Final = _priority_router(3)
|
|
with pytest.raises(litellm.BadRequestError, match="priority must be an integer") as raised:
|
|
await router.acompletion(model="qa-chat", messages=_PRIORITY_MESSAGES, priority=priority)
|
|
assert raised.value.param == "priority"
|
|
assert not provider.called
|
|
|
|
|
|
async def test_acompletion_drops_a_non_integer_priority_from_the_provider_call_under_drop_params(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
provider: Final = _priority_provider(respx_mock, monkeypatch)
|
|
router: Final = _priority_router(None)
|
|
await router.acompletion(model="qa-chat", messages=_PRIORITY_MESSAGES, priority="high", drop_params=True)
|
|
assert "priority" not in json.loads(provider.calls.last.request.content)
|
|
|
|
|
|
@pytest.mark.parametrize("priority", [0, "1"])
|
|
async def test_acompletion_with_an_integer_priority_sends_no_priority_to_the_provider(
|
|
priority: object, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
provider: Final = _priority_provider(respx_mock, monkeypatch)
|
|
router: Final = _priority_router(None)
|
|
await router.acompletion(model="qa-chat", messages=_PRIORITY_MESSAGES, priority=priority)
|
|
assert "priority" not in json.loads(provider.calls.last.request.content)
|
|
|
|
|
|
async def test_schedule_acompletion_under_a_default_priority_queues_the_explicit_priority_once() -> None:
|
|
router: Final = _priority_router(3)
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.schedule_acompletion(
|
|
model="qa-chat", messages=_PRIORITY_MESSAGES, priority=1, mock_response="pong"
|
|
)
|
|
assert add_request.await_count == 1
|
|
assert add_request.await_args.kwargs["request"].priority == 1
|
|
assert response._hidden_params["additional_headers"]["x-litellm-request-prioritization-used"] is True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("deployment_params", "router_defaults"),
|
|
[
|
|
({}, {"drop_params": True}),
|
|
({"drop_params": True}, {}),
|
|
({"drop_params": "true"}, {}),
|
|
({"additional_drop_params": ["priority"]}, {}),
|
|
({}, {"additional_drop_params": ["priority"]}),
|
|
],
|
|
)
|
|
async def test_acompletion_drops_a_non_integer_priority_under_the_router_or_deployment_drop_params(
|
|
deployment_params: dict[str, object], router_defaults: dict[str, object]
|
|
) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "qa-chat",
|
|
"litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "sk-priority-wire", **deployment_params},
|
|
}
|
|
],
|
|
default_priority=3,
|
|
default_litellm_params=router_defaults,
|
|
)
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.acompletion(
|
|
model="qa-chat", messages=_PRIORITY_MESSAGES, mock_response="pong", priority="high"
|
|
)
|
|
assert add_request.await_args.kwargs["request"].priority == 3
|
|
assert response._hidden_params["additional_headers"]["x-litellm-request-prioritization-used"] is True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("deployment", "requested_model", "routing_fields"),
|
|
[
|
|
(
|
|
{
|
|
"model_name": "model_name_team-a_team-a-chat",
|
|
"litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "sk-priority-wire", "drop_params": True},
|
|
"model_info": {"id": "team-a-chat", "team_id": "team-a", "team_public_model_name": "team-chat"},
|
|
},
|
|
"team-chat",
|
|
{"metadata": {"user_api_key_team_id": "team-a"}},
|
|
),
|
|
(
|
|
{
|
|
"model_name": "qa-chat",
|
|
"litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "sk-priority-wire", "drop_params": True},
|
|
"model_info": {"id": "qa-chat-deployment"},
|
|
},
|
|
"qa-chat-deployment",
|
|
{},
|
|
),
|
|
(
|
|
{
|
|
"model_name": "qa-chat",
|
|
"litellm_params": {"model": "openai/gpt-5.4-nano", "api_key": "sk-priority-wire", "drop_params": True},
|
|
},
|
|
"openai/gpt-5.4-nano",
|
|
{"specific_deployment": True},
|
|
),
|
|
],
|
|
ids=["team-route", "deployment-id", "provider-model-name"],
|
|
)
|
|
async def test_deployment_drop_params_drop_a_non_integer_priority_on_every_deployment_route(
|
|
deployment: dict[str, object],
|
|
requested_model: str,
|
|
routing_fields: dict[str, object],
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
router: Final = Router(model_list=[deployment], default_priority=3)
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.acompletion(
|
|
model=requested_model,
|
|
messages=_PRIORITY_MESSAGES,
|
|
mock_response="pong",
|
|
priority="high",
|
|
**routing_fields,
|
|
)
|
|
assert add_request.await_args.kwargs["request"].priority == 3
|
|
assert response._hidden_params["additional_headers"]["x-litellm-request-prioritization-used"] is True
|
|
|
|
|
|
async def test_schedule_acompletion_queues_a_prompt_management_model(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "cached-chat",
|
|
"litellm_params": {
|
|
"model": "anthropic_cache_control_hook/openai/gpt-5.4-nano",
|
|
"api_key": "sk-priority-wire",
|
|
"prompt_id": "cache-points",
|
|
},
|
|
}
|
|
]
|
|
)
|
|
provider: Final = AsyncMock(return_value=litellm.ModelResponse())
|
|
monkeypatch.setattr(litellm, "acompletion", provider)
|
|
logging_obj: Final = LiteLLMLogging(
|
|
model="cached-chat",
|
|
messages=_PRIORITY_MESSAGES,
|
|
stream=False,
|
|
call_type="acompletion",
|
|
start_time=time.time(),
|
|
litellm_call_id="prompt-management-priority",
|
|
function_id="f",
|
|
)
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.schedule_acompletion(
|
|
model="cached-chat", messages=_PRIORITY_MESSAGES, priority=1, litellm_logging_obj=logging_obj
|
|
)
|
|
assert add_request.await_count == 1
|
|
assert add_request.await_args.kwargs["request"].priority == 1
|
|
assert provider.await_count == 1
|
|
assert "priority" not in provider.await_args.kwargs
|
|
assert response._hidden_params["additional_headers"]["x-litellm-request-prioritization-used"] is True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("configured", "expected"),
|
|
[(3, 3), ("3", 3), ("-1", -1), (0, 0), (None, None)],
|
|
ids=["int", "digit-string", "negative-string", "zero", "unset"],
|
|
)
|
|
def test_the_router_parses_default_priority_once_at_init(configured: object, expected: int | None) -> None:
|
|
assert _priority_router(configured).default_priority == expected
|
|
|
|
|
|
@pytest.mark.parametrize("configured", ["high", 1.5, [3], False], ids=["word", "float", "list", "yaml-off"])
|
|
def test_an_invalid_default_priority_is_ignored_with_a_warning(
|
|
configured: object, caplog: pytest.LogCaptureFixture
|
|
) -> None:
|
|
with caplog.at_level(logging.WARNING, logger="LiteLLM Router"):
|
|
router: Final = _priority_router(configured)
|
|
assert router.default_priority is None
|
|
assert any("default_priority is ignored" in record.getMessage() for record in caplog.records), caplog.records
|
|
|
|
|
|
async def test_an_invalid_default_priority_leaves_every_request_unscheduled() -> None:
|
|
router: Final = _priority_router("high")
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.acompletion(model="qa-chat", messages=_PRIORITY_MESSAGES, mock_response="pong")
|
|
assert add_request.await_count == 0
|
|
assert "x-litellm-request-prioritization-used" not in response._hidden_params.get("additional_headers", {})
|
|
|
|
|
|
async def test_schedule_acompletion_without_a_priority_or_a_default_is_rejected_before_the_provider(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
provider: Final = _priority_provider(respx_mock, monkeypatch)
|
|
router: Final = _priority_router(None)
|
|
with pytest.raises(litellm.BadRequestError, match="priority is required") as raised:
|
|
await router.schedule_acompletion(model="qa-chat", messages=_PRIORITY_MESSAGES)
|
|
assert raised.value.param == "priority"
|
|
assert not provider.called
|
|
|
|
|
|
async def test_schedule_acompletion_without_a_priority_queues_the_default_once() -> None:
|
|
router: Final = _priority_router(3)
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.schedule_acompletion(
|
|
model="qa-chat", messages=_PRIORITY_MESSAGES, mock_response="pong"
|
|
)
|
|
assert add_request.await_count == 1
|
|
assert add_request.await_args.kwargs["request"].priority == 3
|
|
assert response._hidden_params["additional_headers"]["x-litellm-request-prioritization-used"] is True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("default_priority", "request_kwargs", "queued_priority"),
|
|
[
|
|
(3, {}, 3),
|
|
(3, {"priority": None}, 3),
|
|
(3, {"priority": 2}, 2),
|
|
(None, {"priority": 0}, 0),
|
|
(None, {"priority": "1"}, 1),
|
|
(3, {"priority": "high", "drop_params": True}, 3),
|
|
],
|
|
)
|
|
async def test_atext_completion_queues_the_resolved_priority_exactly_once(
|
|
default_priority: int | None,
|
|
request_kwargs: dict[str, object],
|
|
queued_priority: int,
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
provider: Final = _priority_provider(respx_mock, monkeypatch)
|
|
router: Final = _priority_router(default_priority)
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.atext_completion(model="qa-chat", prompt="pong?", **request_kwargs)
|
|
assert add_request.await_count == 1
|
|
assert add_request.await_args.kwargs["request"].priority == queued_priority
|
|
assert response._hidden_params["additional_headers"]["x-litellm-request-prioritization-used"] is True
|
|
assert "priority" not in json.loads(provider.calls.last.request.content)
|
|
|
|
|
|
async def test_atext_completion_without_any_priority_never_enters_the_scheduler(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
provider: Final = _priority_provider(respx_mock, monkeypatch)
|
|
router: Final = _priority_router(None)
|
|
with patch.object(router.scheduler, "add_request", wraps=router.scheduler.add_request) as add_request:
|
|
response: Final = await router.atext_completion(model="qa-chat", prompt="pong?")
|
|
assert add_request.await_count == 0
|
|
assert "x-litellm-request-prioritization-used" not in response._hidden_params.get("additional_headers", {})
|
|
assert provider.called
|
|
|
|
|
|
@pytest.mark.parametrize("priority", ["high", [1], 1.5])
|
|
async def test_atext_completion_rejects_a_non_integer_priority_before_the_provider_sees_it(
|
|
priority: object, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
provider: Final = _priority_provider(respx_mock, monkeypatch)
|
|
router: Final = _priority_router(3)
|
|
with pytest.raises(litellm.BadRequestError, match="priority must be an integer") as raised:
|
|
await router.atext_completion(model="qa-chat", prompt="pong?", priority=priority)
|
|
assert raised.value.param == "priority"
|
|
assert not provider.called
|
|
|
|
|
|
def test_router_multi_org_list() -> None:
|
|
"""
|
|
Pass list of orgs in 1 model definition,
|
|
expect a unique deployment for each to be created
|
|
"""
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "*",
|
|
"litellm_params": {
|
|
"model": "openai/*",
|
|
"api_key": "my-key",
|
|
"api_base": "https://api.openai.com/v1",
|
|
"organization": ["org-1", "org-2", "org-3"],
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
assert len(router.get_model_list()) == 3
|
|
|
|
|
|
def test_router_specific_model_via_id():
|
|
"""
|
|
Call a specific deployment by it's id
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"api_key": "my-fake-key",
|
|
"mock_response": "Hello world",
|
|
},
|
|
"model_info": {"id": "1234"},
|
|
}
|
|
]
|
|
)
|
|
|
|
router.completion(model="1234", messages=[{"role": "user", "content": "Hey!"}])
|
|
assert (
|
|
router.completion(model="1234", messages=[{"role": "user", "content": "Hey!"}]).choices[0].message.content
|
|
== "Hello world"
|
|
)
|
|
|
|
|
|
def test_router_order() -> None:
|
|
"""
|
|
Asserts for 2 models in a model group, model with order=1 always called first
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-4o",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
"mock_response": "Hello world",
|
|
"order": 1,
|
|
},
|
|
"model_info": {"id": "1"},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-4o",
|
|
"api_key": "bad-key",
|
|
"mock_response": Exception("this is a bad key"),
|
|
"order": 2,
|
|
},
|
|
"model_info": {"id": "2"},
|
|
},
|
|
],
|
|
num_retries=0,
|
|
allowed_fails=0,
|
|
enable_pre_call_checks=True,
|
|
)
|
|
|
|
for _ in range(100):
|
|
response = router.completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
)
|
|
|
|
assert isinstance(response, litellm.ModelResponse)
|
|
assert response._hidden_params["model_id"] == "1"
|
|
|
|
|
|
@pytest.mark.usefixtures("local_model_cost_map", "isolate_router_model_cost_state")
|
|
def test_router_context_window_check_pre_call_check_in_group_custom_model_info():
|
|
"""
|
|
- Give a gpt-3.5-turbo model group with different context windows (4k vs. 16k)
|
|
- Send a 5k prompt
|
|
- Assert it works
|
|
"""
|
|
import os
|
|
|
|
litellm.set_verbose = False
|
|
|
|
print(f"len(text): {len(text)}")
|
|
try:
|
|
model_list = [
|
|
{
|
|
"model_name": "gpt-3.5-turbo", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_key": os.getenv("AZURE_AI_API_KEY"),
|
|
"api_version": os.getenv("AZURE_API_VERSION"),
|
|
"api_base": os.getenv("AZURE_AI_API_BASE"),
|
|
"base_model": "azure/gpt-35-turbo",
|
|
"mock_response": "Hello world 1!",
|
|
},
|
|
"model_info": {"max_input_tokens": 100},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "gpt-3.5-turbo-1106",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
"mock_response": "Hello world 2!",
|
|
},
|
|
"model_info": {"max_input_tokens": 0},
|
|
},
|
|
]
|
|
|
|
router = Router(model_list=model_list, set_verbose=True, enable_pre_call_checks=True, num_retries=0) # type: ignore
|
|
|
|
response = router.completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[
|
|
{"role": "user", "content": "Who was Alexander?"},
|
|
],
|
|
)
|
|
|
|
print(f"response: {response}")
|
|
|
|
assert response.choices[0].message.content == "Hello world 1!"
|
|
except Exception as e:
|
|
pytest.fail(f"Got unexpected exception on router! - {str(e)}")
|
|
|
|
|
|
@pytest.mark.usefixtures("local_model_cost_map", "isolate_router_model_cost_state")
|
|
def test_router_context_window_check_pre_call_check():
|
|
"""
|
|
- Give a gpt-3.5-turbo model group with different context windows (4k vs. 16k)
|
|
- Send a 5k prompt
|
|
- Assert it works
|
|
"""
|
|
import os
|
|
|
|
litellm.set_verbose = False
|
|
|
|
print(f"len(text): {len(text)}")
|
|
try:
|
|
model_list = [
|
|
{
|
|
"model_name": "gpt-3.5-turbo", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_key": os.getenv("AZURE_AI_API_KEY"),
|
|
"api_version": os.getenv("AZURE_API_VERSION"),
|
|
"api_base": os.getenv("AZURE_AI_API_BASE"),
|
|
"base_model": "azure/gpt-35-turbo",
|
|
"mock_response": "Hello world 1!",
|
|
},
|
|
"model_info": {"base_model": "azure/gpt-35-turbo"},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "gpt-3.5-turbo-1106",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
"mock_response": "Hello world 2!",
|
|
},
|
|
},
|
|
]
|
|
|
|
router = Router(model_list=model_list, set_verbose=True, enable_pre_call_checks=True, num_retries=0) # type: ignore
|
|
|
|
response = router.completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[
|
|
{"role": "system", "content": text},
|
|
{"role": "user", "content": "Who was Alexander?"},
|
|
],
|
|
)
|
|
|
|
print(f"response: {response}")
|
|
|
|
assert response.choices[0].message.content == "Hello world 2!"
|
|
except Exception as e:
|
|
pytest.fail(f"Got unexpected exception on router! - {str(e)}")
|
|
|
|
|
|
@pytest.mark.usefixtures("local_model_cost_map", "isolate_router_model_cost_state")
|
|
def test_router_context_window_check_pre_call_check_out_group():
|
|
"""
|
|
- Give 2 gpt-3.5-turbo model groups with different context windows (4k vs. 16k)
|
|
- Send a 5k prompt
|
|
- Assert it works
|
|
"""
|
|
import os
|
|
|
|
litellm.set_verbose = False
|
|
|
|
print(f"len(text): {len(text)}")
|
|
try:
|
|
model_list = [
|
|
{
|
|
"model_name": "gpt-3.5-turbo-small", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_key": os.getenv("AZURE_AI_API_KEY"),
|
|
"api_version": os.getenv("AZURE_API_VERSION"),
|
|
"api_base": os.getenv("AZURE_AI_API_BASE"),
|
|
"base_model": "azure/gpt-35-turbo",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo-large", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "gpt-4.1-mini",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
"mock_response": "Alexander was a great conqueror.",
|
|
},
|
|
},
|
|
]
|
|
|
|
router = Router(
|
|
model_list=model_list,
|
|
set_verbose=True,
|
|
enable_pre_call_checks=True,
|
|
num_retries=0,
|
|
context_window_fallbacks=[{"gpt-3.5-turbo-small": ["gpt-3.5-turbo-large"]}],
|
|
) # type: ignore
|
|
|
|
response = router.completion(
|
|
model="gpt-3.5-turbo-small",
|
|
messages=[
|
|
{"role": "system", "content": text},
|
|
{"role": "user", "content": "Who was Alexander?"},
|
|
],
|
|
)
|
|
|
|
print(f"response: {response}")
|
|
assert response.choices[0].message.content == "Alexander was a great conqueror."
|
|
except Exception as e:
|
|
pytest.fail(f"Got unexpected exception on router! - {str(e)}")
|
|
|
|
|
|
def test_filter_invalid_params_pre_call_check():
|
|
"""
|
|
- gpt-3.5-turbo supports 'response_object'
|
|
- gpt-3.5-turbo-16k doesn't support 'response_object'
|
|
|
|
run pre-call check -> assert returned list doesn't include gpt-3.5-turbo-16k
|
|
"""
|
|
try:
|
|
model_list = [
|
|
{
|
|
"model_name": "gpt-3.5-turbo", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "gpt-3.5-turbo",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo-16k",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
},
|
|
},
|
|
]
|
|
|
|
router = Router(model_list=model_list, set_verbose=True, enable_pre_call_checks=True, num_retries=0) # type: ignore
|
|
|
|
filtered_deployments = router._pre_call_checks(
|
|
model="gpt-3.5-turbo",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
request_kwargs={"response_format": {"type": "json_object"}},
|
|
)
|
|
assert len(filtered_deployments) == 1
|
|
except Exception as e:
|
|
pytest.fail(f"Got unexpected exception on router! - {str(e)}")
|
|
|
|
|
|
@pytest.mark.parametrize("allowed_model_region", ["eu", None, "us"])
|
|
def test_router_region_pre_call_check(allowed_model_region):
|
|
"""
|
|
If region based routing set
|
|
- check if only model in allowed region is allowed by '_pre_call_checks'
|
|
"""
|
|
model_list = [
|
|
{
|
|
"model_name": "gpt-3.5-turbo", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_key": os.getenv("AZURE_AI_API_KEY"),
|
|
"api_version": os.getenv("AZURE_API_VERSION"),
|
|
"api_base": os.getenv("AZURE_AI_API_BASE"),
|
|
"base_model": "azure/gpt-35-turbo",
|
|
"region_name": allowed_model_region,
|
|
},
|
|
"model_info": {"id": "1"},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo-large", # openai model name
|
|
"litellm_params": { # params for litellm completion/embedding call
|
|
"model": "gpt-4.1-mini",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
"mock_response": "This is a mock response.",
|
|
},
|
|
"model_info": {"id": "2"},
|
|
},
|
|
]
|
|
|
|
router = Router(model_list=model_list, enable_pre_call_checks=True)
|
|
|
|
_healthy_deployments = router._pre_call_checks(
|
|
model="gpt-3.5-turbo",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "Hey!"}],
|
|
request_kwargs={"allowed_model_region": allowed_model_region},
|
|
)
|
|
|
|
if allowed_model_region is None:
|
|
assert len(_healthy_deployments) == 2
|
|
else:
|
|
assert len(_healthy_deployments) == 1, "{} models selected as healthy".format(len(_healthy_deployments))
|
|
assert _healthy_deployments[0]["model_info"]["id"] == "1", (
|
|
"Incorrect model id picked. Got id={}, expected id=1".format(_healthy_deployments[0]["model_info"]["id"])
|
|
)
|
|
|
|
|
|
def test_model_group_info() -> None:
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "nova-2-lite",
|
|
"litellm_params": {"model": "bedrock/amazon.nova-2-lite-v1:0"},
|
|
}
|
|
]
|
|
)
|
|
|
|
response = router.get_model_group_info(model_group="nova-2-lite")
|
|
|
|
assert response is not None
|
|
assert response.model_group == "nova-2-lite"
|
|
assert response.providers == ["bedrock"]
|
|
assert response.max_input_tokens is not None
|
|
|
|
|
|
def test_consistent_model_id() -> None:
|
|
"""
|
|
- For a given model group + litellm params, assert the model id is always the same
|
|
|
|
Test on `generate_model_id`
|
|
|
|
Test on `set_model_list`
|
|
|
|
Test on `_add_deployment`
|
|
"""
|
|
model_group = "gpt-3.5-turbo"
|
|
litellm_params = {
|
|
"model": "openai/my-fake-model",
|
|
"api_key": "my-fake-key",
|
|
"api_base": "https://openai-function-calling-workers.tasslexyz.workers.dev/",
|
|
"stream_timeout": 0.001,
|
|
}
|
|
|
|
id1 = Router().generate_model_id(model_group=model_group, litellm_params=litellm_params)
|
|
|
|
id2 = Router().generate_model_id(model_group=model_group, litellm_params=litellm_params)
|
|
|
|
assert id1 == id2
|
|
|
|
|
|
def test_router_add_deployment():
|
|
initial_model_list = [
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {
|
|
"model": "openai/my-fake-model",
|
|
"api_key": "my-fake-key",
|
|
"api_base": "https://openai-function-calling-workers.tasslexyz.workers.dev/",
|
|
},
|
|
},
|
|
]
|
|
router = Router(model_list=initial_model_list)
|
|
|
|
init_model_id_list = router.get_model_ids()
|
|
|
|
print(f"init_model_id_list: {init_model_id_list}")
|
|
|
|
router.add_deployment(
|
|
deployment=Deployment(
|
|
model_name="gpt-instruct",
|
|
litellm_params=LiteLLM_Params(model="gpt-3.5-turbo-instruct"),
|
|
model_info=ModelInfo(),
|
|
)
|
|
)
|
|
|
|
new_model_id_list = router.get_model_ids()
|
|
|
|
print(f"new_model_id_list: {new_model_id_list}")
|
|
|
|
assert len(new_model_id_list) > len(init_model_id_list)
|
|
|
|
assert new_model_id_list[1] != new_model_id_list[0]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model, base_model, llm_provider",
|
|
[
|
|
("azure/gpt-4", None, "azure"),
|
|
("azure/gpt-4", "azure/gpt-4-0125-preview", "azure"),
|
|
("gpt-4", None, "openai"),
|
|
],
|
|
)
|
|
def test_router_get_model_info(model: str, base_model: str | None, llm_provider: Literal["azure", "openai"]) -> None:
|
|
"""
|
|
Test if router get model info works based on provider
|
|
|
|
For azure -> only if base model set
|
|
For openai -> use model=
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4",
|
|
"litellm_params": {
|
|
"model": model,
|
|
"api_key": "my-fake-key",
|
|
"api_base": "my-fake-base",
|
|
},
|
|
"model_info": {"base_model": base_model, "id": "1"},
|
|
}
|
|
]
|
|
)
|
|
|
|
deployment = router.get_deployment(model_id="1")
|
|
|
|
assert deployment is not None
|
|
|
|
if llm_provider == "openai" or (base_model is not None and llm_provider == "azure"):
|
|
router.get_router_model_info(deployment=deployment.to_json(), received_model_name=model)
|
|
else:
|
|
model_info = router.get_router_model_info(deployment=deployment.to_json(), received_model_name=model)
|
|
|
|
assert model_info is not None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model, base_model, llm_provider",
|
|
[
|
|
("azure/gpt-4", None, "azure"),
|
|
("azure/gpt-4", "azure/gpt-4-0125-preview", "azure"),
|
|
("gpt-4", None, "openai"),
|
|
],
|
|
)
|
|
def test_router_context_window_pre_call_check(model, base_model, llm_provider):
|
|
"""
|
|
- For an azure model
|
|
- if no base model set
|
|
- don't enforce context window limits
|
|
"""
|
|
try:
|
|
model_list = [
|
|
{
|
|
"model_name": "gpt-4",
|
|
"litellm_params": {
|
|
"model": model,
|
|
"api_key": "my-fake-key",
|
|
"api_base": "my-fake-base",
|
|
},
|
|
"model_info": {"base_model": base_model, "id": "1"},
|
|
}
|
|
]
|
|
router = Router(
|
|
model_list=model_list,
|
|
set_verbose=True,
|
|
enable_pre_call_checks=True,
|
|
num_retries=0,
|
|
)
|
|
|
|
litellm.token_counter = MagicMock()
|
|
|
|
def token_counter_side_effect(*args, **kwargs):
|
|
# Process args and kwargs if needed
|
|
return 1000000
|
|
|
|
litellm.token_counter.side_effect = token_counter_side_effect
|
|
try:
|
|
updated_list = router._pre_call_checks(
|
|
model="gpt-4",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
)
|
|
if llm_provider == "azure" and base_model is None:
|
|
assert len(updated_list) == 1
|
|
else:
|
|
pytest.fail("Expected to raise an error. Got={}".format(updated_list))
|
|
except Exception as e:
|
|
if (llm_provider == "azure" and base_model is not None) or llm_provider == "openai":
|
|
pass
|
|
except Exception as e:
|
|
pytest.fail(f"Got unexpected exception on router! - {str(e)}")
|
|
|
|
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
@pytest.mark.asyncio
|
|
async def test_aaarouter_dynamic_cooldown_message_retry_time(sync_mode):
|
|
"""
|
|
User feedback: litellm says "No deployments available for selected model, Try again in 60 seconds"
|
|
but Azure says to retry in at most 9s
|
|
|
|
Tests that:
|
|
1. deployment_callback_on_failure reads retry-after header and uses it as cooldown time
|
|
2. Cooled-down deployments appear in get_cooldown_deployments
|
|
3. RouterRateLimitError is raised with the correct cooldown_time when all deployments are cooled down
|
|
"""
|
|
from httpx import Headers, Request, Response
|
|
|
|
cooldown_time = 30.0
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "text-embedding-ada-002",
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-ada-002",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "text-embedding-ada-002",
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-ada-002",
|
|
},
|
|
},
|
|
],
|
|
cooldown_time=cooldown_time,
|
|
)
|
|
|
|
# Build a 429 exception with retry-after header, matching what the OpenAI SDK raises
|
|
mock_exception = litellm.RateLimitError(
|
|
message="Rate Limit Error!",
|
|
llm_provider="openai",
|
|
model="text-embedding-ada-002",
|
|
response=Response(
|
|
status_code=429,
|
|
headers=Headers(
|
|
{
|
|
"retry-after": f"{cooldown_time}",
|
|
"content-type": "application/json",
|
|
}
|
|
),
|
|
request=Request("POST", "https://api.openai.com/v1/embeddings"),
|
|
),
|
|
)
|
|
|
|
# Directly invoke the Router's failure callback for each deployment,
|
|
# simulating what the logging framework would do on failure.
|
|
# This tests the cooldown logic without depending on the global customLogger state.
|
|
model_ids = router.get_model_ids()
|
|
for model_id in model_ids:
|
|
deployment_kwargs = {
|
|
"exception": mock_exception,
|
|
"litellm_params": {
|
|
"model_info": {"id": model_id},
|
|
},
|
|
}
|
|
router.deployment_callback_on_failure(
|
|
kwargs=deployment_kwargs,
|
|
completion_response=None,
|
|
start_time=None,
|
|
end_time=None,
|
|
)
|
|
|
|
if sync_mode:
|
|
cooldown_deployments = get_cooldown_deployments(ctx=router._core_ctx, parent_otel_span=None)
|
|
else:
|
|
cooldown_deployments = await async_get_cooldown_deployments(ctx=router._core_ctx, parent_otel_span=None)
|
|
|
|
assert len(cooldown_deployments) > 0
|
|
|
|
# Verify that a subsequent call raises RouterRateLimitError with correct cooldown_time
|
|
if sync_mode:
|
|
with pytest.raises(litellm.types.router.RouterRateLimitError) as exc_info:
|
|
router.embedding(
|
|
model="text-embedding-ada-002",
|
|
input="Hello world!",
|
|
mock_response=[0.1, 0.2, 0.3],
|
|
)
|
|
else:
|
|
with pytest.raises(litellm.types.router.RouterRateLimitError) as exc_info:
|
|
await router.aembedding(
|
|
model="text-embedding-ada-002",
|
|
input="Hello world!",
|
|
mock_response=[0.1, 0.2, 0.3],
|
|
)
|
|
|
|
assert exc_info.value.cooldown_time == cooldown_time
|
|
|
|
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
@pytest.mark.asyncio()
|
|
@pytest.mark.flaky(retries=6, delay=1)
|
|
async def test_router_weighted_pick(sync_mode: bool) -> None:
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"weight": 2,
|
|
"mock_response": "Hello world 1!",
|
|
},
|
|
"model_info": {"id": "1"},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"weight": 1,
|
|
"mock_response": "Hello world 2!",
|
|
},
|
|
"model_info": {"id": "2"},
|
|
},
|
|
]
|
|
)
|
|
|
|
model_id_1_count = 0
|
|
model_id_2_count = 0
|
|
for _ in range(50):
|
|
if sync_mode:
|
|
response = router.completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hello world!"}],
|
|
)
|
|
else:
|
|
response = await router.acompletion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hello world!"}],
|
|
)
|
|
|
|
model_id = int(response._hidden_params["model_id"])
|
|
|
|
if model_id == 1:
|
|
model_id_1_count += 1
|
|
elif model_id == 2:
|
|
model_id_2_count += 1
|
|
else:
|
|
raise Exception("invalid model id returned!")
|
|
assert model_id_1_count > model_id_2_count
|
|
|
|
|
|
@pytest.mark.parametrize("hidden", [True, False])
|
|
def test_model_group_alias(hidden: bool) -> None:
|
|
_model_list = [
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
},
|
|
{"model_name": "gpt-4", "litellm_params": {"model": "gpt-4"}},
|
|
]
|
|
router = Router(
|
|
model_list=_model_list,
|
|
model_group_alias={"gpt-4.5-turbo": {"model": "gpt-3.5-turbo", "hidden": hidden}},
|
|
)
|
|
|
|
models = router.get_model_list()
|
|
|
|
model_names = router.get_model_names()
|
|
|
|
if hidden:
|
|
assert len(models) == len(_model_list)
|
|
assert len(model_names) == len(_model_list)
|
|
else:
|
|
assert len(models) == len(_model_list) + 1
|
|
assert len(model_names) == len(_model_list) + 1
|
|
|
|
|
|
def test_router_get_model_list_from_model_alias() -> None:
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
}
|
|
],
|
|
model_group_alias={"my-special-fake-model-alias-name": "fake-openai-endpoint-3"},
|
|
)
|
|
|
|
model_alias_list = router.get_model_list_from_model_alias(model_name="gpt-3.5-turbo")
|
|
assert len(model_alias_list) == 0
|
|
|
|
|
|
def test_router_dynamic_credentials() -> None:
|
|
"""
|
|
Assert model id for dynamic api key 1 != model id for dynamic api key 2
|
|
"""
|
|
original_model_id = "123"
|
|
original_api_key = "my-bad-key"
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-3.5-turbo",
|
|
"api_key": original_api_key,
|
|
"mock_response": "fake_response",
|
|
},
|
|
"model_info": {"id": original_model_id},
|
|
}
|
|
]
|
|
)
|
|
|
|
deployment = router.get_deployment(model_id=original_model_id)
|
|
assert deployment is not None
|
|
assert deployment.litellm_params.api_key == original_api_key
|
|
|
|
response = router.completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
api_key="my-bad-key-2",
|
|
)
|
|
|
|
response_2 = router.completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
api_key="my-bad-key-3",
|
|
)
|
|
|
|
assert response_2._hidden_params["model_id"] != response._hidden_params["model_id"]
|
|
|
|
deployment = router.get_deployment(model_id=original_model_id)
|
|
assert deployment is not None
|
|
assert deployment.litellm_params.api_key == original_api_key
|
|
|
|
|
|
def test_router_get_model_group_info() -> None:
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {"model": "gpt-3.5-turbo"},
|
|
},
|
|
{
|
|
"model_name": "gpt-4",
|
|
"litellm_params": {"model": "gpt-4"},
|
|
},
|
|
],
|
|
)
|
|
|
|
model_group_info = router.get_model_group_info(model_group="gpt-4")
|
|
assert model_group_info is not None
|
|
assert model_group_info.model_group == "gpt-4"
|
|
assert model_group_info.input_cost_per_token > 0
|
|
assert model_group_info.output_cost_per_token > 0
|
|
|
|
|
|
def test_pre_call_checks_keep_deployment_whose_rpm_is_reached():
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {"model": "openai/my-fake-model", "api_key": "my-fake-key", "rpm": 0},
|
|
"model_info": {"id": "rpm-zero"},
|
|
},
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {"model": "openai/my-fake-model", "api_key": "my-fake-key"},
|
|
"model_info": {"id": "uncapped"},
|
|
},
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
kept: Final = router._pre_call_checks(
|
|
model="fake-openai-endpoint",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
)
|
|
|
|
assert [deployment["model_info"]["id"] for deployment in kept] == ["rpm-zero", "uncapped"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_enable_pre_call_checks_alone_serves_past_deployment_rpm():
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {"model": "openai/my-fake-model", "api_key": "my-fake-key", "rpm": 1},
|
|
"model_info": {"id": "rpm-one"},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
num_retries=0,
|
|
)
|
|
|
|
served: Final = [
|
|
await router.acompletion(
|
|
model="fake-openai-endpoint",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
mock_response="served",
|
|
)
|
|
for _ in range(3)
|
|
]
|
|
|
|
assert [response.choices[0].message.content for response in served] == ["served"] * 3
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_enforce_model_rate_limits_returns_429_at_deployment_rpm():
|
|
frozen_minute: Final = datetime(2026, 1, 1, 12, 30, 15, tzinfo=timezone.utc)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {"model": "openai/my-fake-model", "api_key": "my-fake-key", "rpm": 1},
|
|
"model_info": {"id": "rpm-one"},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
optional_pre_call_checks=["enforce_model_rate_limits"],
|
|
num_retries=0,
|
|
)
|
|
|
|
with patch(
|
|
"litellm.router_utils.pre_call_checks.model_rate_limit_check.get_utc_datetime", return_value=frozen_minute
|
|
):
|
|
first: Final = await router.acompletion(
|
|
model="fake-openai-endpoint",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
mock_response="served",
|
|
)
|
|
with pytest.raises(litellm.RateLimitError) as rate_limited:
|
|
await router.acompletion(
|
|
model="fake-openai-endpoint",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
mock_response="served",
|
|
)
|
|
|
|
assert first.choices[0].message.content == "served"
|
|
assert rate_limited.value.status_code == 429
|
|
|
|
|
|
class CompletionCustomHandler(
|
|
CustomLogger
|
|
): # https://docs.litellm.ai/docs/observability/custom_callback#callback-class
|
|
"""
|
|
The set of expected inputs to a custom handler for a
|
|
"""
|
|
|
|
# Class variables or attributes
|
|
def __init__(self):
|
|
self.errors = []
|
|
self.states: Optional[
|
|
List[
|
|
Literal[
|
|
"sync_pre_api_call",
|
|
"async_pre_api_call",
|
|
"post_api_call",
|
|
"sync_stream",
|
|
"async_stream",
|
|
"sync_success",
|
|
"async_success",
|
|
"sync_failure",
|
|
"async_failure",
|
|
]
|
|
]
|
|
] = []
|
|
|
|
def log_pre_api_call(self, model, messages, kwargs):
|
|
try:
|
|
print(f"received kwargs in pre-input: {kwargs}")
|
|
self.states.append("sync_pre_api_call")
|
|
## MODEL
|
|
assert isinstance(model, str)
|
|
## MESSAGES
|
|
assert isinstance(messages, list)
|
|
## KWARGS
|
|
assert isinstance(kwargs["model"], str)
|
|
assert isinstance(kwargs["messages"], list)
|
|
assert isinstance(kwargs["optional_params"], dict)
|
|
assert isinstance(kwargs["litellm_params"], dict)
|
|
assert isinstance(kwargs["start_time"], (datetime, type(None)))
|
|
assert isinstance(kwargs["stream"], bool)
|
|
assert isinstance(kwargs["user"], (str, type(None)))
|
|
### ROUTER-SPECIFIC KWARGS
|
|
assert isinstance(kwargs["litellm_params"]["metadata"], dict)
|
|
assert isinstance(kwargs["litellm_params"]["metadata"]["model_group"], str)
|
|
assert isinstance(kwargs["litellm_params"]["metadata"]["deployment"], str)
|
|
assert isinstance(kwargs["litellm_params"]["model_info"], dict)
|
|
assert isinstance(kwargs["litellm_params"]["model_info"]["id"], str)
|
|
assert isinstance(kwargs["litellm_params"]["proxy_server_request"], (str, type(None)))
|
|
assert isinstance(kwargs["litellm_params"]["preset_cache_key"], (str, type(None)))
|
|
assert isinstance(kwargs["litellm_params"]["stream_response"], dict)
|
|
except Exception as e:
|
|
print(f"Assertion Error: {traceback.format_exc()}")
|
|
self.errors.append(traceback.format_exc())
|
|
|
|
def log_post_api_call(self, kwargs, response_obj, start_time, end_time):
|
|
try:
|
|
self.states.append("post_api_call")
|
|
## START TIME
|
|
assert isinstance(start_time, datetime)
|
|
## END TIME
|
|
assert end_time == None
|
|
## RESPONSE OBJECT
|
|
assert response_obj == None
|
|
## KWARGS
|
|
assert isinstance(kwargs["model"], str)
|
|
assert isinstance(kwargs["messages"], list)
|
|
assert isinstance(kwargs["optional_params"], dict)
|
|
assert isinstance(kwargs["litellm_params"], dict)
|
|
assert isinstance(kwargs["start_time"], (datetime, type(None)))
|
|
assert isinstance(kwargs["stream"], bool)
|
|
assert isinstance(kwargs["user"], (str, type(None)))
|
|
assert isinstance(kwargs["input"], (list, dict, str))
|
|
assert isinstance(kwargs["api_key"], (str, type(None)))
|
|
assert (
|
|
isinstance(kwargs["original_response"], (str, litellm.CustomStreamWrapper))
|
|
or inspect.iscoroutine(kwargs["original_response"])
|
|
or inspect.isasyncgen(kwargs["original_response"])
|
|
)
|
|
assert isinstance(kwargs["additional_args"], (dict, type(None)))
|
|
assert isinstance(kwargs["log_event_type"], str)
|
|
### ROUTER-SPECIFIC KWARGS
|
|
assert isinstance(kwargs["litellm_params"]["metadata"], dict)
|
|
assert isinstance(kwargs["litellm_params"]["metadata"]["model_group"], str)
|
|
assert isinstance(kwargs["litellm_params"]["metadata"]["deployment"], str)
|
|
assert isinstance(kwargs["litellm_params"]["model_info"], dict)
|
|
assert isinstance(kwargs["litellm_params"]["model_info"]["id"], str)
|
|
assert isinstance(kwargs["litellm_params"]["proxy_server_request"], (str, type(None)))
|
|
assert isinstance(kwargs["litellm_params"]["preset_cache_key"], (str, type(None)))
|
|
assert isinstance(kwargs["litellm_params"]["stream_response"], dict)
|
|
except Exception:
|
|
print(f"Assertion Error: {traceback.format_exc()}")
|
|
self.errors.append(traceback.format_exc())
|
|
|
|
async def async_log_stream_event(self, kwargs, response_obj, start_time, end_time):
|
|
try:
|
|
self.states.append("async_stream")
|
|
## START TIME
|
|
assert isinstance(start_time, datetime)
|
|
## END TIME
|
|
assert isinstance(end_time, datetime)
|
|
## RESPONSE OBJECT
|
|
assert isinstance(response_obj, litellm.ModelResponseStream)
|
|
## KWARGS
|
|
assert isinstance(kwargs["model"], str)
|
|
assert isinstance(kwargs["messages"], list) and isinstance(kwargs["messages"][0], dict)
|
|
assert isinstance(kwargs["optional_params"], dict)
|
|
assert isinstance(kwargs["litellm_params"], dict)
|
|
assert isinstance(kwargs["start_time"], (datetime, type(None)))
|
|
assert isinstance(kwargs["stream"], bool)
|
|
assert isinstance(kwargs["user"], (str, type(None)))
|
|
assert (isinstance(kwargs["input"], list) and isinstance(kwargs["input"][0], dict)) or isinstance(
|
|
kwargs["input"], (dict, str)
|
|
)
|
|
assert isinstance(kwargs["api_key"], (str, type(None)))
|
|
assert (
|
|
isinstance(kwargs["original_response"], (str, litellm.CustomStreamWrapper))
|
|
or inspect.isasyncgen(kwargs["original_response"])
|
|
or inspect.iscoroutine(kwargs["original_response"])
|
|
)
|
|
assert isinstance(kwargs["additional_args"], (dict, type(None)))
|
|
assert isinstance(kwargs["log_event_type"], str)
|
|
except Exception:
|
|
print(f"Assertion Error: {traceback.format_exc()}")
|
|
self.errors.append(traceback.format_exc())
|
|
|
|
def log_success_event(self, kwargs, response_obj, start_time, end_time):
|
|
try:
|
|
self.states.append("sync_success")
|
|
## START TIME
|
|
assert isinstance(start_time, datetime)
|
|
## END TIME
|
|
assert isinstance(end_time, datetime)
|
|
## RESPONSE OBJECT
|
|
assert isinstance(response_obj, litellm.ModelResponse)
|
|
## KWARGS
|
|
assert isinstance(kwargs["model"], str)
|
|
assert isinstance(kwargs["messages"], list) and isinstance(kwargs["messages"][0], dict)
|
|
assert isinstance(kwargs["optional_params"], dict)
|
|
assert isinstance(kwargs["litellm_params"], dict)
|
|
assert isinstance(kwargs["start_time"], (datetime, type(None)))
|
|
assert isinstance(kwargs["stream"], bool)
|
|
assert isinstance(kwargs["user"], (str, type(None)))
|
|
assert (isinstance(kwargs["input"], list) and isinstance(kwargs["input"][0], dict)) or isinstance(
|
|
kwargs["input"], (dict, str)
|
|
)
|
|
assert isinstance(kwargs["api_key"], (str, type(None)))
|
|
assert isinstance(kwargs["original_response"], (str, litellm.CustomStreamWrapper))
|
|
assert isinstance(kwargs["additional_args"], (dict, type(None)))
|
|
assert isinstance(kwargs["log_event_type"], str)
|
|
assert kwargs["cache_hit"] is None or isinstance(kwargs["cache_hit"], bool)
|
|
except Exception:
|
|
print(f"Assertion Error: {traceback.format_exc()}")
|
|
self.errors.append(traceback.format_exc())
|
|
|
|
def log_failure_event(self, kwargs, response_obj, start_time, end_time):
|
|
try:
|
|
self.states.append("sync_failure")
|
|
## START TIME
|
|
assert isinstance(start_time, datetime)
|
|
## END TIME
|
|
assert isinstance(end_time, datetime)
|
|
## RESPONSE OBJECT
|
|
assert response_obj == None
|
|
## KWARGS
|
|
assert isinstance(kwargs["model"], str)
|
|
assert isinstance(kwargs["messages"], list) and isinstance(kwargs["messages"][0], dict)
|
|
assert isinstance(kwargs["optional_params"], dict)
|
|
assert isinstance(kwargs["litellm_params"], dict)
|
|
assert isinstance(kwargs["start_time"], (datetime, type(None)))
|
|
assert isinstance(kwargs["stream"], bool)
|
|
assert isinstance(kwargs["user"], (str, type(None)))
|
|
assert (isinstance(kwargs["input"], list) and isinstance(kwargs["input"][0], dict)) or isinstance(
|
|
kwargs["input"], (dict, str)
|
|
)
|
|
assert isinstance(kwargs["api_key"], (str, type(None)))
|
|
assert (
|
|
isinstance(kwargs["original_response"], (str, litellm.CustomStreamWrapper))
|
|
or kwargs["original_response"] == None
|
|
)
|
|
assert isinstance(kwargs["additional_args"], (dict, type(None)))
|
|
assert isinstance(kwargs["log_event_type"], str)
|
|
except Exception:
|
|
print(f"Assertion Error: {traceback.format_exc()}")
|
|
self.errors.append(traceback.format_exc())
|
|
|
|
async def async_log_pre_api_call(self, model, messages, kwargs):
|
|
try:
|
|
"""
|
|
No-op.
|
|
Not implemented yet.
|
|
"""
|
|
pass
|
|
except Exception as e:
|
|
print(f"Assertion Error: {traceback.format_exc()}")
|
|
self.errors.append(traceback.format_exc())
|
|
|
|
async def async_log_success_event(self, kwargs, response_obj, start_time, end_time):
|
|
try:
|
|
print("CompletionCustomHandler.async_log_success_event, kwargs: ", kwargs)
|
|
self.states.append("async_success")
|
|
print(
|
|
"############### CompletionCustomHandler async success, kwargs: ",
|
|
kwargs,
|
|
)
|
|
## START TIME
|
|
assert isinstance(start_time, datetime)
|
|
## END TIME
|
|
assert isinstance(end_time, datetime)
|
|
## RESPONSE OBJECT
|
|
assert isinstance(response_obj, (litellm.ModelResponse, litellm.EmbeddingResponse))
|
|
## KWARGS
|
|
assert isinstance(kwargs["model"], str)
|
|
|
|
# checking we use base_model for azure cost calculation
|
|
base_model = litellm.utils.get_base_model_from_metadata(model_call_details=kwargs)
|
|
|
|
if kwargs["model"] == "chatgpt-v-3" and base_model is not None and kwargs["stream"] != True:
|
|
# when base_model is set for azure, we should use pricing for the base_model
|
|
# this checks response_cost == litellm.cost_per_token(model=base_model)
|
|
assert isinstance(kwargs["response_cost"], float)
|
|
response_cost = kwargs["response_cost"]
|
|
print(f"response_cost: {response_cost}, for model: {kwargs['model']} and base_model: {base_model}")
|
|
prompt_tokens = response_obj.usage.prompt_tokens
|
|
completion_tokens = response_obj.usage.completion_tokens
|
|
# ensure the pricing is based on the base_model here
|
|
prompt_price, completion_price = litellm.cost_per_token(
|
|
model=base_model,
|
|
prompt_tokens=prompt_tokens,
|
|
completion_tokens=completion_tokens,
|
|
)
|
|
expected_price = prompt_price + completion_price
|
|
print(f"expected price: {expected_price}")
|
|
assert response_cost == expected_price, (
|
|
f"response_cost: {response_cost} != expected_price: {expected_price}. For model: {kwargs['model']} and base_model: {base_model}. should have used base_model for price"
|
|
)
|
|
|
|
assert isinstance(kwargs["messages"], list)
|
|
assert isinstance(kwargs["optional_params"], dict)
|
|
assert isinstance(kwargs["litellm_params"], dict)
|
|
assert isinstance(kwargs["start_time"], (datetime, type(None)))
|
|
assert isinstance(kwargs["stream"], bool)
|
|
assert isinstance(kwargs["user"], (str, type(None)))
|
|
assert isinstance(kwargs["input"], (list, dict, str))
|
|
assert isinstance(kwargs["api_key"], (str, type(None)))
|
|
assert (
|
|
isinstance(kwargs["original_response"], (str, litellm.CustomStreamWrapper))
|
|
or inspect.isasyncgen(kwargs["original_response"])
|
|
or inspect.iscoroutine(kwargs["original_response"])
|
|
)
|
|
assert isinstance(kwargs["additional_args"], (dict, type(None)))
|
|
assert isinstance(kwargs["log_event_type"], str)
|
|
assert kwargs["cache_hit"] is None or isinstance(kwargs["cache_hit"], bool)
|
|
### ROUTER-SPECIFIC KWARGS
|
|
assert isinstance(kwargs["litellm_params"]["metadata"], dict)
|
|
assert isinstance(kwargs["litellm_params"]["metadata"]["model_group"], str)
|
|
assert isinstance(kwargs["litellm_params"]["metadata"]["deployment"], str)
|
|
assert isinstance(kwargs["litellm_params"]["model_info"], dict)
|
|
assert isinstance(kwargs["litellm_params"]["model_info"]["id"], str)
|
|
assert isinstance(kwargs["litellm_params"]["proxy_server_request"], (str, type(None)))
|
|
assert isinstance(kwargs["litellm_params"]["preset_cache_key"], (str, type(None)))
|
|
assert isinstance(kwargs["litellm_params"]["stream_response"], dict)
|
|
except Exception:
|
|
print(f"Assertion Error: {traceback.format_exc()}")
|
|
self.errors.append(traceback.format_exc())
|
|
|
|
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time):
|
|
try:
|
|
print(f"received original response: {kwargs['original_response']}")
|
|
self.states.append("async_failure")
|
|
## START TIME
|
|
assert isinstance(start_time, datetime)
|
|
## END TIME
|
|
assert isinstance(end_time, datetime)
|
|
## RESPONSE OBJECT
|
|
assert response_obj == None
|
|
## KWARGS
|
|
assert isinstance(kwargs["model"], str)
|
|
assert isinstance(kwargs["messages"], list)
|
|
assert isinstance(kwargs["optional_params"], dict)
|
|
assert isinstance(kwargs["litellm_params"], dict)
|
|
assert isinstance(kwargs["start_time"], (datetime, type(None)))
|
|
assert isinstance(kwargs["stream"], bool)
|
|
assert isinstance(kwargs["user"], (str, type(None)))
|
|
assert isinstance(kwargs["input"], (list, str, dict))
|
|
assert isinstance(kwargs["api_key"], (str, type(None)))
|
|
assert (
|
|
isinstance(kwargs["original_response"], (str, litellm.CustomStreamWrapper))
|
|
or inspect.isasyncgen(kwargs["original_response"])
|
|
or inspect.iscoroutine(kwargs["original_response"])
|
|
or kwargs["original_response"] == None
|
|
)
|
|
assert isinstance(kwargs["additional_args"], (dict, type(None)))
|
|
assert isinstance(kwargs["log_event_type"], str)
|
|
except Exception:
|
|
print(f"Assertion Error: {traceback.format_exc()}")
|
|
self.errors.append(traceback.format_exc())
|
|
|
|
|
|
_FALLBACK_WIRE_PRIMARY: Final = "http://primary.wire.test/v1"
|
|
_FALLBACK_WIRE_BACKUP: Final = "http://backup.wire.test/v1"
|
|
_FALLBACK_WIRE_BACKUP_REPLY: Final = {
|
|
"id": "chatcmpl-backup",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-5.6",
|
|
"choices": [{"index": 0, "finish_reason": "stop", "message": {"role": "assistant", "content": "pong"}}],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
}
|
|
|
|
|
|
def _fallback_wire_router() -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-primary",
|
|
"api_base": _FALLBACK_WIRE_PRIMARY,
|
|
"max_retries": 0,
|
|
},
|
|
},
|
|
{
|
|
"model_name": "backup",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-5.6",
|
|
"api_key": "sk-backup",
|
|
"api_base": _FALLBACK_WIRE_BACKUP,
|
|
"max_retries": 0,
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"primary": ["backup"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
|
|
def _mock_fallback_wire(respx_mock: respx.MockRouter) -> tuple[respx.Route, respx.Route]:
|
|
primary = respx_mock.post(f"{_FALLBACK_WIRE_PRIMARY}/chat/completions").mock(
|
|
return_value=httpx.Response(500, json={"error": {"message": "primary overloaded", "type": "server_error"}})
|
|
)
|
|
backup = respx_mock.post(f"{_FALLBACK_WIRE_BACKUP}/chat/completions").mock(
|
|
return_value=httpx.Response(200, json=_FALLBACK_WIRE_BACKUP_REPLY)
|
|
)
|
|
return primary, backup
|
|
|
|
|
|
def _assert_fallback_errors_reached_the_caller_and_not_the_wire(
|
|
response: object, primary: respx.Route, backup: respx.Route
|
|
) -> None:
|
|
for route in (primary, backup):
|
|
assert route.called
|
|
for call in route.calls:
|
|
assert "include_fallback_errors" not in json.loads(call.request.content)
|
|
assert isinstance(response, litellm.ModelResponse)
|
|
assert response.choices[0].message.content == "pong"
|
|
headers = response._hidden_params["additional_headers"]
|
|
assert headers["x-litellm-attempted-fallbacks"] == 1
|
|
errors = json.loads(headers["x-litellm-fallback-errors"])
|
|
assert len(errors) == 1
|
|
assert "primary overloaded" in errors[0]["message"]
|
|
|
|
|
|
def test_sync_completion_keeps_include_fallback_errors_off_the_wire_and_returns_the_errors():
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
primary, backup = _mock_fallback_wire(respx_mock)
|
|
response = _fallback_wire_router().completion(
|
|
model="primary", messages=[{"role": "user", "content": "hi"}], include_fallback_errors=True
|
|
)
|
|
_assert_fallback_errors_reached_the_caller_and_not_the_wire(response, primary, backup)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_keeps_include_fallback_errors_off_the_wire_and_returns_the_errors(monkeypatch):
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
with respx.mock(assert_all_called=True) as respx_mock:
|
|
primary, backup = _mock_fallback_wire(respx_mock)
|
|
response = await _fallback_wire_router().acompletion(
|
|
model="primary", messages=[{"role": "user", "content": "hi"}], include_fallback_errors=True
|
|
)
|
|
_assert_fallback_errors_reached_the_caller_and_not_the_wire(response, primary, backup)
|
|
|
|
|
|
def _create_custom_routing_router():
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "azure-model",
|
|
"litellm_params": {
|
|
"model": "openai/very-special-endpoint",
|
|
"api_base": FAKE_OPENAI_API_BASE,
|
|
"api_key": "fake-key",
|
|
},
|
|
"model_info": {"id": "very-special-endpoint"},
|
|
},
|
|
{
|
|
"model_name": "azure-model",
|
|
"litellm_params": {
|
|
"model": "openai/fast-endpoint",
|
|
"api_base": FAKE_OPENAI_API_BASE,
|
|
"api_key": "fake-key",
|
|
},
|
|
"model_info": {"id": "fast-endpoint"},
|
|
},
|
|
],
|
|
set_verbose=True,
|
|
debug_level="DEBUG",
|
|
)
|
|
|
|
|
|
class SpecialEndpointRoutingStrategy(CustomRoutingStrategyBase):
|
|
def __init__(self, router_instance: Router):
|
|
self._router = router_instance
|
|
|
|
async def async_get_available_deployment(
|
|
self,
|
|
model: str,
|
|
messages: list[dict[str, str]] | None = None,
|
|
input: str | list | None = None,
|
|
specific_deployment: bool | None = False,
|
|
request_kwargs: dict | None = None,
|
|
):
|
|
print("In CUSTOM async get available deployment")
|
|
model_list = self._router.model_list
|
|
print("router model list=", model_list)
|
|
for model in model_list:
|
|
if isinstance(model, dict):
|
|
if model["litellm_params"]["model"] == "openai/very-special-endpoint":
|
|
return model
|
|
pass
|
|
|
|
def get_available_deployment(
|
|
self,
|
|
model: str,
|
|
messages: list[dict[str, str]] | None = None,
|
|
input: str | list | None = None,
|
|
specific_deployment: bool | None = False,
|
|
request_kwargs: dict | None = None,
|
|
):
|
|
pass
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_select_picks_through_a_custom_routing_strategy() -> None:
|
|
router: Final = _create_custom_routing_router()
|
|
router.set_custom_routing_strategy(SpecialEndpointRoutingStrategy(router))
|
|
|
|
picked: Final = await router.select("azure-model", {"metadata": {}})
|
|
|
|
assert picked["model_info"]["id"] == "very-special-endpoint"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_azure_router_acompletion_passes_deployment_credentials_for_plain_and_stream(
|
|
monkeypatch: pytest.MonkeyPatch, respx_mock: respx.MockRouter
|
|
) -> None:
|
|
monkeypatch.delenv("AZURE_API_KEY", raising=False)
|
|
monkeypatch.delenv("AZURE_OPENAI_API_KEY", raising=False)
|
|
monkeypatch.delenv("AZURE_AI_API_KEY", raising=False)
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
|
|
api_key: Final = "deployment-api-key"
|
|
api_base: Final = "https://router-azure.example.com"
|
|
api_version: Final = "2025-04-01-preview"
|
|
messages: Final = [{"role": "user", "content": "hello this request will pass"}]
|
|
stream_chunk: Final = {
|
|
"id": "chatcmpl-router-test",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1,
|
|
"model": "gpt-4.1-mini",
|
|
"choices": [{"index": 0, "delta": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}],
|
|
}
|
|
route: Final = respx_mock.post(url__regex=r"https://router-azure\.example\.com/.*").mock(
|
|
side_effect=[
|
|
httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-router-test",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-4.1-mini",
|
|
"choices": [
|
|
{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}
|
|
],
|
|
"usage": {"prompt_tokens": 6, "completion_tokens": 1, "total_tokens": 7},
|
|
},
|
|
),
|
|
httpx.Response(
|
|
200,
|
|
headers={"content-type": "text/event-stream"},
|
|
text=f"data: {json.dumps(stream_chunk)}\n\ndata: [DONE]\n\n",
|
|
),
|
|
]
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "azure-routed",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_key": api_key,
|
|
"api_base": api_base,
|
|
"api_version": api_version,
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
response: Final = await router.acompletion(model="azure-routed", messages=messages)
|
|
stream: Final = await router.acompletion(model="azure-routed", messages=messages, stream=True)
|
|
streamed_text: Final = "".join([chunk.choices[0].delta.content or "" async for chunk in stream])
|
|
|
|
assert response.choices[0].message.content == "ok"
|
|
assert streamed_text == "ok"
|
|
assert [str(call.request.url) for call in route.calls] == [
|
|
f"{api_base}/openai/deployments/gpt-4.1-mini/chat/completions?api-version={api_version}"
|
|
] * 2
|
|
assert [call.request.headers["api-key"] for call in route.calls] == [api_key, api_key]
|
|
assert [json.loads(call.request.content) for call in route.calls] == [
|
|
{"model": "gpt-4.1-mini", "messages": messages, "stream": False},
|
|
{"model": "gpt-4.1-mini", "messages": messages, "stream": True},
|
|
]
|
|
|
|
|
|
def test_multiple_deployments_route_to_a_configured_model(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
|
|
def _deployment_response(request: httpx.Request) -> httpx.Response:
|
|
request_body: Final = json.loads(request.content)
|
|
requested_model: Final = str(request_body["model"])
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-deployment",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": requested_model,
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"message": {"role": "assistant", "content": f"deployment response for {requested_model}"},
|
|
"finish_reason": "stop",
|
|
}
|
|
],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
},
|
|
)
|
|
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(side_effect=_deployment_response)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "test-group",
|
|
"litellm_params": {"model": "openai/first", "api_key": "test-key"},
|
|
"model_info": {"id": "first"},
|
|
},
|
|
{
|
|
"model_name": "test-group",
|
|
"litellm_params": {"model": "openai/second", "api_key": "test-key"},
|
|
"model_info": {"id": "second"},
|
|
},
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="test-group",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
)
|
|
|
|
model_id: Final = str(response._hidden_params["model_id"])
|
|
assert model_id in ("first", "second")
|
|
assert route.call_count == 1
|
|
assert json.loads(route.calls[0].request.content)["model"] == model_id
|
|
assert response.choices[0].message.content == f"deployment response for {model_id}"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_custom_routing_uses_the_strategy_selected_deployment(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post(f"{FAKE_OPENAI_API_BASE}/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-custom-routing",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "very-special-endpoint",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"message": {"role": "assistant", "content": "custom deployment response"},
|
|
"finish_reason": "stop",
|
|
}
|
|
],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
},
|
|
)
|
|
)
|
|
router: Final = _create_custom_routing_router()
|
|
router.set_custom_routing_strategy(SpecialEndpointRoutingStrategy(router))
|
|
|
|
response: Final = await router.acompletion(
|
|
model="azure-model",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
)
|
|
|
|
assert response.choices[0].message.content == "custom deployment response"
|
|
assert response._hidden_params["model_id"] == "very-special-endpoint"
|
|
assert route.call_count == 1
|
|
assert json.loads(route.calls[0].request.content)["model"] == "very-special-endpoint"
|
|
|
|
|
|
def _openai_chat_response(content: str = "scripted reply") -> httpx.Response:
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-router",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-4o-mini",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"message": {"role": "assistant", "content": content},
|
|
"finish_reason": "stop",
|
|
}
|
|
],
|
|
"usage": {"prompt_tokens": 2, "completion_tokens": 2, "total_tokens": 4},
|
|
},
|
|
)
|
|
|
|
|
|
def _openai_chat_stream_response(content: str = "streamed reply") -> httpx.Response:
|
|
return httpx.Response(
|
|
200,
|
|
text=(
|
|
"data: "
|
|
+ json.dumps(
|
|
{
|
|
"id": "chatcmpl-router-stream",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1,
|
|
"model": "gpt-4o-mini",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"delta": {"role": "assistant", "content": content},
|
|
"finish_reason": None,
|
|
}
|
|
],
|
|
}
|
|
)
|
|
+ "\n\ndata: "
|
|
+ json.dumps(
|
|
{
|
|
"id": "chatcmpl-router-stream",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1,
|
|
"model": "gpt-4o-mini",
|
|
"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
|
|
}
|
|
)
|
|
+ "\n\ndata: [DONE]\n\n"
|
|
),
|
|
headers={"content-type": "text/event-stream"},
|
|
)
|
|
|
|
|
|
def test_router_deployment_typing_routes_typed_deployment() -> None:
|
|
deployment: Final = DeploymentTypedDict(
|
|
model_name="typed-model",
|
|
litellm_params={
|
|
"model": "openai/gpt-4o-mini",
|
|
"mock_response": "typed deployment response",
|
|
},
|
|
model_info={"id": "typed-id"},
|
|
)
|
|
router: Final = Router(model_list=[deployment], num_retries=0)
|
|
|
|
response: Final = router.completion(
|
|
model="typed-model",
|
|
messages=[{"role": "user", "content": "typed deployment"}],
|
|
)
|
|
|
|
assert response.choices[0].message.content == "typed deployment response"
|
|
assert response._hidden_params["model_id"] == "typed-id"
|
|
|
|
|
|
def test_router_retries_transient_server_error(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
side_effect=[
|
|
httpx.Response(500, json={"error": {"message": "temporary failure"}}),
|
|
_openai_chat_response("recovered"),
|
|
]
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "retry-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "test-key",
|
|
},
|
|
}
|
|
],
|
|
num_retries=1,
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="retry-model",
|
|
messages=[{"role": "user", "content": "retry once"}],
|
|
)
|
|
|
|
assert response.choices[0].message.content == "recovered"
|
|
assert route.call_count == 2
|
|
|
|
|
|
def test_router_authentication_error_is_not_swallowed(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
side_effect=[
|
|
httpx.Response(
|
|
401,
|
|
json={
|
|
"error": {
|
|
"message": "first invalid key",
|
|
"type": "authentication_error",
|
|
}
|
|
},
|
|
),
|
|
httpx.Response(
|
|
401,
|
|
json={
|
|
"error": {
|
|
"message": "last invalid key",
|
|
"type": "authentication_error",
|
|
}
|
|
},
|
|
),
|
|
]
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "auth-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "first-test-key",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "auth-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "second-test-key",
|
|
},
|
|
},
|
|
],
|
|
num_retries=1,
|
|
)
|
|
|
|
with pytest.raises(openai.AuthenticationError, match="last invalid key"):
|
|
router.completion(
|
|
model="auth-model",
|
|
messages=[{"role": "user", "content": "auth failure"}],
|
|
)
|
|
|
|
assert route.call_count == 2
|
|
assert frozenset(call.request.headers["authorization"] for call in route.calls) == (
|
|
frozenset({"Bearer first-test-key", "Bearer second-test-key"})
|
|
)
|
|
|
|
|
|
def test_router_does_not_log_model_list_api_key_after_auth_failure(
|
|
respx_mock: respx.MockRouter,
|
|
caplog: pytest.LogCaptureFixture,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
for logger in (verbose_logger, verbose_router_logger):
|
|
caplog.set_level("DEBUG", logger=logger.name)
|
|
logger.addHandler(caplog.handler)
|
|
api_key: Final = "sensitive-router-test-key"
|
|
respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
401,
|
|
json={"error": {"message": "invalid api key", "type": "authentication_error"}},
|
|
)
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "auth-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": api_key,
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
with pytest.raises(openai.AuthenticationError):
|
|
router.completion(
|
|
model="auth-model",
|
|
messages=[{"role": "user", "content": "auth failure"}],
|
|
)
|
|
|
|
assert "invalid api key" in caplog.text
|
|
assert api_key not in caplog.text
|
|
|
|
|
|
def test_router_reads_api_key_from_model_list(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
side_effect=[_openai_chat_response(), _openai_chat_stream_response()]
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "keyed-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "model-list-key",
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="keyed-model",
|
|
messages=[{"role": "user", "content": "key from model list"}],
|
|
)
|
|
stream: Final = router.completion(
|
|
model="keyed-model",
|
|
messages=[{"role": "user", "content": "stream key from model list"}],
|
|
stream=True,
|
|
)
|
|
streamed_content: Final = "".join(chunk.choices[0].delta.content or "" for chunk in stream)
|
|
|
|
assert response.choices[0].message.content == "scripted reply"
|
|
assert streamed_content == "streamed reply"
|
|
assert route.call_count == 2
|
|
assert tuple(call.request.headers["authorization"] for call in route.calls) == (
|
|
"Bearer model-list-key",
|
|
"Bearer model-list-key",
|
|
)
|
|
|
|
|
|
def test_router_calls_requested_specific_deployment(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=_openai_chat_response()
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/gpt-4o-mini",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4.1-mini",
|
|
"api_key": "group-key",
|
|
},
|
|
"model_info": {"id": "group-id"},
|
|
},
|
|
{
|
|
"model_name": "specific-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "specific-key",
|
|
},
|
|
"model_info": {"id": "specific-id"},
|
|
},
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="openai/gpt-4o-mini",
|
|
messages=[{"role": "user", "content": "specific deployment"}],
|
|
specific_deployment=True,
|
|
)
|
|
|
|
assert response.choices[0].message.content == "scripted reply"
|
|
assert response._hidden_params["model_id"] == "specific-id"
|
|
assert route.call_count == 1
|
|
assert route.calls[0].request.headers["authorization"] == "Bearer specific-key"
|
|
assert json.loads(route.calls[0].request.content)["model"] == "gpt-4o-mini"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_context_window_error_uses_configured_fallback(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
side_effect=[
|
|
httpx.Response(
|
|
400,
|
|
json={
|
|
"error": {
|
|
"message": "This model's maximum context length is 16385 tokens",
|
|
"type": "invalid_request_error",
|
|
"code": "context_length_exceeded",
|
|
}
|
|
},
|
|
),
|
|
_openai_chat_response("fallback response"),
|
|
]
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "test-key",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "fallback",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4.1-mini",
|
|
"api_key": "test-key",
|
|
},
|
|
},
|
|
],
|
|
context_window_fallbacks=[{"primary": ["fallback"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "context fallback"}],
|
|
)
|
|
|
|
assert response.choices[0].message.content == "fallback response"
|
|
assert route.call_count == 2
|
|
assert tuple(json.loads(call.request.content)["model"] for call in route.calls) == ("gpt-4o-mini", "gpt-4.1-mini")
|
|
|
|
|
|
def test_router_function_calling_sends_tools_and_returns_tool_call(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-tools",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "gpt-4o-mini",
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"message": {
|
|
"role": "assistant",
|
|
"content": None,
|
|
"tool_calls": [
|
|
{
|
|
"id": "call-weather",
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_weather",
|
|
"arguments": '{"city":"Boston"}',
|
|
},
|
|
}
|
|
],
|
|
},
|
|
"finish_reason": "tool_calls",
|
|
}
|
|
],
|
|
"usage": {
|
|
"prompt_tokens": 5,
|
|
"completion_tokens": 3,
|
|
"total_tokens": 8,
|
|
},
|
|
},
|
|
)
|
|
)
|
|
tools: Final = [
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_weather",
|
|
"description": "Get weather for a city",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {"city": {"type": "string"}},
|
|
"required": ["city"],
|
|
},
|
|
},
|
|
}
|
|
]
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "tools-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "test-key",
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="tools-model",
|
|
messages=[{"role": "user", "content": "weather in Boston"}],
|
|
tools=tools,
|
|
)
|
|
|
|
assert response.choices[0].message.tool_calls[0].function.name == "get_weather"
|
|
assert json.loads(route.calls[0].request.content)["tools"] == tools
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_openai_completion_supports_sync_async_and_streaming(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
side_effect=[
|
|
_openai_chat_response("async reply"),
|
|
_openai_chat_response("sync reply"),
|
|
_openai_chat_stream_response("async stream"),
|
|
_openai_chat_stream_response("sync stream"),
|
|
]
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "test-key",
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
async_response: Final = await router.acompletion(
|
|
model="openai-model",
|
|
messages=[{"role": "user", "content": "async"}],
|
|
)
|
|
sync_response: Final = router.completion(
|
|
model="openai-model",
|
|
messages=[{"role": "user", "content": "sync"}],
|
|
)
|
|
async_stream: Final = await router.acompletion(
|
|
model="openai-model",
|
|
messages=[{"role": "user", "content": "async stream"}],
|
|
stream=True,
|
|
)
|
|
async_stream_chunks: Final = tuple([chunk async for chunk in async_stream])
|
|
async_stream_content: Final = "".join(chunk.choices[0].delta.content or "" for chunk in async_stream_chunks)
|
|
sync_stream: Final = router.completion(
|
|
model="openai-model",
|
|
messages=[{"role": "user", "content": "sync stream"}],
|
|
stream=True,
|
|
)
|
|
sync_stream_content: Final = "".join(chunk.choices[0].delta.content or "" for chunk in sync_stream)
|
|
|
|
assert async_response.choices[0].message.content == "async reply"
|
|
assert sync_response.choices[0].message.content == "sync reply"
|
|
assert async_stream_content == "async stream"
|
|
assert sync_stream_content == "sync stream"
|
|
assert route.call_count == 4
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_router_moderation_returns_provider_result(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/moderations").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "modr-router",
|
|
"model": "omni-moderation-latest",
|
|
"results": [
|
|
{
|
|
"flagged": False,
|
|
"categories": {"sexual": False, "violence": False},
|
|
"category_scores": {"sexual": 0.0, "violence": 0.0},
|
|
}
|
|
],
|
|
},
|
|
)
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "moderation-model",
|
|
"litellm_params": {
|
|
"model": "omni-moderation-latest",
|
|
"api_key": "test-key",
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
response: Final = await router.amoderation(model="moderation-model", input="safe scripted text")
|
|
|
|
assert response.results[0].flagged is False
|
|
assert json.loads(route.calls[0].request.content)["input"] == "safe scripted text"
|
|
|
|
|
|
def test_router_anthropic_key_comes_from_model_list(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://api.anthropic.com/v1/messages").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "msg-router",
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"model": "claude-haiku-4-5-20251001",
|
|
"content": [{"type": "text", "text": "anthropic reply"}],
|
|
"stop_reason": "end_turn",
|
|
"stop_sequence": None,
|
|
"usage": {"input_tokens": 2, "output_tokens": 2},
|
|
},
|
|
)
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "anthropic-model",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-haiku-4-5-20251001",
|
|
"api_key": "model-list-anthropic-key",
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="anthropic-model",
|
|
messages=[{"role": "user", "content": "key check"}],
|
|
)
|
|
|
|
assert response.choices[0].message.content == "anthropic reply"
|
|
assert route.calls[0].request.headers["x-api-key"] == "model-list-anthropic-key"
|
|
|
|
|
|
def test_router_does_not_cool_down_api_connection_errors() -> None:
|
|
router: Final = Router()
|
|
|
|
assert (
|
|
_is_cooldown_required(
|
|
ctx=router._core_ctx,
|
|
model_id="connection-deployment",
|
|
exception_status=500,
|
|
exception_str="litellm.APIConnectionError: connection refused",
|
|
)
|
|
is False
|
|
)
|
|
|
|
|
|
def test_router_preserves_provider_rate_limit_error(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
429,
|
|
json={
|
|
"error": {
|
|
"message": "scripted rate limit",
|
|
"type": "rate_limit_error",
|
|
"code": "rate_limit_exceeded",
|
|
}
|
|
},
|
|
)
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "rate-limit-model",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "test-key",
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
with pytest.raises(litellm.RateLimitError) as raised:
|
|
router.completion(
|
|
model="rate-limit-model",
|
|
messages=[{"role": "user", "content": "preserve provider error"}],
|
|
)
|
|
|
|
assert raised.value.status_code == 429
|
|
|
|
|
|
def test_router_completion_with_model_id_routes_to_deployment() -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-4o-mini",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"mock_response": "deployment response",
|
|
},
|
|
"model_info": {"id": "deployment-123"},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="deployment-123",
|
|
messages=[{"role": "user", "content": "route by deployment id"}],
|
|
)
|
|
|
|
assert response.choices[0].message.content == "deployment response"
|
|
assert response._hidden_params["model_id"] == "deployment-123"
|
|
|
|
|
|
def _batch_router() -> litellm.Router:
|
|
return litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": name,
|
|
"litellm_params": {
|
|
"model": f"openai/{name}",
|
|
"api_key": "test-key",
|
|
"api_base": "https://batch-router.test/v1",
|
|
},
|
|
}
|
|
for name in ("first", "second")
|
|
],
|
|
num_retries=0,
|
|
)
|
|
|
|
|
|
def _batch_response(request: httpx.Request) -> httpx.Response:
|
|
request_body: Final = json.loads(request.content)
|
|
model: Final = request_body["model"]
|
|
prompt: Final = request_body["messages"][0]["content"]
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-batch-router",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": model,
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"message": {"role": "assistant", "content": f"{model}:{prompt}"},
|
|
"finish_reason": "stop",
|
|
}
|
|
],
|
|
},
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_batch_completion_multiple_models_returns_each_model_response(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://batch-router.test/v1/chat/completions").mock(side_effect=_batch_response)
|
|
router: Final = _batch_router()
|
|
|
|
responses: Final = cast(
|
|
list[litellm.ModelResponse],
|
|
await router.abatch_completion(
|
|
models=["first", "second"],
|
|
messages=[{"role": "user", "content": "same prompt"}],
|
|
),
|
|
)
|
|
|
|
assert route.call_count == 2
|
|
assert frozenset(response.choices[0].message.content for response in responses) == {
|
|
"first:same prompt",
|
|
"second:same prompt",
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_batch_completion_multiple_models_and_messages_preserves_each_pair(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route: Final = respx_mock.post("https://batch-router.test/v1/chat/completions").mock(side_effect=_batch_response)
|
|
router: Final = _batch_router()
|
|
|
|
responses: Final = cast(
|
|
list[list[litellm.ModelResponse]],
|
|
await router.abatch_completion(
|
|
models=["first", "second"],
|
|
messages=[
|
|
[{"role": "user", "content": "first prompt"}],
|
|
[{"role": "user", "content": "second prompt"}],
|
|
],
|
|
),
|
|
)
|
|
|
|
assert route.call_count == 4
|
|
assert [frozenset(response.choices[0].message.content for response in group) for group in responses] == [
|
|
{"first:first prompt", "second:first prompt"},
|
|
{"first:second prompt", "second:second prompt"},
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_batch_completion_fastest_response_prefers_the_mocked_deployment(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
release_provider: Final = asyncio.Event()
|
|
|
|
async def response(request: httpx.Request) -> httpx.Response:
|
|
await release_provider.wait()
|
|
return _batch_response(request)
|
|
|
|
respx_mock.post("https://batch-router.test/v1/chat/completions").mock(side_effect=response)
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "first",
|
|
"litellm_params": {
|
|
"model": "openai/first",
|
|
"api_key": "test-key",
|
|
"api_base": "https://batch-router.test/v1",
|
|
},
|
|
"model_info": {"id": "first-deployment"},
|
|
},
|
|
{
|
|
"model_name": "second",
|
|
"litellm_params": {
|
|
"model": "openai/second",
|
|
"api_key": "test-key",
|
|
"api_base": "https://batch-router.test/v1",
|
|
"mock_response": "mocked deployment response",
|
|
},
|
|
"model_info": {"id": "second-deployment"},
|
|
},
|
|
],
|
|
num_retries=0,
|
|
)
|
|
try:
|
|
result: Final = await router.abatch_completion_fastest_response(
|
|
model="first,second",
|
|
messages=[{"role": "user", "content": "fastest prompt"}],
|
|
)
|
|
finally:
|
|
release_provider.set()
|
|
|
|
assert result._hidden_params["model_id"] == "second-deployment"
|
|
assert result.choices[0].message.content == "mocked deployment response"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_batch_completion_fastest_response_streams_mocked_chunks() -> None:
|
|
router: Final = _batch_router()
|
|
|
|
response: Final = await router.abatch_completion_fastest_response(
|
|
model="first,second",
|
|
messages=[{"role": "user", "content": "stream prompt"}],
|
|
stream=True,
|
|
mock_response="stream answer",
|
|
)
|
|
chunks: Final = tuple([chunk async for chunk in response])
|
|
text: Final = "".join(
|
|
chunk.choices[0].delta.content or "" for chunk in chunks if chunk.choices and chunk.choices[0].delta is not None
|
|
)
|
|
|
|
assert text == "stream answer"
|
|
assert chunks[-1].choices[0].finish_reason == "stop"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_prompt_caching_pins_do_not_evict_router_state():
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-sonnet-4-5", "api_key": "sk-test"},
|
|
"model_info": {"id": "dep-a"},
|
|
},
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-sonnet-4-5", "api_key": "sk-test"},
|
|
"model_info": {"id": "dep-b"},
|
|
},
|
|
],
|
|
optional_pre_call_checks=["prompt_caching"],
|
|
)
|
|
router.cache.set_cache("deployment:dep-a:cooldown", True, ttl=5)
|
|
pins: Final = PromptCachingCache(cache=router.prompt_caching_pin_cache)
|
|
for index in range(DEFAULT_MAX_SIZE_IN_MEMORY):
|
|
await pins.async_add_model_id(
|
|
model_id="dep-a",
|
|
messages=[
|
|
{
|
|
"role": "system",
|
|
"content": [
|
|
{"type": "text", "text": f"policy {index}", "cache_control": {"type": "ephemeral", "ttl": "1h"}}
|
|
],
|
|
},
|
|
{"role": "user", "content": "ok"},
|
|
],
|
|
tools=None,
|
|
)
|
|
|
|
assert router.cache.get_cache("deployment:dep-a:cooldown") is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_one_hour_pins_past_the_routers_default_cache_size_keep_fresh_five_minute_pins():
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-sonnet-4-5", "api_key": "sk-test"},
|
|
"model_info": {"id": "dep-a"},
|
|
},
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-sonnet-4-5", "api_key": "sk-test"},
|
|
"model_info": {"id": "dep-b"},
|
|
},
|
|
],
|
|
optional_pre_call_checks=["prompt_caching"],
|
|
)
|
|
pins: Final = PromptCachingCache(cache=router.prompt_caching_pin_cache)
|
|
|
|
def policy(index: int, ttl: str) -> list[AllMessageValues]:
|
|
return [
|
|
{
|
|
"role": "system",
|
|
"content": [
|
|
{
|
|
"type": "text",
|
|
"text": f"policy {ttl} {index}",
|
|
"cache_control": {"type": "ephemeral", "ttl": ttl},
|
|
}
|
|
],
|
|
},
|
|
{"role": "user", "content": "ok"},
|
|
]
|
|
|
|
for index in range(DEFAULT_MAX_SIZE_IN_MEMORY):
|
|
await pins.async_add_model_id(
|
|
model_id="dep-a", messages=policy(index, "1h"), tools=None, honors_one_hour_ttl=True
|
|
)
|
|
for index in range(3):
|
|
await pins.async_add_model_id(
|
|
model_id="dep-b", messages=policy(index, "5m"), tools=None, honors_one_hour_ttl=True
|
|
)
|
|
|
|
assert [await pins.async_get_model_id(messages=policy(index, "5m"), tools=None) for index in range(3)] == [
|
|
{"model_id": "dep-b"}
|
|
] * 3
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_flush_cache_drops_prompt_caching_pins():
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-sonnet-4-5", "api_key": "sk-test"},
|
|
"model_info": {"id": "dep-a"},
|
|
},
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-sonnet-4-5", "api_key": "sk-test"},
|
|
"model_info": {"id": "dep-b"},
|
|
},
|
|
],
|
|
optional_pre_call_checks=["prompt_caching"],
|
|
)
|
|
messages: Final = [
|
|
{
|
|
"role": "system",
|
|
"content": [{"type": "text", "text": "policy", "cache_control": {"type": "ephemeral", "ttl": "1h"}}],
|
|
},
|
|
{"role": "user", "content": "ok"},
|
|
]
|
|
pins: Final = PromptCachingCache(cache=router.prompt_caching_pin_cache)
|
|
await pins.async_add_model_id(model_id="dep-a", messages=messages, tools=None)
|
|
assert await pins.async_get_model_id(messages=messages, tools=None) == {"model_id": "dep-a"}
|
|
|
|
router.flush_cache()
|
|
|
|
assert await pins.async_get_model_id(messages=messages, tools=None) is None
|
|
|
|
|
|
class _FirstPullStream:
|
|
"""A stream whose first pull yields `first`, raises it, or ends (first=None)."""
|
|
|
|
def __init__(self, first: object) -> None:
|
|
self._first = first
|
|
|
|
def _pull(self, stop: type[Exception]) -> object:
|
|
if self._first is None:
|
|
raise stop
|
|
if isinstance(self._first, Exception):
|
|
raise self._first
|
|
return self._first
|
|
|
|
def __iter__(self) -> "_FirstPullStream":
|
|
return self
|
|
|
|
def __next__(self) -> object:
|
|
return self._pull(StopIteration)
|
|
|
|
def __aiter__(self) -> "_FirstPullStream":
|
|
return self
|
|
|
|
async def __anext__(self) -> object:
|
|
return self._pull(StopAsyncIteration)
|
|
|
|
|
|
def _first_chunk_cases():
|
|
from litellm.exceptions import MidStreamFallbackError
|
|
from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
|
|
|
|
chunk = ModelResponseStream(choices=[StreamingChoices(delta=Delta(content="hi"))])
|
|
provider_error = litellm.InternalServerError(
|
|
message="primary died at stream start", llm_provider="openai", model="m"
|
|
)
|
|
wrapped = MidStreamFallbackError(
|
|
message=str(provider_error),
|
|
model="m",
|
|
llm_provider="openai",
|
|
original_exception=provider_error,
|
|
is_pre_first_chunk=True,
|
|
)
|
|
return [
|
|
pytest.param(chunk, chunk, None, id="first-chunk"),
|
|
pytest.param(None, None, None, id="empty-stream"),
|
|
pytest.param(wrapped, None, provider_error, id="pre-content-failure"),
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("first, expected, raised", _first_chunk_cases())
|
|
def test_read_first_chunk_direct_call(first, expected, raised):
|
|
from litellm.router_rust.invoke.streaming import _read_first_chunk # noqa: PLC2701
|
|
|
|
stream = _FirstPullStream(first)
|
|
if raised is None:
|
|
taken = _read_first_chunk(stream)
|
|
assert (taken.chunk, taken.rest) == (expected, stream)
|
|
return
|
|
with pytest.raises(type(raised)) as caught:
|
|
_read_first_chunk(stream)
|
|
assert caught.value is raised
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("first, expected, raised", _first_chunk_cases())
|
|
async def test_aread_first_chunk_direct_call(first, expected, raised):
|
|
from litellm.router_rust.invoke.streaming import _aread_first_chunk # noqa: PLC2701
|
|
|
|
stream = _FirstPullStream(first)
|
|
if raised is None:
|
|
taken = await _aread_first_chunk(stream)
|
|
assert (taken.chunk, taken.rest) == (expected, stream)
|
|
return
|
|
with pytest.raises(type(raised)) as caught:
|
|
await _aread_first_chunk(stream)
|
|
assert caught.value is raised
|
|
|
|
|
|
class _IterationHandedOffStream(CustomStreamWrapper):
|
|
"""A CustomStreamWrapper that, like the MCP gateway's MCPStreamWrapper, has no completion_stream of its
|
|
own and hands iteration to another object from __aiter__ / __iter__, a fresh one per call so a second
|
|
__aiter__ would replay the first chunk."""
|
|
|
|
def __init__(self, chunks: list[litellm.ModelResponseStream]) -> None:
|
|
super().__init__(completion_stream=None, model="gpt-5.4", custom_llm_provider="openai", logging_obj=MagicMock())
|
|
self._chunks: Final = chunks
|
|
|
|
def __aiter__(self):
|
|
return _AsyncList(self._chunks)
|
|
|
|
def __iter__(self):
|
|
return iter(self._chunks)
|
|
|
|
|
|
def _handed_off_chunks() -> list[litellm.ModelResponseStream]:
|
|
return [
|
|
litellm.ModelResponseStream(choices=[{"index": 0, "delta": {"content": text}}]) for text in ("tool ", "answer")
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_acompletion_streams_a_wrapper_that_hands_iteration_to_another_iterator():
|
|
"""The eager first-chunk read goes through __aiter__ like `async for` does, so a wrapper with
|
|
completion_stream=None (MCPStreamWrapper) streams instead of failing with a NoneType iterator."""
|
|
router = _mid_stream_opt_out_router()
|
|
chunks = _handed_off_chunks()
|
|
|
|
with patch("litellm.acompletion", new=AsyncMock(return_value=_IterationHandedOffStream(chunks))):
|
|
response = await router.acompletion(model="primary", messages=[{"role": "user", "content": "Hi"}], stream=True)
|
|
streamed = [chunk async for chunk in response]
|
|
|
|
assert streamed == chunks
|
|
|
|
|
|
def test_completion_streams_a_wrapper_that_hands_iteration_to_another_iterator():
|
|
"""Sync twin: Router._completion's first-chunk read goes through __iter__ like a for loop does."""
|
|
router = _mid_stream_opt_out_router()
|
|
chunks = _handed_off_chunks()
|
|
|
|
with patch("litellm.completion", return_value=_IterationHandedOffStream(chunks)):
|
|
response = router.completion(model="primary", messages=[{"role": "user", "content": "Hi"}], stream=True)
|
|
streamed = list(response)
|
|
|
|
assert streamed == chunks
|
|
|
|
|
|
_PRE_CALL_CHECK_UNPARSEABLE_MESSAGES: Final = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "image_url", "image_url": {"detail": "not-a-real-detail-value"}},
|
|
],
|
|
}
|
|
]
|
|
|
|
|
|
def test_pre_call_checks_filters_deployment_when_token_count_exceeds_limit():
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {"model": "openai/my-fake-model", "api_key": "my-fake-key"},
|
|
"model_info": {"id": "small", "max_input_tokens": 5},
|
|
},
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {"model": "openai/my-other-fake-model", "api_key": "my-fake-key"},
|
|
"model_info": {"id": "large", "max_input_tokens": 10000},
|
|
},
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
filtered: Final = router._pre_call_checks(
|
|
model="fake-openai-endpoint",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": " ".join(["word"] * 100)}],
|
|
)
|
|
|
|
assert [d["model_info"]["id"] for d in filtered] == ["large"]
|
|
|
|
|
|
def test_pre_call_checks_context_window_check_is_skipped_but_not_fatal_when_token_counting_fails():
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {"model": "openai/my-fake-model", "api_key": "my-fake-key"},
|
|
"model_info": {"max_input_tokens": 100},
|
|
},
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
filtered: Final = router._pre_call_checks(
|
|
model="fake-openai-endpoint",
|
|
healthy_deployments=model_list,
|
|
messages=_PRE_CALL_CHECK_UNPARSEABLE_MESSAGES,
|
|
)
|
|
|
|
assert len(filtered) == 1
|
|
|
|
|
|
def test_pre_call_checks_keeps_unsupported_param_filter_when_token_counting_fails(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
no_json_mode_model: Final = "gpt-3.5-turbo-16k"
|
|
assert "response_format" not in (
|
|
litellm.get_supported_openai_params(model=no_json_mode_model, custom_llm_provider="openai") or []
|
|
)
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {"model": f"openai/{deployment_model}", "api_key": "my-fake-key"},
|
|
"model_info": {"id": deployment_model, "max_input_tokens": 100},
|
|
}
|
|
for deployment_model in (no_json_mode_model, "gpt-4o")
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
filtered: Final = router._pre_call_checks(
|
|
model="fake-openai-endpoint",
|
|
healthy_deployments=model_list,
|
|
messages=_PRE_CALL_CHECK_UNPARSEABLE_MESSAGES,
|
|
request_kwargs={"response_format": {"type": "json_object"}},
|
|
)
|
|
|
|
assert [d["model_info"]["id"] for d in filtered] == ["gpt-4o"]
|
|
|
|
|
|
async def test_async_get_healthy_deployments_keeps_region_check_when_token_counting_fails():
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "fake-openai-endpoint",
|
|
"litellm_params": {"model": "openai/my-fake-model", "api_key": "my-fake-key", "region_name": region},
|
|
"model_info": {"id": f"{region}-deployment", "max_input_tokens": 100},
|
|
}
|
|
for region in ("eu", "us")
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
healthy: Final = await router.async_get_healthy_deployments(
|
|
model="fake-openai-endpoint",
|
|
request_kwargs={"allowed_model_region": "eu"},
|
|
messages=_PRE_CALL_CHECK_UNPARSEABLE_MESSAGES,
|
|
input=None,
|
|
specific_deployment=False,
|
|
parent_otel_span=None,
|
|
)
|
|
|
|
assert [deployment["model_info"]["id"] for deployment in healthy] == ["eu-deployment"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"messages",
|
|
[_PRE_CALL_CHECK_UNPARSEABLE_MESSAGES, [{"role": "user", "content": "hi"}]],
|
|
ids=["uncountable", "countable"],
|
|
)
|
|
def test_completion_answers_400_when_no_deployment_supports_the_requested_param(
|
|
monkeypatch: pytest.MonkeyPatch, messages: list[dict[str, object]]
|
|
):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "no-json-mode",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo-16k", "api_key": "my-fake-key"},
|
|
"model_info": {"max_input_tokens": 100},
|
|
}
|
|
],
|
|
enable_pre_call_checks=True,
|
|
num_retries=2,
|
|
)
|
|
|
|
with pytest.raises(litellm.UnsupportedParamsError) as error:
|
|
router.completion(
|
|
model="no-json-mode",
|
|
messages=messages,
|
|
response_format={"type": "json_object"},
|
|
mock_response="should not be served",
|
|
)
|
|
|
|
assert error.value.status_code == 400
|
|
assert "allowed_openai_params" in str(error.value)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("deployment_params", "request_kwargs"),
|
|
[
|
|
({}, {"allowed_openai_params": ["response_format"]}),
|
|
({}, {"drop_params": True}),
|
|
({}, {"additional_drop_params": ["response_format"]}),
|
|
({"allowed_openai_params": ["response_format"]}, {}),
|
|
({"drop_params": True}, {}),
|
|
],
|
|
ids=["request-allowlist", "request-drop", "request-additional-drop", "deployment-allowlist", "deployment-drop"],
|
|
)
|
|
def test_pre_call_checks_keeps_deployment_whose_unsupported_param_is_allowed_or_dropped(
|
|
monkeypatch: pytest.MonkeyPatch, deployment_params: Mapping[str, object], request_kwargs: Mapping[str, object]
|
|
):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "no-json-mode",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo-16k", "api_key": "my-fake-key", **deployment_params},
|
|
"model_info": {"id": "only", "max_input_tokens": 100},
|
|
}
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
filtered: Final = router._pre_call_checks(
|
|
model="no-json-mode",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
request_kwargs={"response_format": {"type": "json_object"}, **request_kwargs},
|
|
)
|
|
|
|
assert [d["model_info"]["id"] for d in filtered] == ["only"]
|
|
|
|
|
|
def test_completion_uses_context_window_fallback_when_the_small_deployment_also_lacks_the_param(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "small",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo-16k", "api_key": "my-fake-key"},
|
|
"model_info": {"id": "small-id", "max_input_tokens": 5},
|
|
},
|
|
{
|
|
"model_name": "big",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "my-fake-key"},
|
|
"model_info": {"id": "big-id"},
|
|
},
|
|
],
|
|
enable_pre_call_checks=True,
|
|
context_window_fallbacks=[{"small": ["big"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="small",
|
|
messages=[{"role": "user", "content": "hi " * 50}],
|
|
response_format={"type": "json_object"},
|
|
mock_response="served by the fallback",
|
|
)
|
|
|
|
assert response._hidden_params["model_id"] == "big-id"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("deployment_params", "request_kwargs"),
|
|
[
|
|
({}, {"allowed_openai_params": ["response_format"]}),
|
|
({}, {"drop_params": True}),
|
|
({}, {"additional_drop_params": ["response_format"]}),
|
|
({"drop_params": True}, {}),
|
|
({"allowed_openai_params": ["response_format"]}, {}),
|
|
],
|
|
ids=["request-allowlist", "request-drop", "request-additional-drop", "deployment-drop", "deployment-allowlist"],
|
|
)
|
|
def test_pre_call_checks_prefers_a_deployment_that_takes_the_param_over_dropping_it(
|
|
monkeypatch: pytest.MonkeyPatch, deployment_params: Mapping[str, object], request_kwargs: Mapping[str, object]
|
|
):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "mixed-json-mode",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo-16k", "api_key": "my-fake-key", **deployment_params},
|
|
"model_info": {"id": "gpt-3.5-turbo-16k", "max_input_tokens": 100},
|
|
},
|
|
{
|
|
"model_name": "mixed-json-mode",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "my-fake-key"},
|
|
"model_info": {"id": "gpt-4o", "max_input_tokens": 100},
|
|
},
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
filtered: Final = router._pre_call_checks(
|
|
model="mixed-json-mode",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
request_kwargs={"response_format": {"type": "json_object"}, **request_kwargs},
|
|
)
|
|
|
|
assert [d["model_info"]["id"] for d in filtered] == ["gpt-4o"]
|
|
|
|
|
|
def test_pre_call_checks_drops_the_param_when_the_capable_deployment_is_outside_the_region(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "regional-json-mode",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo-16k", "api_key": "my-fake-key", "region_name": "eu"},
|
|
"model_info": {"id": "eu-without-json-mode", "max_input_tokens": 100},
|
|
},
|
|
{
|
|
"model_name": "regional-json-mode",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "my-fake-key", "region_name": "us"},
|
|
"model_info": {"id": "us-with-json-mode", "max_input_tokens": 100},
|
|
},
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
filtered: Final = router._pre_call_checks(
|
|
model="regional-json-mode",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
request_kwargs={"response_format": {"type": "json_object"}, "drop_params": True, "allowed_model_region": "eu"},
|
|
)
|
|
|
|
assert [d["model_info"]["id"] for d in filtered] == ["eu-without-json-mode"]
|
|
|
|
|
|
def test_pre_call_checks_filters_an_authenticating_provider_deployment_that_lacks_the_param(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path))
|
|
(tmp_path / "api-key.json").write_text(json.dumps({"token": "tid=test", "expires_at": 4102444800}))
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "mixed-json-mode",
|
|
"litellm_params": {"model": "github_copilot/gpt-4"},
|
|
"model_info": {"id": "copilot-gpt-4", "max_input_tokens": 100},
|
|
},
|
|
{
|
|
"model_name": "mixed-json-mode",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "my-fake-key"},
|
|
"model_info": {"id": "gpt-4o", "max_input_tokens": 100},
|
|
},
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
filtered: Final = router._pre_call_checks(
|
|
model="mixed-json-mode",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
request_kwargs={"response_format": {"type": "json_object"}},
|
|
)
|
|
|
|
assert [d["model_info"]["id"] for d in filtered] == ["gpt-4o"]
|
|
|
|
|
|
def test_pre_call_checks_answers_400_when_the_group_name_lists_no_deployments(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "no-json-mode",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo-16k", "api_key": "my-fake-key"},
|
|
"model_info": {"id": "only", "max_input_tokens": 100},
|
|
}
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
with pytest.raises(litellm.UnsupportedParamsError):
|
|
router._pre_call_checks(
|
|
model="team-public-name",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
request_kwargs={"response_format": {"type": "json_object"}},
|
|
)
|
|
|
|
|
|
def test_pre_call_checks_lets_a_request_setting_replace_the_deployment_setting(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "no-json-mode",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo-16k", "api_key": "my-fake-key", "drop_params": True},
|
|
"model_info": {"id": "drops-it", "max_input_tokens": 100},
|
|
},
|
|
{
|
|
"model_name": "no-json-mode",
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4",
|
|
"api_key": "my-fake-key",
|
|
"allowed_openai_params": ["response_format"],
|
|
},
|
|
"model_info": {"id": "allows-it", "max_input_tokens": 100},
|
|
},
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
filtered: Final = router._pre_call_checks(
|
|
model="no-json-mode",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
request_kwargs={"response_format": {"type": "json_object"}, "drop_params": False},
|
|
)
|
|
|
|
assert [d["model_info"]["id"] for d in filtered] == ["allows-it"]
|
|
|
|
|
|
async def _select_team_json_deployment(router: Router, entrypoint: str, selection: Mapping[str, object]) -> object:
|
|
if entrypoint == "async":
|
|
return await router.async_get_available_deployment(**selection)
|
|
if entrypoint == "sync":
|
|
return router.get_available_deployment(**selection)
|
|
return router.get_available_deployment_for_pass_through(**selection)
|
|
|
|
|
|
@pytest.mark.parametrize("entrypoint", ["sync", "async", "pass-through"])
|
|
@pytest.mark.parametrize(
|
|
("deployment_models", "cooled_down", "expected_error"),
|
|
[
|
|
pytest.param(("gpt-3.5-turbo-16k", "gpt-4"), (), litellm.UnsupportedParamsError, id="none-takes-it"),
|
|
pytest.param(("gpt-3.5-turbo-16k", "gpt-4o"), ("gpt-4o",), RouterRateLimitError, id="capable-cooling-down"),
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_team_public_model_name_answers_400_only_when_no_team_deployment_takes_the_param(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
entrypoint: str,
|
|
deployment_models: tuple[str, str],
|
|
cooled_down: tuple[str, ...],
|
|
expected_error: type[Exception],
|
|
):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": f"team-json-{deployment_model}",
|
|
"litellm_params": {
|
|
"model": f"openai/{deployment_model}",
|
|
"api_key": "my-fake-key",
|
|
"use_in_pass_through": True,
|
|
},
|
|
"model_info": {
|
|
"id": deployment_model,
|
|
"max_input_tokens": 100,
|
|
"team_id": "json-team",
|
|
"team_public_model_name": "team-json",
|
|
},
|
|
}
|
|
for deployment_model in deployment_models
|
|
],
|
|
enable_pre_call_checks=True,
|
|
num_retries=0,
|
|
)
|
|
_cool_down(router, *cooled_down)
|
|
selection: Final = {
|
|
"model": "team-json",
|
|
"messages": [{"role": "user", "content": "hi"}],
|
|
"request_kwargs": {
|
|
"metadata": {"user_api_key_team_id": "json-team"},
|
|
"response_format": {"type": "json_object"},
|
|
},
|
|
}
|
|
|
|
with pytest.raises(expected_error):
|
|
await _select_team_json_deployment(router, entrypoint, selection)
|
|
|
|
|
|
def test_pre_call_checks_ignores_an_image_response_format_string(monkeypatch: pytest.MonkeyPatch):
|
|
monkeypatch.setattr(litellm, "drop_params", False)
|
|
model_list: Final = [
|
|
{
|
|
"model_name": "image-model",
|
|
"litellm_params": {"model": "openai/gpt-3.5-turbo-16k", "api_key": "my-fake-key"},
|
|
"model_info": {"id": "only", "max_input_tokens": 100},
|
|
}
|
|
]
|
|
router: Final = Router(model_list=model_list, enable_pre_call_checks=True, num_retries=0)
|
|
|
|
filtered: Final = router._pre_call_checks(
|
|
model="image-model",
|
|
healthy_deployments=model_list,
|
|
messages=[{"role": "user", "content": "prompt"}],
|
|
request_kwargs={"response_format": "b64_json"},
|
|
)
|
|
|
|
assert [d["model_info"]["id"] for d in filtered] == ["only"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("deployment_stream_options", "default_stream_options", "request_stream_options", "usage_chunk_choice_count"),
|
|
[
|
|
({"include_usage": True}, None, None, 1),
|
|
(None, {"include_usage": True}, None, 1),
|
|
({"include_usage": True}, None, {"include_usage": True}, 0),
|
|
(None, None, {"include_usage": True}, 0),
|
|
],
|
|
ids=["deployment-config", "router-default", "request-over-deployment", "request-only"],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_router_configured_stream_usage_keeps_a_choice_on_the_usage_chunk(
|
|
deployment_stream_options: dict[str, bool] | None,
|
|
default_stream_options: dict[str, bool] | None,
|
|
request_stream_options: dict[str, bool] | None,
|
|
usage_chunk_choice_count: int,
|
|
):
|
|
deployment_params: Final = {"model": "openai/gpt-5-mini", "api_key": "fake", "mock_response": "hi there"}
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "m",
|
|
"litellm_params": (
|
|
deployment_params
|
|
if deployment_stream_options is None
|
|
else {**deployment_params, "stream_options": deployment_stream_options}
|
|
),
|
|
}
|
|
],
|
|
default_litellm_params=None if default_stream_options is None else {"stream_options": default_stream_options},
|
|
)
|
|
request_kwargs: Final = {} if request_stream_options is None else {"stream_options": request_stream_options}
|
|
|
|
response: Final = await router.acompletion(
|
|
model="m", messages=[{"role": "user", "content": "hi"}], stream=True, **request_kwargs
|
|
)
|
|
chunks: Final = [chunk async for chunk in response]
|
|
|
|
assert getattr(chunks[-1], "usage", None) is not None
|
|
assert len(chunks[-1].choices) == usage_chunk_choice_count
|
|
assert all(len(chunk.choices) == 1 for chunk in chunks[:-1])
|
|
|
|
|
|
def _concurrent_delete_deployment(index: int) -> dict[str, object]:
|
|
return {
|
|
"model_name": f"model-{index}",
|
|
"litellm_params": {"model": f"openai/gpt-{index}", "api_key": "k"},
|
|
"model_info": {
|
|
"id": f"deployment-{index}",
|
|
"team_id": "team-1",
|
|
"team_public_model_name": "public-shared" if index >= 2 else f"public-{index}",
|
|
},
|
|
}
|
|
|
|
|
|
def _concurrent_delete_router() -> Router:
|
|
return Router(model_list=[_concurrent_delete_deployment(index) for index in range(4)])
|
|
|
|
|
|
def test_delete_of_an_unknown_deployment_returns_none_and_keeps_the_list() -> None:
|
|
router: Final = _concurrent_delete_router()
|
|
|
|
assert router.delete_deployment("deployment-9") is None
|
|
assert router.get_model_ids() == [f"deployment-{index}" for index in range(4)]
|
|
|
|
|
|
def test_get_model_ids_by_name_skips_team_deployments_only_when_asked() -> None:
|
|
shared: Final = {"model_name": "shared", "litellm_params": {"model": "openai/gpt-shared", "api_key": "k"}}
|
|
router: Final = Router(
|
|
model_list=[
|
|
{**shared, "model_info": {"id": "global"}},
|
|
{**shared, "model_info": {"id": "team", "team_id": "team-1", "team_public_model_name": "public"}},
|
|
]
|
|
)
|
|
|
|
assert router.get_model_ids("shared", exclude_team_models=True) == ["global"]
|
|
assert router.get_model_ids("shared") == ["global", "team"]
|
|
|
|
|
|
_IMAGE_PART = {"type": "image_url", "image_url": {"url": "data:image/png;base64,iVBORw0KGgo="}}
|
|
_IMAGE_CHAT = [{"role": "user", "content": [{"type": "text", "text": "What is in this image?"}, _IMAGE_PART]}]
|
|
_IMAGE_RESPONSES_INPUT = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "input_text", "text": "What is in this image?"},
|
|
{"type": "input_image", "image_url": "data:image/png;base64,iVBORw0KGgo="},
|
|
],
|
|
}
|
|
]
|
|
_VISION_LEG: Final[dict[str, object]] = {
|
|
"litellm_params": {"model": "openai/gpt-5.6-terra"},
|
|
"model_info": {"id": "vision"},
|
|
}
|
|
_DECLARED_TEXT_LEG: Final[dict[str, object]] = {
|
|
"litellm_params": {"model": "bedrock/openai.gpt-oss-120b-1:0"},
|
|
"model_info": {"id": "declared-text", "supports_vision": False},
|
|
}
|
|
|
|
|
|
def _mixed_router(*deployments: dict[str, object], routing_strategy: str = "latency-based-routing") -> litellm.Router:
|
|
return litellm.Router(
|
|
model_list=[{"model_name": "mixed", **deployment} for deployment in deployments],
|
|
routing_strategy=routing_strategy,
|
|
)
|
|
|
|
|
|
def test_router_picks_the_vision_leg_for_image_requests_without_enable_pre_call_checks():
|
|
router: Final = _mixed_router(_VISION_LEG, _DECLARED_TEXT_LEG)
|
|
assert router.enable_pre_call_checks is False
|
|
for _ in range(5):
|
|
assert router.get_available_deployment(model="mixed", messages=_IMAGE_CHAT)["model_info"]["id"] == "vision"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_router_picks_the_vision_leg_for_responses_images_without_enable_pre_call_checks():
|
|
router: Final = _mixed_router(_VISION_LEG, _DECLARED_TEXT_LEG)
|
|
for _ in range(5):
|
|
deployment: Final = await router.async_get_available_deployment(
|
|
model="mixed", request_kwargs={"input": _IMAGE_RESPONSES_INPUT}
|
|
)
|
|
assert deployment["model_info"]["id"] == "vision"
|
|
|
|
|
|
def test_router_sends_an_image_to_the_text_only_leg_when_pre_call_checks_drop_the_vision_leg():
|
|
router: Final = litellm.Router(
|
|
model_list=[
|
|
{"model_name": "mixed", **_VISION_LEG, "model_info": {"id": "vision", "max_input_tokens": 1}},
|
|
{"model_name": "mixed", **_DECLARED_TEXT_LEG},
|
|
],
|
|
routing_strategy="latency-based-routing",
|
|
enable_pre_call_checks=True,
|
|
)
|
|
assert router.get_available_deployment(model="mixed", messages=_IMAGE_CHAT)["model_info"]["id"] == "declared-text"
|
|
|
|
|
|
def test_router_sends_an_image_to_the_text_only_leg_when_the_vision_leg_is_cooling_down():
|
|
router: Final = _mixed_router(_VISION_LEG, _DECLARED_TEXT_LEG)
|
|
router.cooldown_cache.add_deployment_to_cooldown(
|
|
model_id="vision", original_exception=Exception("bad key"), exception_status=401, cooldown_time=60
|
|
)
|
|
assert router.get_available_deployment(model="mixed", messages=_IMAGE_CHAT)["model_info"]["id"] == "declared-text"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("isolate_litellm_state")
|
|
@pytest.mark.parametrize("surface", ["chat", "messages"])
|
|
@pytest.mark.parametrize("credential_kind", ["oauth", "api_key"])
|
|
async def test_anthropic_caller_rate_limit_does_not_block_other_credentials(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch, surface: str, credential_kind: str
|
|
) -> None:
|
|
from litellm.llms.anthropic.messages.native import handler as python_messages
|
|
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
|
|
from litellm.types.router import AllowedFailsPolicy
|
|
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
monkeypatch.setattr("time.time", lambda: 1767225600.0)
|
|
monkeypatch.setattr(litellm, "anthropic_messages", python_messages.anthropic_messages)
|
|
|
|
def upstream(request: httpx.Request) -> httpx.Response:
|
|
credential: Final = request.headers.get("authorization", request.headers.get("x-api-key", ""))
|
|
if credential.endswith("limited") and route.call_count < 2:
|
|
return httpx.Response(
|
|
429,
|
|
headers={"retry-after": "168307"},
|
|
json={"type": "error", "error": {"type": "rate_limit_error", "message": "usage limit"}},
|
|
)
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "msg-healthy",
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"model": "claude-sonnet-5-5",
|
|
"content": [{"type": "text", "text": "healthy account"}],
|
|
"stop_reason": "end_turn",
|
|
"usage": {"input_tokens": 2, "output_tokens": 2},
|
|
},
|
|
)
|
|
|
|
route: Final = respx_mock.post("https://api.anthropic.com/v1/messages").mock(side_effect=upstream)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-sonnet-5-5",
|
|
"api_key": "fake-gateway-key",
|
|
},
|
|
"model_info": {"id": "shared-anthropic"},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
allowed_fails_policy=AllowedFailsPolicy(RateLimitErrorAllowedFails=1),
|
|
)
|
|
client: Final = AsyncHTTPHandler(transport=httpx.AsyncHTTPTransport())
|
|
|
|
async def call(account: str | None) -> object:
|
|
credentials: Final = (
|
|
(
|
|
{
|
|
"provider_specific_header": {
|
|
"custom_llm_provider": "anthropic",
|
|
"extra_headers": {
|
|
"Authorization": f"Bearer sk-ant-oat-fake-{account}",
|
|
},
|
|
}
|
|
}
|
|
if credential_kind == "oauth"
|
|
else {"api_key": f"fake-{account}"}
|
|
)
|
|
if account is not None
|
|
else {}
|
|
)
|
|
if surface == "messages":
|
|
max_tokens: Final = litellm.get_max_tokens("anthropic/claude-sonnet-5-5")
|
|
assert max_tokens is not None
|
|
return await router.aanthropic_messages(
|
|
model="claude",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
client=client,
|
|
max_tokens=max_tokens,
|
|
**credentials,
|
|
)
|
|
return await router.acompletion(
|
|
model="claude",
|
|
messages=[{"role": "user", "content": "hello"}],
|
|
client=client,
|
|
**credentials,
|
|
)
|
|
|
|
try:
|
|
for _ in range(2):
|
|
with pytest.raises(litellm.RateLimitError):
|
|
await call("limited")
|
|
await GLOBAL_LOGGING_WORKER.flush()
|
|
limited_calls: Final = len(route.calls)
|
|
assert limited_calls == 2
|
|
await call("healthy")
|
|
assert len(route.calls) == limited_calls + 1
|
|
assert route.calls[-1].request.headers.get(
|
|
"authorization", route.calls[-1].request.headers.get("x-api-key", "")
|
|
) == ("Bearer sk-ant-oat-fake-healthy" if credential_kind == "oauth" else "fake-healthy")
|
|
await call(None)
|
|
assert len(route.calls) == limited_calls + 2
|
|
assert route.calls[-1].request.headers["x-api-key"] == "fake-gateway-key"
|
|
await call("limited")
|
|
assert len(route.calls) == limited_calls + 3
|
|
finally:
|
|
await GLOBAL_LOGGING_WORKER.flush()
|
|
await client.close()
|
|
|
|
|
|
_FALLBACK_VALIDATION_MODEL_LIST: Final = (
|
|
{"model_name": "gpt-4", "litellm_params": {"model": "openai/gpt-4", "api_key": "fake-key"}},
|
|
{"model_name": "gpt-3.5", "litellm_params": {"model": "openai/gpt-3.5-turbo", "api_key": "fake-key"}},
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("router_kwargs", "expected_message"),
|
|
[
|
|
({"fallbacks": [{"gpt-4": "gpt-3.5"}]}, "Fallbacks for 'gpt-4' must be a list of model names, got 'gpt-3.5'"),
|
|
({"default_fallbacks": "gpt-3.5"}, "default_fallbacks must be a list of model names, got 'gpt-3.5'"),
|
|
(
|
|
{"content_policy_fallbacks": [{"gpt-4": "gpt-3.5"}]},
|
|
"Fallbacks for 'gpt-4' must be a list of model names, got 'gpt-3.5'",
|
|
),
|
|
],
|
|
ids=["string_fallback_target", "string_default_fallbacks", "string_content_policy_fallback_target"],
|
|
)
|
|
def test_router_rejects_a_string_fallback_target_at_init(
|
|
router_kwargs: Mapping[str, object], expected_message: str
|
|
) -> None:
|
|
with pytest.raises(TypeError, match="must be a list of model names") as excinfo:
|
|
Router(model_list=list(_FALLBACK_VALIDATION_MODEL_LIST), **router_kwargs)
|
|
|
|
assert expected_message in str(excinfo.value)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("router_kwargs", "expected_fallbacks"),
|
|
[
|
|
({"fallbacks": [{"gpt-4": ["gpt-3.5"]}]}, [{"gpt-4": ["gpt-3.5"]}]),
|
|
({"default_fallbacks": ["gpt-3.5"]}, [{"*": ["gpt-3.5"]}]),
|
|
(
|
|
{"fallbacks": [{"gpt-4": [{"model": "gpt-3.5", "api_key": "other-key"}]}]},
|
|
[{"gpt-4": [{"model": "gpt-3.5", "api_key": "other-key"}]}],
|
|
),
|
|
({"fallbacks": [{"model": "gpt-3.5"}]}, [{"model": "gpt-3.5"}]),
|
|
],
|
|
ids=["list_fallback_target", "list_default_fallbacks", "dict_override_fallback_target", "direct_target_entry"],
|
|
)
|
|
def test_router_keeps_supported_fallback_shapes(
|
|
router_kwargs: Mapping[str, object], expected_fallbacks: list[dict[str, object]]
|
|
) -> None:
|
|
router: Final = Router(model_list=list(_FALLBACK_VALIDATION_MODEL_LIST), **router_kwargs)
|
|
|
|
assert router.fallbacks == expected_fallbacks
|
|
|
|
|
|
_SAGEMAKER_ROUTER_CREDENTIALS: Final = MappingProxyType(
|
|
{"aws_region_name": "us-east-1", "aws_access_key_id": "test-key", "aws_secret_access_key": "test-secret"}
|
|
)
|
|
|
|
_PINNED_COMPONENT: Final = MappingProxyType({"model_id": "deployment-component"})
|
|
_HEADER_PINNED_COMPONENT: Final = MappingProxyType(
|
|
{"extra_headers": {"x-amzn-sagemaker-inference-component": "deployment-component"}}
|
|
)
|
|
|
|
_INFERENCE_COMPONENT_PRECEDENCE: Final = pytest.mark.parametrize(
|
|
("deployment_params", "sent_component"),
|
|
[
|
|
(_PINNED_COMPONENT, "deployment-component"),
|
|
(_HEADER_PINNED_COMPONENT, "deployment-component"),
|
|
(MappingProxyType({}), "client-component"),
|
|
(
|
|
MappingProxyType({**_PINNED_COMPONENT, "configurable_clientside_auth_params": ["model_id"]}),
|
|
"client-component",
|
|
),
|
|
],
|
|
ids=[
|
|
"deployment_pins_the_component",
|
|
"deployment_pins_the_component_with_a_header",
|
|
"deployment_leaves_the_component_to_the_client",
|
|
"deployment_lets_the_client_override_its_component",
|
|
],
|
|
)
|
|
|
|
|
|
def _sagemaker_router(
|
|
model: str,
|
|
deployment_params: Mapping[str, object],
|
|
mode: str,
|
|
router_defaults: Mapping[str, str] = MappingProxyType({}),
|
|
model_name: str = "sagemaker-model",
|
|
) -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": model_name,
|
|
"litellm_params": {"model": model, **_SAGEMAKER_ROUTER_CREDENTIALS, **deployment_params},
|
|
"model_info": {"mode": mode},
|
|
}
|
|
],
|
|
default_litellm_params=dict(router_defaults),
|
|
)
|
|
|
|
|
|
_CHAT_COMPLETION_REPLY: Final = MappingProxyType(
|
|
{
|
|
"id": "chatcmpl-1",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": "served-model",
|
|
"choices": [{"index": 0, "message": {"role": "assistant", "content": "hi"}, "finish_reason": "stop"}],
|
|
"usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2},
|
|
}
|
|
)
|
|
|
|
|
|
def _sagemaker_chat_route(respx_mock: respx.MockRouter) -> respx.Route:
|
|
return respx_mock.post(
|
|
"https://runtime.sagemaker.us-east-1.amazonaws.com/endpoints/chat-endpoint/invocations"
|
|
).respond(json=dict(_CHAT_COMPLETION_REPLY))
|
|
|
|
|
|
@_INFERENCE_COMPONENT_PRECEDENCE
|
|
@pytest.mark.asyncio
|
|
async def test_a_client_model_id_cannot_move_a_sagemaker_chat_call_off_the_deployments_component(
|
|
respx_mock: respx.MockRouter,
|
|
_router_httpx_transport: None,
|
|
deployment_params: Mapping[str, object],
|
|
sent_component: str,
|
|
) -> None:
|
|
route: Final = _sagemaker_chat_route(respx_mock)
|
|
router: Final = _sagemaker_router("sagemaker_chat/chat-endpoint", deployment_params, "chat")
|
|
|
|
await router.acompletion(
|
|
model="sagemaker-model", messages=[{"role": "user", "content": "hi"}], model_id="client-component"
|
|
)
|
|
|
|
assert route.calls.last.request.headers["X-Amzn-SageMaker-Inference-Component"] == sent_component
|
|
|
|
|
|
@_INFERENCE_COMPONENT_PRECEDENCE
|
|
def test_a_client_model_id_cannot_move_a_sync_sagemaker_text_completion_off_the_deployments_component(
|
|
respx_mock: respx.MockRouter, deployment_params: Mapping[str, object], sent_component: str
|
|
) -> None:
|
|
route: Final = _sagemaker_chat_route(respx_mock)
|
|
router: Final = _sagemaker_router(
|
|
"sagemaker_chat/chat-endpoint", deployment_params, "completion", model_name="sagemaker_chat/chat-endpoint"
|
|
)
|
|
|
|
router.text_completion(model="sagemaker_chat/chat-endpoint", prompt="hi", model_id="client-component")
|
|
|
|
assert route.calls.last.request.headers["X-Amzn-SageMaker-Inference-Component"] == sent_component
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"client_header",
|
|
[
|
|
MappingProxyType({"extra_headers": {"x-amzn-sagemaker-inference-component": "client-component"}}),
|
|
MappingProxyType({"headers": {"x-amzn-sagemaker-inference-component": "client-component"}}),
|
|
MappingProxyType(
|
|
{
|
|
"provider_specific_header": {
|
|
"custom_llm_provider": "sagemaker_chat",
|
|
"extra_headers": {"x-amzn-sagemaker-inference-component": "client-component"},
|
|
}
|
|
}
|
|
),
|
|
],
|
|
ids=["extra_headers", "headers", "provider_specific_header"],
|
|
)
|
|
@pytest.mark.parametrize(
|
|
"deployment_pin", [_PINNED_COMPONENT, _HEADER_PINNED_COMPONENT], ids=["model_id_pin", "header_pin"]
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_a_client_inference_component_header_cannot_move_a_pinned_sagemaker_chat_call(
|
|
respx_mock: respx.MockRouter,
|
|
_router_httpx_transport: None,
|
|
client_header: Mapping[str, object],
|
|
deployment_pin: Mapping[str, object],
|
|
) -> None:
|
|
route: Final = _sagemaker_chat_route(respx_mock)
|
|
router: Final = _sagemaker_router("sagemaker_chat/chat-endpoint", deployment_pin, "chat")
|
|
|
|
await router.acompletion(model="sagemaker-model", messages=[{"role": "user", "content": "hi"}], **client_header)
|
|
|
|
assert route.calls.last.request.headers["X-Amzn-SageMaker-Inference-Component"] == "deployment-component"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"request_component",
|
|
[MappingProxyType({}), MappingProxyType({"model_id": "deployment-component"})],
|
|
ids=["request_without_model_id", "request_with_the_pinned_model_id"],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_a_router_default_model_id_cannot_move_a_sagemaker_call_off_the_deployments_component(
|
|
respx_mock: respx.MockRouter, _router_httpx_transport: None, request_component: Mapping[str, str]
|
|
) -> None:
|
|
route: Final = _sagemaker_chat_route(respx_mock)
|
|
router: Final = _sagemaker_router(
|
|
"sagemaker_chat/chat-endpoint",
|
|
_PINNED_COMPONENT,
|
|
"chat",
|
|
router_defaults=MappingProxyType({"model_id": "default-component"}),
|
|
)
|
|
|
|
await router.acompletion(model="sagemaker-model", messages=[{"role": "user", "content": "hi"}], **request_component)
|
|
|
|
assert route.calls.last.request.headers["X-Amzn-SageMaker-Inference-Component"] == "deployment-component"
|
|
|
|
|
|
@_INFERENCE_COMPONENT_PRECEDENCE
|
|
@pytest.mark.asyncio
|
|
async def test_a_client_model_id_cannot_move_a_sagemaker_embedding_call_off_the_deployments_component(
|
|
deployment_params: Mapping[str, object], sent_component: str
|
|
) -> None:
|
|
component_embeddings: Final = MappingProxyType({"deployment-component": [0.1], "client-component": [0.2]})
|
|
|
|
def invoke_endpoint(**request: str | bytes) -> Mapping[str, io.BytesIO]:
|
|
served: Final = component_embeddings[str(request["InferenceComponentName"])]
|
|
return {
|
|
"Body": io.BytesIO(
|
|
json.dumps(
|
|
{"object": "list", "data": [{"object": "embedding", "index": 0, "embedding": served}]}
|
|
).encode()
|
|
)
|
|
}
|
|
|
|
client: Final = MagicMock(invoke_endpoint=invoke_endpoint)
|
|
router: Final = _sagemaker_router("sagemaker/openai/embed-endpoint", deployment_params, "embedding")
|
|
|
|
with patch("boto3.Session", return_value=MagicMock(client=MagicMock(return_value=client))):
|
|
response: Final = await router.aembedding(model="sagemaker-model", input=["hello"], model_id="client-component")
|
|
|
|
assert response.data[0]["embedding"] == component_embeddings[sent_component]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_header_pin_holds_when_the_deployment_names_sagemaker_through_custom_llm_provider(
|
|
respx_mock: respx.MockRouter, _router_httpx_transport: None
|
|
) -> None:
|
|
route: Final = _sagemaker_chat_route(respx_mock)
|
|
router: Final = _sagemaker_router(
|
|
"chat-endpoint", MappingProxyType({**_HEADER_PINNED_COMPONENT, "custom_llm_provider": "sagemaker_chat"}), "chat"
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="sagemaker-model", messages=[{"role": "user", "content": "hi"}], model_id="client-component"
|
|
)
|
|
|
|
assert route.calls.last.request.headers["X-Amzn-SageMaker-Inference-Component"] == "deployment-component"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_a_component_header_on_a_non_sagemaker_deployment_leaves_the_clients_model_id_alone(
|
|
respx_mock: respx.MockRouter, _router_httpx_transport: None
|
|
) -> None:
|
|
route: Final = respx_mock.post("https://gateway.local/v1/chat/completions").respond(
|
|
json=dict(_CHAT_COMPLETION_REPLY)
|
|
)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gateway-model",
|
|
"litellm_params": {
|
|
"model": "openai/served-model",
|
|
"api_base": "https://gateway.local/v1",
|
|
"api_key": "test-key",
|
|
"extra_headers": {"X-Amzn-SageMaker-Inference-Component": "gateway-component"},
|
|
},
|
|
}
|
|
]
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="gateway-model", messages=[{"role": "user", "content": "hi"}], model_id="client-value"
|
|
)
|
|
|
|
assert json.loads(route.calls.last.request.content)["model_id"] == "client-value"
|
|
|
|
|
|
def test_router_warns_when_a_deployment_enforces_endpoints_without_a_list(caplog: pytest.LogCaptureFixture) -> None:
|
|
with caplog.at_level(logging.WARNING, logger="LiteLLM Router"):
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "embed-from-config",
|
|
"litellm_params": {"model": "openai/text-embedding-3-small", "api_key": "sk-fake"},
|
|
"model_info": {"enforce_supported_endpoints": True},
|
|
}
|
|
]
|
|
)
|
|
router.add_deployment(
|
|
Deployment(
|
|
model_name="embed-from-api",
|
|
litellm_params=LiteLLM_Params(model="openai/text-embedding-3-small", api_key="sk-fake"),
|
|
model_info=ModelInfo(enforce_supported_endpoints=True, supported_endpoints=["/v1/embeddings", 7]),
|
|
)
|
|
)
|
|
|
|
assert "Deployment 'embed-from-config' sets enforce_supported_endpoints without" in caplog.text
|
|
assert "Deployment 'embed-from-api' sets enforce_supported_endpoints without" in caplog.text
|
|
|
|
|
|
def test_registry_finds_a_team_deployment_by_public_and_internal_name_for_its_team_only() -> None:
|
|
internal_name: Final = "model_name_team-a_row"
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": internal_name,
|
|
"litellm_params": {"model": "openai/gpt-5.5", "api_key": "x"},
|
|
"model_info": {"id": "team-row", "team_id": "team-a", "team_public_model_name": "public-chat"},
|
|
}
|
|
]
|
|
)
|
|
registry: Final = router.registry()
|
|
|
|
assert [row["model_info"]["id"] for row in registry.deployments("public-chat", team_id="team-a")] == ["team-row"]
|
|
assert [row["model_info"]["id"] for row in registry.deployments(internal_name, team_id="team-a")] == ["team-row"]
|
|
assert list(registry.deployments("public-chat", team_id="team-b")) == []
|
|
assert list(registry.deployments("public-chat")) == []
|