mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-03 02:22:24 +00:00
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ci: rename fork-flag to unit-flag now that it applies on every event * test: move tests/test_litellm root and small trees into tests/unit Pure renames, no content changes. Follow-up commits in this PR fix references, merge the three files that already existed in tests/unit, keep live-provider tests in tests/test_litellm and wire CI. * test: carry tests/test_litellm conftest isolation into tests/unit Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS, proxy-URL and keychain env, and session-end client cleanup now reset for unit tests too. The environment isolation owns its MonkeyPatch so a test's own monkeypatch is undone before the model-cost teardown runs. * test: merge, split and prune the moved root and small-tree tests Merge batches/test_batch_utils.py and the chat_completions and messages dispatch tests into the files that already existed in tests/unit. Keep the live Gemini interactions tests, the async image-fetch format test and the OpenAI embedding scorer test in tests/test_litellm since they need real network or keys. Put test_router.py under tests/unit/test_router so the existing package no longer shadows it. Delete eight tests the audit found superseded by stronger ones kept in this move. * ci: run the moved root and small-tree tests under their legacy flags Add the misc and responses-caching-types flags to unit_selection.sh and CircleCI, extend enterprise-routing and mcp-integration, and point the legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest and change classifier at the new paths. * test: make the new tests/unit directories packages tests/unit/test_package_layout.py requires every directory to carry an __init__.py, and without one the moved and retained test_litellm_responses_bridge.py modules collide on import. * test: scope the unit socket block to tests/unit in shared sessions The GHA shards collect the legacy test-path and the unit selection in one pytest session. The unit conftest's loopback-only block leaked into legacy modules that reach the network at import. The legacy conftest now lifts the restriction at collect and setup time, and the unit conftest re-applies it when collecting its own modules. * test: move tests/test_litellm/llms into tests/unit/llms Rename-only. Moves the provider tests and the fine-tuning fixtures they load, mirroring the old paths. Follow-up commits merge, split and wire them. * test: merge, split and prune the moved llms tests Merges the Databricks chat transformation tests into the existing unit file, keeps the tests that need real keys or the network in tests/test_litellm, deletes the audited tests a stronger unit test already covers, and points imports at tests.unit.llms. * ci: run the moved llms tests under their legacy flags The Vertex AI and All Other Providers shards keep their legacy test-path for the retained files and add the llm-vertex-ai and llm-other-providers unit selections. CircleCI gets matching unit jobs. * test: make the tests/unit/llms directories packages Adds __init__.py to the moved dirs and drops the legacy ones whose directories no longer hold tests. * test: drop script runners and path hacks the llms split left dangling The __main__ runners in the split openai_like files and the Databricks e2e runner called tests that now live in the other half of the split or were deleted. The retained legacy halves also no longer need sys.path edits. * test: give the shard-script tests their own GITHUB_OUTPUT They only passed where the runner set it. The CircleCI unit job's env allowlist drops it, so the script's redirect failed there. * test: point the router and module-deletion checks at tests/unit router_code_coverage and code_qa_check_tests only searched tests/test_litellm, so the moved router tests no longer counted. The two silent-experiment tests the audit deleted were the only direct callers of those methods; they are replaced with tests that assert the forwarded shadow request and the recursion guard. * test: move tests/test_litellm integrations and secret_managers into tests/unit Rename-only. Mirrors the old paths, including the directory conftests and the prompt and JSON fixtures. Follow-up commits prune and wire them. * test: prune and repoint the moved integrations tests Deletes the 7 audited tests a stronger test in the same tree already covers, imports the TLS sink helpers from their new conftest path, and restores os.environ after each integrations test. Some presets write OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the legacy tree's test ordering that header leaked into the AgentOps tests. * ci: run the moved integrations tests under their legacy flag The integrations GHA shard and a new CircleCI job run the integrations unit selection. secret_managers joins the misc selection. * docs: point integrations and secret_managers references at tests/unit * test: make the moved integrations directories packages * test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path The Databricks e2e file is a manual script whose main() calls the tests that were pruned, so pruning them broke the documented run. It is back to its main version. The SageMaker Nova docstring now points at the file's real location in tests/local_testing. * test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit Rename-only. Mirrors the old paths, including fixtures, the stubtest config and the native-route wheel script. Two files that collide with existing unit files are merged in a follow-up commit. * test: merge, prune and repoint the moved core, routing, responses, caching and rust_bridge tests Merges the two files that collided with existing unit files, folding the legacy extra case into test_is_chat_completion_cached_dict, and deletes the 9 audited tests a stronger test in the same file already covers. Keeps what needs the network in tests/test_litellm: test_tokenizers pulls a tokenizer from the Hugging Face hub, and the gpt2 and r50k_base tokenizer cases download their BPE files. The unit core_utils conftest points TIKTOKEN_CACHE_DIR at litellm's bundled encodings so the rest never depend on import order to stay offline, and FakeSecretVault moves to a shared module so both trees can build it. * ci: run the moved core, routing, responses, caching and rust_bridge tests under their flags core_utils gets a core-utils flag and CircleCI job, and its GHA shard keeps the legacy path for the retained network tests. router_utils and router_strategy join enterprise-routing, responses joins responses-caching-types (minus responses/mcp, which mcp-integration owns), caching joins caching-local and rust_bridge joins misc. The redis-compat, test-rust, stubtest and merge-smoke paths follow the move. * docs: point the Rust crate references at tests/unit * test: make the moved core, routing and rust_bridge directories packages * test: keep the no-loop DualCache batch_get_cache regression test It runs the sync path outside any event loop, which the inside-loop test cannot, so a change that picks the Redis client by loop state would only show up there. * test: keep the job's UNIT_FLAG out of the shard-script tests * fix(url_utils): block 192.0.0.0/24 on every Python patch release * test: move the new budget limiter tests into tests/unit/router_strategy * test: move the new sentry scrubbing tests into tests/unit/litellm_core_utils * test: move the new zerobus tests into tests/unit/integrations * test: make tests/unit/integrations/zerobus a package * test: load litellm's own tiktoken cache setup once instead of resetting it per test --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
772 lines
29 KiB
Python
772 lines
29 KiB
Python
import asyncio
|
|
import json
|
|
from typing import Any, Dict, Final, List, Optional
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
from litellm.router_strategy.auto_router.auto_router import AutoRouter
|
|
|
|
pytestmark_skip_beta = pytest.mark.skip(
|
|
reason="Skipping auto router tests - beta feature"
|
|
)
|
|
|
|
|
|
class TestExtractTextFromMessages:
|
|
"""Tests for AutoRouter._extract_text_from_messages (no semantic_router dependency)."""
|
|
|
|
def test_should_extract_content_from_simple_user_message(self):
|
|
messages = [{"role": "user", "content": "Hello world"}]
|
|
result = AutoRouter._extract_text_from_messages(messages)
|
|
assert result == "Hello world"
|
|
|
|
def test_should_extract_last_user_message_from_tool_call_conversation(self):
|
|
messages = [
|
|
{"role": "user", "content": "What's the weather in NYC?"},
|
|
{
|
|
"role": "assistant",
|
|
"content": None,
|
|
"tool_calls": [
|
|
{
|
|
"id": "call_abc123",
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_weather",
|
|
"arguments": '{"location": "NYC"}',
|
|
},
|
|
}
|
|
],
|
|
},
|
|
{
|
|
"role": "tool",
|
|
"tool_call_id": "call_abc123",
|
|
"content": "72°F and sunny",
|
|
},
|
|
{"role": "user", "content": "Now tell me about London"},
|
|
]
|
|
result = AutoRouter._extract_text_from_messages(messages)
|
|
assert result == "Now tell me about London"
|
|
|
|
def test_should_find_user_message_when_last_message_is_assistant_with_tool_calls(
|
|
self,
|
|
):
|
|
messages = [
|
|
{"role": "user", "content": "What's the weather?"},
|
|
{
|
|
"role": "assistant",
|
|
"content": None,
|
|
"tool_calls": [
|
|
{
|
|
"id": "call_abc",
|
|
"type": "function",
|
|
"function": {"name": "get_weather", "arguments": "{}"},
|
|
}
|
|
],
|
|
},
|
|
]
|
|
result = AutoRouter._extract_text_from_messages(messages)
|
|
assert result == "What's the weather?"
|
|
|
|
def test_should_find_user_message_when_last_message_is_tool_response(self):
|
|
messages = [
|
|
{"role": "user", "content": "What's the weather?"},
|
|
{
|
|
"role": "assistant",
|
|
"content": None,
|
|
"tool_calls": [
|
|
{
|
|
"id": "call_abc",
|
|
"type": "function",
|
|
"function": {"name": "get_weather", "arguments": "{}"},
|
|
}
|
|
],
|
|
},
|
|
{
|
|
"role": "tool",
|
|
"tool_call_id": "call_abc",
|
|
"content": "72°F and sunny",
|
|
},
|
|
]
|
|
result = AutoRouter._extract_text_from_messages(messages)
|
|
assert result == "What's the weather?"
|
|
|
|
def test_should_handle_multimodal_content_list(self):
|
|
messages = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "What's in this image?"},
|
|
{
|
|
"type": "image_url",
|
|
"image_url": {"url": "https://example.com/img.png"},
|
|
},
|
|
],
|
|
}
|
|
]
|
|
result = AutoRouter._extract_text_from_messages(messages)
|
|
assert result == "What's in this image?"
|
|
|
|
def test_should_handle_multimodal_content_with_multiple_text_blocks(self):
|
|
messages = [
|
|
{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "First part"},
|
|
{"type": "text", "text": "Second part"},
|
|
{
|
|
"type": "image_url",
|
|
"image_url": {"url": "https://example.com/img.png"},
|
|
},
|
|
],
|
|
}
|
|
]
|
|
result = AutoRouter._extract_text_from_messages(messages)
|
|
assert result == "First part Second part"
|
|
|
|
def test_should_return_empty_string_when_user_content_is_none(self):
|
|
messages = [{"role": "user", "content": None}]
|
|
result = AutoRouter._extract_text_from_messages(messages)
|
|
assert result == ""
|
|
|
|
def test_should_return_empty_string_when_no_user_messages(self):
|
|
messages = [
|
|
{"role": "system", "content": "You are a helpful assistant"},
|
|
{
|
|
"role": "assistant",
|
|
"content": None,
|
|
"tool_calls": [
|
|
{
|
|
"id": "call_abc",
|
|
"type": "function",
|
|
"function": {"name": "get_weather", "arguments": "{}"},
|
|
}
|
|
],
|
|
},
|
|
]
|
|
result = AutoRouter._extract_text_from_messages(messages)
|
|
assert result == ""
|
|
|
|
def test_should_return_empty_string_for_empty_messages_list(self):
|
|
result = AutoRouter._extract_text_from_messages([])
|
|
assert result == ""
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_router_instance():
|
|
"""Create a mock LiteLLM Router instance."""
|
|
router = MagicMock()
|
|
router.acompletion = AsyncMock()
|
|
return router
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_semantic_router():
|
|
"""Create a mock SemanticRouter instance."""
|
|
mock_router = MagicMock()
|
|
mock_route = MagicMock()
|
|
mock_route.name = "test-route"
|
|
mock_router.routes = [mock_route]
|
|
return mock_router
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_route_choice():
|
|
"""Create a mock RouteChoice instance."""
|
|
mock_choice = MagicMock()
|
|
mock_choice.name = "test-model"
|
|
return mock_choice
|
|
|
|
|
|
@pytestmark_skip_beta
|
|
class TestAutoRouter:
|
|
"""Test class for AutoRouter methods."""
|
|
|
|
@patch("semantic_router.routers.SemanticRouter")
|
|
def test_init(self, mock_semantic_router_class, mock_router_instance):
|
|
"""Test that AutoRouter initializes correctly with all required parameters."""
|
|
# Arrange
|
|
mock_semantic_router_class.from_json.return_value = mock_semantic_router_class
|
|
|
|
model_name = "test-auto-router"
|
|
router_config_path = "test/path/router.json"
|
|
default_model = "gpt-4o-mini"
|
|
embedding_model = "text-embedding-model"
|
|
|
|
# Act
|
|
auto_router = AutoRouter(
|
|
model_name=model_name,
|
|
auto_router_config_path=router_config_path,
|
|
default_model=default_model,
|
|
embedding_model=embedding_model,
|
|
litellm_router_instance=mock_router_instance,
|
|
)
|
|
|
|
# Assert
|
|
assert auto_router.auto_router_config_path == router_config_path
|
|
assert auto_router.auto_sync_value == AutoRouter.DEFAULT_AUTO_SYNC_VALUE
|
|
assert auto_router.default_model == default_model
|
|
assert auto_router.embedding_model == embedding_model
|
|
assert auto_router.litellm_router_instance == mock_router_instance
|
|
assert auto_router.routelayer is None
|
|
mock_semantic_router_class.from_json.assert_called_once_with(router_config_path)
|
|
|
|
@pytest.mark.asyncio
|
|
@patch("semantic_router.routers.SemanticRouter")
|
|
@patch("litellm.router_strategy.auto_router.litellm_encoder.LiteLLMRouterEncoder")
|
|
async def test_async_pre_routing_hook_with_route_choice(
|
|
self,
|
|
mock_encoder_class,
|
|
mock_semantic_router_class,
|
|
mock_router_instance,
|
|
mock_route_choice,
|
|
):
|
|
"""Test async_pre_routing_hook returns correct model when route is found."""
|
|
# Arrange
|
|
mock_loaded_router = MagicMock()
|
|
mock_loaded_router.routes = ["route1", "route2"]
|
|
mock_semantic_router_class.from_json.return_value = mock_loaded_router
|
|
|
|
mock_routelayer = MagicMock()
|
|
mock_routelayer.return_value = mock_route_choice
|
|
mock_semantic_router_class.return_value = mock_routelayer
|
|
|
|
auto_router = AutoRouter(
|
|
model_name="test-auto-router",
|
|
auto_router_config_path="test/path/router.json",
|
|
default_model="gpt-4o-mini",
|
|
embedding_model="text-embedding-model",
|
|
litellm_router_instance=mock_router_instance,
|
|
)
|
|
|
|
messages = [{"role": "user", "content": "test message"}]
|
|
|
|
# Act
|
|
result = await auto_router.async_pre_routing_hook(
|
|
model="test-model", request_kwargs={}, messages=messages
|
|
)
|
|
|
|
# Assert
|
|
assert result is not None
|
|
assert result.model == "test-model" # Should use the route choice name
|
|
assert result.messages == messages
|
|
mock_routelayer.assert_called_once_with(text="test message")
|
|
|
|
@pytest.mark.asyncio
|
|
@patch("semantic_router.routers.SemanticRouter")
|
|
@patch("litellm.router_strategy.auto_router.litellm_encoder.LiteLLMRouterEncoder")
|
|
async def test_async_pre_routing_hook_with_list_route_choice(
|
|
self,
|
|
mock_encoder_class,
|
|
mock_semantic_router_class,
|
|
mock_router_instance,
|
|
mock_route_choice,
|
|
):
|
|
"""Test async_pre_routing_hook handles list of RouteChoice objects correctly."""
|
|
# Arrange
|
|
mock_loaded_router = MagicMock()
|
|
mock_loaded_router.routes = ["route1", "route2"]
|
|
mock_semantic_router_class.from_json.return_value = mock_loaded_router
|
|
|
|
mock_routelayer = MagicMock()
|
|
mock_routelayer.return_value = [mock_route_choice] # Return list
|
|
mock_semantic_router_class.return_value = mock_routelayer
|
|
|
|
auto_router = AutoRouter(
|
|
model_name="test-auto-router",
|
|
auto_router_config_path="test/path/router.json",
|
|
default_model="gpt-4o-mini",
|
|
embedding_model="text-embedding-model",
|
|
litellm_router_instance=mock_router_instance,
|
|
)
|
|
|
|
messages = [{"role": "user", "content": "test message"}]
|
|
|
|
# Act
|
|
result = await auto_router.async_pre_routing_hook(
|
|
model="test-model", request_kwargs={}, messages=messages
|
|
)
|
|
|
|
# Assert
|
|
assert result is not None
|
|
assert result.model == "test-model"
|
|
assert result.messages == messages
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_pre_routing_hook_no_messages(self, mock_router_instance):
|
|
"""Test async_pre_routing_hook returns None when no messages provided."""
|
|
# Arrange
|
|
with patch("semantic_router.routers.SemanticRouter"):
|
|
auto_router = AutoRouter(
|
|
model_name="test-auto-router",
|
|
auto_router_config_path="test/path/router.json",
|
|
default_model="gpt-4o-mini",
|
|
embedding_model="text-embedding-model",
|
|
litellm_router_instance=mock_router_instance,
|
|
)
|
|
|
|
# Act
|
|
result = await auto_router.async_pre_routing_hook(
|
|
model="test-model", request_kwargs={}, messages=None
|
|
)
|
|
|
|
# Assert
|
|
assert result is None
|
|
|
|
|
|
semantic_router = pytest.importorskip("semantic_router", reason="auto-router needs the semantic-router extra")
|
|
|
|
# SemanticRouter(auto_sync="local") calls the encoder's sync embedding path twice per build:
|
|
# once to probe the encoder's output dimension, once to embed ROUTER_CONFIG's one route's
|
|
# utterances.
|
|
_EMBEDDING_CALLS_PER_ROUTELAYER_BUILD: Final = 2
|
|
|
|
ROUTER_CONFIG: Final = json.dumps(
|
|
{
|
|
"routes": [
|
|
{
|
|
"name": "code-model",
|
|
"description": "coding asks",
|
|
"utterances": ["fix this stack trace", "refactor this function"],
|
|
"score_threshold": 0.5,
|
|
}
|
|
]
|
|
}
|
|
)
|
|
|
|
|
|
class FixedRouteLayer:
|
|
"""Route layer that returns whatever the test tells it to for the query vector it is handed."""
|
|
|
|
def __init__(self, route_choice: Any) -> None:
|
|
self.route_choice = route_choice
|
|
|
|
async def acall(self, vector: Any) -> Any:
|
|
return self.route_choice
|
|
|
|
|
|
def _embedding_response(input: List[str]) -> Any:
|
|
import litellm
|
|
|
|
return litellm.EmbeddingResponse(
|
|
data=[{"embedding": [0.1, 0.2], "index": i, "object": "embedding"} for i in range(len(input))]
|
|
)
|
|
|
|
|
|
class StubEmbeddingRouter:
|
|
"""Stands in for the LiteLLM Router, recording the text and kwargs each query embedding was made with."""
|
|
|
|
def __init__(self) -> None:
|
|
self.seen_text: str | None = None
|
|
self.aembedding_kwargs: Dict[str, Any] | None = None
|
|
|
|
def embedding(self, input: List[str], model: str, **kwargs: Any) -> Any:
|
|
return _embedding_response(input)
|
|
|
|
async def aembedding(self, input: List[str], model: str, **kwargs: Any) -> Any:
|
|
self.seen_text = input[0]
|
|
self.aembedding_kwargs = kwargs
|
|
return _embedding_response(input)
|
|
|
|
|
|
class FailingEmbeddingRouter(StubEmbeddingRouter):
|
|
"""Router whose query embedding fails, as it does when the prompt exceeds the encoder's window."""
|
|
|
|
async def aembedding(self, input: List[str], model: str, **kwargs: Any) -> Any:
|
|
raise ValueError(
|
|
"litellm.InternalServerError: input is too large to process. increase the physical batch size"
|
|
)
|
|
|
|
|
|
def _auto_router(routelayer: Any, litellm_router_instance: Any = None, **kwargs: Any) -> AutoRouter:
|
|
auto_router: Final = AutoRouter(
|
|
model_name="my-auto-router",
|
|
auto_router_config=ROUTER_CONFIG,
|
|
default_model="fallback-model",
|
|
embedding_model="text-embedding-3-small",
|
|
litellm_router_instance=litellm_router_instance or StubEmbeddingRouter(),
|
|
**kwargs,
|
|
)
|
|
auto_router.routelayer = routelayer
|
|
return auto_router
|
|
|
|
|
|
class TestAutoRouterAlwaysResolvesARoutableModel:
|
|
"""The hook returns the alias's default model instead of failing or leaking the alias downstream."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_fall_back_to_default_model_when_the_embedding_call_fails(self):
|
|
auto_router: Final = _auto_router(FixedRouteLayer(None), litellm_router_instance=FailingEmbeddingRouter())
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "a" * 100_000}],
|
|
)
|
|
|
|
assert result is not None
|
|
assert result.model == "fallback-model"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_fall_back_to_default_model_when_no_route_matches(self):
|
|
auto_router: Final = _auto_router(FixedRouteLayer(None))
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "nothing like any route"}],
|
|
)
|
|
|
|
assert result is not None
|
|
# Leaving "my-auto-router" here fails downstream with "Unmapped LLM provider".
|
|
assert result.model == "fallback-model"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_fall_back_to_default_model_when_the_route_layer_returns_an_empty_list(self):
|
|
auto_router: Final = _auto_router(FixedRouteLayer([]))
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "nothing like any route"}],
|
|
)
|
|
|
|
assert result is not None
|
|
assert result.model == "fallback-model"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_route_to_the_first_choice_when_the_route_layer_returns_a_populated_list(self):
|
|
from semantic_router.schema import RouteChoice
|
|
|
|
auto_router: Final = _auto_router(
|
|
FixedRouteLayer([RouteChoice(name="code-model"), RouteChoice(name="chat-model")])
|
|
)
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
|
|
assert result is not None
|
|
assert result.model == "code-model"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_still_route_to_the_matched_route_when_one_matches(self):
|
|
from semantic_router.schema import RouteChoice
|
|
|
|
router: Final = StubEmbeddingRouter()
|
|
auto_router: Final = _auto_router(FixedRouteLayer(RouteChoice(name="code-model")), litellm_router_instance=router)
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
|
|
assert result is not None
|
|
assert result.model == "code-model"
|
|
assert router.seen_text == "fix this stack trace"
|
|
|
|
|
|
class TestAutoRouterEmbeddingInputCap:
|
|
"""The cap configured on the deployment is what the encoder enforces."""
|
|
|
|
def test_should_default_the_cap_to_the_shared_constant(self):
|
|
from litellm.constants import DEFAULT_AUTO_ROUTER_MAX_INPUT_CHARS
|
|
|
|
assert _auto_router(FixedRouteLayer(None)).max_input_chars == DEFAULT_AUTO_ROUTER_MAX_INPUT_CHARS
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_build_its_encoder_with_the_configured_cap(self):
|
|
auto_router: Final = _auto_router(None, litellm_router_instance=StubEmbeddingRouter(), max_input_chars=777)
|
|
|
|
await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
|
|
assert auto_router.routelayer is not None
|
|
assert auto_router.routelayer.encoder.max_input_chars == 777
|
|
|
|
|
|
class TestAutoRouterRoutesResponsesApiInput:
|
|
"""Responses API requests carry the prompt in `input`, not `messages`, and still have to reach the route layer."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_route_a_string_input_when_messages_is_none(self):
|
|
from semantic_router.schema import RouteChoice
|
|
|
|
router: Final = StubEmbeddingRouter()
|
|
auto_router: Final = _auto_router(FixedRouteLayer(RouteChoice(name="code-model")), litellm_router_instance=router)
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={
|
|
"input": "fix this stack trace",
|
|
"litellm_metadata": {"user_api_key_request_route": "/v1/responses"},
|
|
},
|
|
messages=None,
|
|
)
|
|
|
|
assert result is not None
|
|
assert result.model == "code-model"
|
|
assert result.messages is None
|
|
assert router.seen_text == "fix this stack trace"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_route_a_list_input_with_instructions_when_messages_is_none(self):
|
|
from semantic_router.schema import RouteChoice
|
|
|
|
router: Final = StubEmbeddingRouter()
|
|
auto_router: Final = _auto_router(FixedRouteLayer(RouteChoice(name="code-model")), litellm_router_instance=router)
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={
|
|
"instructions": "You are a coding agent.",
|
|
"input": [
|
|
{
|
|
"type": "message",
|
|
"role": "user",
|
|
"content": [{"type": "input_text", "text": "fix this stack trace"}],
|
|
}
|
|
],
|
|
"litellm_metadata": {"user_api_key_request_route": "/v1/responses"},
|
|
},
|
|
messages=None,
|
|
)
|
|
|
|
assert result is not None
|
|
assert result.model == "code-model"
|
|
assert router.seen_text is not None
|
|
assert "fix this stack trace" in router.seen_text
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_skip_routing_when_neither_messages_nor_input_is_present(self):
|
|
router: Final = StubEmbeddingRouter()
|
|
auto_router: Final = _auto_router(FixedRouteLayer(None), litellm_router_instance=router)
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={"litellm_metadata": {"user_api_key_request_route": "/v1/responses"}},
|
|
messages=None,
|
|
)
|
|
|
|
assert result is None
|
|
assert router.seen_text is None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_keep_routing_an_empty_messages_list_to_the_default_model(self):
|
|
router: Final = StubEmbeddingRouter()
|
|
auto_router: Final = _auto_router(FixedRouteLayer(None), litellm_router_instance=router)
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={"messages": [], "litellm_metadata": {"user_api_key_request_route": "/v1/chat/completions"}},
|
|
messages=[],
|
|
)
|
|
|
|
assert result is not None
|
|
assert result.model == "fallback-model"
|
|
assert router.seen_text == ""
|
|
|
|
|
|
class TestAutoRouterAttributesItsEmbeddingSpend:
|
|
"""The query embedding is billed to the key that sent the request, like any other call it made."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_forward_the_callers_identity_to_the_query_embedding_minus_its_budget_reservation(self):
|
|
from litellm.constants import INTERNAL_CALL_ORIGIN_METADATA_KEY
|
|
|
|
router: Final = StubEmbeddingRouter()
|
|
auto_router: Final = _auto_router(None, litellm_router_instance=router)
|
|
request_kwargs: Final = {
|
|
"metadata": {
|
|
"user_api_key": "hashed-key",
|
|
"user_api_key_team_id": "team-1",
|
|
"user_api_key_budget_reservation": {"reservation_id": "r-1"},
|
|
},
|
|
"litellm_session_id": "session-1",
|
|
}
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs=request_kwargs,
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
|
|
assert result is not None
|
|
assert router.seen_text == "fix this stack trace"
|
|
assert router.aembedding_kwargs is not None
|
|
forwarded: Final = router.aembedding_kwargs["metadata"]
|
|
assert forwarded["user_api_key"] == "hashed-key"
|
|
assert forwarded["user_api_key_team_id"] == "team-1"
|
|
assert forwarded[INTERNAL_CALL_ORIGIN_METADATA_KEY] == "autorouter_classifier"
|
|
assert "user_api_key_budget_reservation" not in forwarded
|
|
assert router.aembedding_kwargs["litellm_session_id"] == "session-1"
|
|
assert router.aembedding_kwargs["proxy_server_request"] == {
|
|
"body": {"model": "text-embedding-3-small", "input": ["fix this stack trace"]}
|
|
}
|
|
|
|
|
|
class ThreadTrackingEmbeddingRouter(StubEmbeddingRouter):
|
|
"""Records which OS thread and how many times `embedding()` was called during a build."""
|
|
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.embedding_call_threads: list[int] = []
|
|
|
|
def embedding(self, input: list[str], model: str, **kwargs: Any) -> Any:
|
|
import threading
|
|
|
|
self.embedding_call_threads.append(threading.get_ident())
|
|
return super().embedding(input, model, **kwargs)
|
|
|
|
|
|
class TestAutoRouterColdStartDoesNotBlockTheEventLoop:
|
|
"""The first request through a fresh alias builds the route layer off the event loop thread,
|
|
and concurrent first requests build it exactly once."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_build_the_routelayer_on_a_worker_thread_not_the_event_loop_thread(self):
|
|
import threading
|
|
|
|
embedding_router: Final = ThreadTrackingEmbeddingRouter()
|
|
auto_router: Final = _auto_router(None, litellm_router_instance=embedding_router)
|
|
event_loop_thread: Final = threading.get_ident()
|
|
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
|
|
assert result is not None
|
|
assert len(embedding_router.embedding_call_threads) == _EMBEDDING_CALLS_PER_ROUTELAYER_BUILD
|
|
assert set(embedding_router.embedding_call_threads) == {embedding_router.embedding_call_threads[0]}
|
|
assert embedding_router.embedding_call_threads[0] != event_loop_thread
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_build_the_routelayer_exactly_once_under_concurrent_cold_start_requests(self):
|
|
embedding_router: Final = ThreadTrackingEmbeddingRouter()
|
|
auto_router: Final = _auto_router(None, litellm_router_instance=embedding_router)
|
|
|
|
results: Final = await asyncio.gather(
|
|
*(
|
|
auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
for _ in range(10)
|
|
)
|
|
)
|
|
|
|
assert all(result is not None for result in results)
|
|
assert len(embedding_router.embedding_call_threads) == _EMBEDDING_CALLS_PER_ROUTELAYER_BUILD
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_not_duplicate_the_build_when_a_caller_is_cancelled_mid_build(self):
|
|
"""A caller arriving while the first is cancelled mid-build must reuse it, not duplicate it."""
|
|
import threading
|
|
|
|
class BlockingEmbeddingRouter(ThreadTrackingEmbeddingRouter):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.started = threading.Event()
|
|
self.release = threading.Event()
|
|
|
|
def embedding(self, input: list[str], model: str, **kwargs: Any) -> Any:
|
|
self.started.set()
|
|
self.release.wait(timeout=5)
|
|
return super().embedding(input, model, **kwargs)
|
|
|
|
embedding_router: Final = BlockingEmbeddingRouter()
|
|
auto_router: Final = _auto_router(None, litellm_router_instance=embedding_router)
|
|
|
|
first_call: Final = asyncio.ensure_future(
|
|
auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
)
|
|
while not embedding_router.started.is_set():
|
|
await asyncio.sleep(0.01)
|
|
|
|
first_call.cancel()
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await first_call
|
|
|
|
second_call: Final = asyncio.ensure_future(
|
|
auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
)
|
|
await asyncio.sleep(0.01) # let the second call observe the still-running build
|
|
embedding_router.release.set()
|
|
result: Final = await second_call
|
|
|
|
assert result is not None
|
|
assert len(embedding_router.embedding_call_threads) == _EMBEDDING_CALLS_PER_ROUTELAYER_BUILD
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_should_clear_a_failed_build_even_with_no_caller_left_to_observe_it(self):
|
|
"""A build failing after its only caller was cancelled must still clear, not stay cached."""
|
|
import threading
|
|
|
|
class FailsOnFirstAttemptEmbeddingRouter(ThreadTrackingEmbeddingRouter):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.started = threading.Event()
|
|
self.release = threading.Event()
|
|
self.attempts = 0
|
|
|
|
def embedding(self, input: list[str], model: str, **kwargs: Any) -> Any:
|
|
self.attempts += 1
|
|
attempt = self.attempts
|
|
self.started.set()
|
|
self.release.wait(timeout=5)
|
|
if attempt == 1:
|
|
raise ValueError("boom")
|
|
return super().embedding(input, model, **kwargs)
|
|
|
|
embedding_router: Final = FailsOnFirstAttemptEmbeddingRouter()
|
|
auto_router: Final = _auto_router(None, litellm_router_instance=embedding_router)
|
|
|
|
first_call: Final = asyncio.ensure_future(
|
|
auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
)
|
|
while not embedding_router.started.is_set():
|
|
await asyncio.sleep(0.01)
|
|
first_call.cancel()
|
|
with pytest.raises(asyncio.CancelledError):
|
|
await first_call
|
|
|
|
# Nobody awaits the build now. Let the first attempt fail on its own.
|
|
embedding_router.release.set()
|
|
build_task = auto_router._routelayer_build_task
|
|
assert build_task is not None
|
|
while not build_task.done():
|
|
await asyncio.sleep(0.01)
|
|
await asyncio.sleep(0.01) # let the done-callback (scheduled via call_soon) run
|
|
|
|
assert auto_router._routelayer_build_task is None
|
|
|
|
embedding_router.started.clear()
|
|
embedding_router.release.clear()
|
|
result: Final = await auto_router.async_pre_routing_hook(
|
|
model="my-auto-router",
|
|
request_kwargs={},
|
|
messages=[{"role": "user", "content": "fix this stack trace"}],
|
|
)
|
|
|
|
assert result is not None
|