mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-27 01:22:18 +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: 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. --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
245 lines
8.6 KiB
Python
245 lines
8.6 KiB
Python
"""
|
|
Tests for the ``aliases`` feature in the model cost map.
|
|
|
|
The ``_expand_model_aliases`` function processes ``aliases`` lists from model
|
|
entries, creating shared dict references for alias entries at load time.
|
|
"""
|
|
|
|
from unittest.mock import patch
|
|
|
|
from litellm import verbose_logger
|
|
from litellm.litellm_core_utils.get_model_cost_map import _expand_model_aliases
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Core expansion behaviour
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestExpandModelAliases:
|
|
"""Unit tests for _expand_model_aliases."""
|
|
|
|
def test_basic_expansion(self):
|
|
"""Aliases are added as top-level entries in model_cost."""
|
|
model_cost = {
|
|
"my-model-latest": {
|
|
"aliases": ["my-model-20250101"],
|
|
"input_cost_per_token": 1e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
assert "my-model-20250101" in result
|
|
assert result["my-model-20250101"]["input_cost_per_token"] == 1e-06
|
|
assert result["my-model-20250101"]["litellm_provider"] == "test"
|
|
|
|
def test_multiple_aliases(self):
|
|
"""A single entry can declare multiple aliases."""
|
|
model_cost = {
|
|
"provider/model-latest": {
|
|
"aliases": ["provider/model-v1", "provider/model-v2"],
|
|
"input_cost_per_token": 5e-06,
|
|
"litellm_provider": "provider",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
assert "provider/model-v1" in result
|
|
assert "provider/model-v2" in result
|
|
|
|
def test_shared_dict_reference(self):
|
|
"""Alias entries share the same dict object as the canonical entry (no copy)."""
|
|
model_cost = {
|
|
"canonical-model": {
|
|
"aliases": ["alias-model"],
|
|
"input_cost_per_token": 2e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
assert result["alias-model"] is result["canonical-model"]
|
|
|
|
def test_aliases_key_removed(self):
|
|
"""The ``aliases`` key is removed from the entry after expansion."""
|
|
model_cost = {
|
|
"my-model": {
|
|
"aliases": ["my-model-alias"],
|
|
"input_cost_per_token": 1e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
assert "aliases" not in result["my-model"]
|
|
assert "aliases" not in result["my-model-alias"]
|
|
|
|
def test_entries_without_aliases_unchanged(self):
|
|
"""Entries with no ``aliases`` key are left untouched."""
|
|
model_cost = {
|
|
"plain-model": {
|
|
"input_cost_per_token": 3e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
assert "plain-model" in result
|
|
assert result["plain-model"]["input_cost_per_token"] == 3e-06
|
|
assert len(result) == 1
|
|
|
|
def test_empty_aliases_list(self):
|
|
"""An empty ``aliases`` list is treated the same as no aliases."""
|
|
model_cost = {
|
|
"model-a": {
|
|
"aliases": [],
|
|
"input_cost_per_token": 1e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
assert len(result) == 1
|
|
assert "model-a" in result
|
|
assert "aliases" not in result["model-a"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Conflict handling
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestAliasConflicts:
|
|
"""Tests for alias conflict detection and handling."""
|
|
|
|
def test_alias_conflicts_with_canonical_entry(self):
|
|
"""Alias that matches an existing canonical entry is skipped with a warning."""
|
|
model_cost = {
|
|
"model-latest": {
|
|
"aliases": ["model-dated"],
|
|
"input_cost_per_token": 1e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
"model-dated": {
|
|
"input_cost_per_token": 1e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
with patch.object(verbose_logger, "warning") as mock_warn:
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
# The canonical "model-dated" entry is preserved, not overwritten
|
|
assert "model-dated" in result
|
|
# Verify a warning about the alias conflict was logged
|
|
mock_warn.assert_called()
|
|
warning_messages = " ".join(str(c) for c in mock_warn.call_args_list)
|
|
assert "alias conflict" in warning_messages.lower()
|
|
|
|
def test_duplicate_alias_across_entries(self):
|
|
"""Same alias claimed by two different entries: second one is skipped."""
|
|
model_cost = {
|
|
"model-a": {
|
|
"aliases": ["shared-alias"],
|
|
"input_cost_per_token": 1e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
"model-b": {
|
|
"aliases": ["shared-alias"],
|
|
"input_cost_per_token": 2e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
with patch.object(verbose_logger, "warning") as mock_warn:
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
# "shared-alias" should point to model-a (first one wins)
|
|
assert "shared-alias" in result
|
|
assert result["shared-alias"]["input_cost_per_token"] == 1e-06
|
|
# Verify a warning about the alias conflict was logged
|
|
mock_warn.assert_called()
|
|
warning_messages = " ".join(str(c) for c in mock_warn.call_args_list)
|
|
assert "alias conflict" in warning_messages.lower()
|
|
|
|
def test_canonical_entry_not_overwritten_by_alias(self):
|
|
"""An alias must never overwrite an existing canonical entry's data."""
|
|
original_cost = 9.99e-06
|
|
model_cost = {
|
|
"existing-model": {
|
|
"input_cost_per_token": original_cost,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
"other-model": {
|
|
"aliases": ["existing-model"],
|
|
"input_cost_per_token": 1e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
# Original entry must be preserved
|
|
assert result["existing-model"]["input_cost_per_token"] == original_cost
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Integration with model_cost dict mutation
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestAliasIntegration:
|
|
"""Higher-level tests verifying aliases work with the model_cost dict."""
|
|
|
|
def test_mutation_through_alias_visible_on_canonical(self):
|
|
"""Since alias is a shared reference, mutations are visible on both."""
|
|
model_cost = {
|
|
"canonical": {
|
|
"aliases": ["alias"],
|
|
"input_cost_per_token": 1e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
# Mutate via alias
|
|
result["alias"]["input_cost_per_token"] = 999
|
|
assert result["canonical"]["input_cost_per_token"] == 999
|
|
|
|
def test_mixed_entries_with_and_without_aliases(self):
|
|
"""A model_cost dict with a mix of aliased and plain entries."""
|
|
model_cost = {
|
|
"model-with-alias": {
|
|
"aliases": ["alias-1", "alias-2"],
|
|
"input_cost_per_token": 1e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
"plain-model": {
|
|
"input_cost_per_token": 2e-06,
|
|
"litellm_provider": "test",
|
|
"mode": "chat",
|
|
},
|
|
}
|
|
result = _expand_model_aliases(model_cost)
|
|
|
|
assert len(result) == 4 # 2 canonical + 2 aliases
|
|
assert "alias-1" in result
|
|
assert "alias-2" in result
|
|
assert "plain-model" in result
|
|
assert "model-with-alias" in result
|
|
|
|
def test_expand_on_empty_dict(self):
|
|
"""Expanding an empty dict returns an empty dict."""
|
|
assert _expand_model_aliases({}) == {}
|