mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* feat(proxy): mixed Codex model picker and LiteLLM routing on native codex routes
The native Codex catalog now lists the key's other chat and responses models after
ChatGPT's, within Codex's catalog size limit, and drops the ETag so the merged list
refreshes on Codex's timer. A request for a model configured by exact name on another
provider goes through /v1/responses without the ChatGPT bearer or account header,
while ChatGPT names and wildcard-only matches keep the subscription passthrough.
Reasoning items another provider produced are dropped before forwarding to ChatGPT.
* fix(proxy): keep Codex's picker from turning reasoning off for LiteLLM models
Codex fills a missing default_reasoning_level with effort "none" when a model is picked in /model, while
`codex -m <model>` sends no effort at all. A thinking-only provider model then answers 400. On the native
/backend-api/codex/responses route, drop effort "none" for a LiteLLM model whose catalog entry names no
default, so the provider applies its own default exactly as `codex -m` does
* fix(proxy): harden Codex mixed-model routing on the native ChatGPT routes
Keep the ChatGPT credentials out of hook raw headers on the inner /v1/responses
request, route to LiteLLM and merge LiteLLM catalog rows only for keys that may
call /v1/responses, drop unpersisted reasoning ids on the passthrough when store
is not true, keep any slug the caller's ChatGPT account lists on the subscription
via a shared account catalog cache, and keep X-Models-Etag for keys whose catalog
added no LiteLLM rows. The guide covers the new behavior and the /v1 wildcard
precedence over a catch-all deployment.
* fix(proxy): keep Codex's native route on the subscription when the account list is unknown
A name OpenAI lists for the caller's ChatGPT account now goes to LiteLLM only when the
account's list is known and leaves it out. A failed catalog read is remembered for 60 s and
the Codex version comes only from a User-Agent whose product matches the originator header.
The merged catalog serves a stable ETag digest of OpenAI's ETag and the key's LiteLLM rows,
and /responses replaces X-Models-Etag with the same value, memoized per key for 300 s, so
Codex stops refetching or dropping its catalog cache.
LiteLLM-routed native replies wrap their reasoning with the model id, so an OpenAI or Azure
blob is dropped before chatgpt.com and unwrapped for LiteLLM, and the chat-completions bridge
decodes wrapped thinking blocks with their signature.
The access-group recipe uses chatgpt/ model names so /v1 routing does not change, and the
native route checks the chatgpt/ form of a name the key holds only through that group. A
none reasoning effort is dropped only under drop_params. The guide covers app tools, every
routing case, and the ETag, and the catalog logs how many models the byte limit left out.
* fix(fireworks_ai): move Codex additional_tools items into tools on the Responses API
* fix(proxy): keep Codex native turns working when the key's model listing fails
A listing callback that raises no longer turns chatgpt.com's catalog or
responses answer into a 500. The catalog keeps upstream's body and ETag, and
responses keep upstream's X-Models-Etag.
Under drop_params, a `none` reasoning effort is now dropped only when a
candidate deployment does not explicitly support it, so an alias to a model
like openai/gpt-5.4 keeps it.
The account slug list and its unavailable marker are keyed on the account id
plus the caller's ChatGPT Authorization header, so a failed fetch with one
bearer cannot turn off LiteLLM routing for another caller on the same account.
The auth step checks the Codex route before importing the Codex module.
* fix(proxy): keep Codex model switches working across LiteLLM providers
On the Codex native route, a LiteLLM model now gets only the encrypted reasoning every deployment it can route to is able to decrypt, so ChatGPT or another deployment's blobs no longer make xAI answer 400
Codex additional_tools items are hoisted into tools for every provider except the OpenAI API, and namespace tools become flat tools named mcp__docs__search, the shape xAI accepts
A JWT caller's models ETag is cached on a hash of its LiteLLM credential, so ChatGPT turns stop rebuilding the catalog rows
* fix(proxy): keep Codex namespace tool calls intact across LiteLLM providers
Fireworks and Bedrock Mantle accept Codex namespace tools natively, so the
native route now hoists them without flattening, like OpenAI and Azure and
the chat completions bridge. Other native Responses providers still get
flat names, now built with the bridge's namespace_tool_name_map, and each
function_call they return gets its tool name and namespace back in the
non-streaming body and in streamed output_item.added, output_item.done,
and completed/incomplete/failed events. Every other event passes through
unchanged
* fix(proxy): bridge Codex turns for providers whose Responses API rejects Codex tools
Codex sends its freeform exec tool as a custom tool on every turn, and xAI's
Responses API answers 422 for custom, namespace and additional_tools shapes.
On the Codex native route, a model whose deployments include a native
Responses provider other than OpenAI, Azure, Fireworks or Bedrock Mantle now
asks for the chat completions bridge with use_chat_completions_api, which
already turns custom and namespace tools into functions, hoists
additional_tools, and puts the namespace back on each returned call
OpenAI and Azure still get the request unchanged, and Fireworks and Bedrock
Mantle still get it on their own Responses API with additional_tools hoisted.
The route's namespace flattening and the response-side namespace restore are
removed, since no path sends flattened names anymore
* fix(proxy): drop reasoning ids a Codex turn's LiteLLM target cannot resolve
A reasoning item another deployment produced, replayed with store false and no blob the
target can decrypt, now reaches the target as its readable summary without id or status,
and is dropped when nothing readable is left. OpenAI answered 400 on the status field and
404 on the id for a glm-5p3 item replayed whole to an openai deployment. Same-deployment
replays and stored conversations keep the item whole
* refactor(codex): reach encrypted reasoning stripping through the router registry
The Codex route read Router.optional_callbacks and imported EncryptedContentAffinityCheck from
litellm.router_utils, which the router surface fence forbids outside the router. RouterRegistry
gains decryptable_reasoning(model_group, request_input, targets), which keeps the same rule: with
encrypted content affinity on for the group, reasoning one target can decrypt stays, otherwise
every target must decrypt it
* fix(proxy): keep Codex's priority tier off LiteLLM rows in the native model catalog
* fix(proxy): drop Codex's none effort for an alias whose native entry names no default
* fix(proxy): start a native Codex alias at its pinned reasoning effort and build the catalog off the event loop
* test(proxy): map the pinned reasoning efforts field in the router registry stub
* fix(proxy): honor admin-only /v1/responses and keep Codex's default tier off native aliases
Codex native routes now send a key to LiteLLM only when admin_only_routes allows it,
a native alias with configured service tiers starts with no tier picked, and the
account slug cache declares its Redis key family.
* fix(proxy): check the key's tool allowlist on native Codex LiteLLM turns and additional_tools items
* fix(proxy): check vector store grants on file_search tools inside additional_tools items
A Responses request can define file_search inside an additional_tools input item, which the native Codex route hoists for LiteLLM-served models and OpenAI runs as is on /v1/responses. The vector store check now reads those tools in both strict and legacy modes. The input item scan uses a Mapping guard instead of a pydantic error per item, since auth now runs it on every list input.
* fix(proxy): apply the proxy-wide allowed_routes to LiteLLM models on native Codex routes
* fix(proxy): cap native Codex account catalog reads per LiteLLM caller before rate limits apply
* fix(guardrails): check additional_tools items next to top-level function tools in tool_policy
ToolPolicyGuardrail read tools from the request body only when no top-level
function tool existed, so a blocked tool defined in an additional_tools item
reached Fireworks once the Responses transform moved the item into tools after
the pre-call hooks. Check every list on each request, including namespace
members inside items.
* fix(router): serve decryptable_reasoning from the Rust core's optional checks
The merge with main removed the Python router core, so the registry's host has no
optional_callbacks and no routing context, and native Codex requests with encrypted
reasoning raised RustCoreUnsupported. The filter now lives with the optional checks,
which own both. The ported pinned_reasoning_efforts field gets a test on the Rust core
Also give the static litellm.Router a declared constructor. Since the Python core was
deleted a bare __init__ assignment reads as RustCore's, so every Router(model_list=...)
is a type error and make lint fails on main. TYPE_CHECKING only, no runtime change
* fix(proxy): check the budget of a saved alias target on native Codex LiteLLM turns
Native /backend-api/codex/responses binds {endpoint}, so auth skipped the key's or team's model_group_alias and read budgets on the requested name, while the later /v1/responses handoff served the target unchecked. Auth now resolves the alias on a turn that routes to LiteLLM, as it does on /v1, and leaves ChatGPT-only names on the subscription. Also makes the tool_policy tests build a real ToolPolicyRegistry and type their fixtures.
* fix(proxy): keep a name the ChatGPT account lists on the subscription despite a saved alias
* fix(codex): keep LiteLLM keys off ChatGPT, enforce tool policy on namespaces and pin reasoning on aliases
Auth no longer asks ChatGPT for the account's model list when the bearer is
missing or is the LiteLLM key, so a key sent as the bearer never reaches it.
tool_policy now reads the members of a top-level namespace, which is where
the native route's hoist puts them.
A global alias now reads encrypted content affinity from its target group.
A non-list top-level tools value and a non-string function name now reach the
provider, which refuses them, instead of raising a 500.
The catalog ETag no longer depends on the order a process iterates priced
service tiers in, so workers and pods agree on it.
* fix(proxy): list a team's models in the order the team set them
get_team_models built the names in a set, so each process listed a team key's models in its own hash order and the native Codex catalog sent a different row order and ETag from each worker
* fix(proxy): apply JWT route policy before native Codex routes serve LiteLLM models
A standard JWT caller now gets a LiteLLM model on /backend-api/codex/models
and /backend-api/codex/responses only when the JWT auth flow would admit a
direct /v1/responses call for that model, so team_allowed_routes,
admin_allowed_routes and RBAC route limits hold on the handoff too.
The Fireworks Responses transform also leaves additional_tools input items
in place when the tools param is not a list, instead of raising while hoisting
* fix(fireworks): hoist additional_tools when tools is a tuple
litellm.responses passes a tuple tools argument through to the Fireworks
transform unchanged, so the non-list guard skipped hoisting and left the
additional_tools item in the input. Treat tuples like lists and cover both
shapes in the drop_params merge test
---------
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
663 lines
28 KiB
Python
663 lines
28 KiB
Python
"""
|
|
Router entry points and DeprecatedRouterMembers.
|
|
|
|
Every member that moved into DeprecatedRouterMembers must return what its entry point returns,
|
|
warn once per access from outside the router, and count the access. Internal access (the router's
|
|
own modules) must stay silent.
|
|
"""
|
|
|
|
import asyncio
|
|
import copy
|
|
import dataclasses
|
|
import warnings
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
import litellm
|
|
from litellm import Router
|
|
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
|
|
from litellm.router_api import SETTING_FIELDS, ChangeSet, RouterSettings
|
|
from litellm.router_deprecated import DEPRECATED_ROUTER_MEMBER_USES, DeprecatedRouterMembers
|
|
from litellm.types.router import Deployment
|
|
|
|
|
|
def _router() -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-a"},
|
|
"model_info": {"id": "dep-a", "access_groups": ["grp"]},
|
|
},
|
|
{
|
|
"model_name": "gpt",
|
|
"litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-b"},
|
|
"model_info": {"id": "dep-b"},
|
|
},
|
|
{
|
|
"model_name": "team-internal",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-t"},
|
|
"model_info": {"id": "dep-team", "team_id": "team-1", "team_public_model_name": "team-gpt"},
|
|
},
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {"model": "openai/*", "api_key": "sk-w"},
|
|
"model_info": {"id": "dep-wild"},
|
|
},
|
|
],
|
|
model_group_alias={"alias-gpt": "gpt"},
|
|
fallbacks=[{"gpt": ["team-internal"]}],
|
|
cooldown_time=7,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def router():
|
|
r = _router()
|
|
yield r
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
r.retire()
|
|
|
|
|
|
# (old member, how to read it through the old name, how to read it through the entry point)
|
|
READS = [
|
|
("model_list", lambda r: r.model_list, lambda r: r.registry().all()),
|
|
("model_names", lambda r: r.model_names, lambda r: r.registry().names(kind="group_set")),
|
|
("deployment_names", lambda r: r.deployment_names, lambda r: r.registry().names(kind="deployment_names")),
|
|
("team_public_model_names", lambda r: r.team_public_model_names, lambda r: r.registry().names(kind="team_public")),
|
|
(
|
|
"model_name_to_deployment_indices",
|
|
lambda r: r.model_name_to_deployment_indices,
|
|
lambda r: r.registry().index("model_name"),
|
|
),
|
|
(
|
|
"team_model_to_deployment_indices",
|
|
lambda r: r.team_model_to_deployment_indices,
|
|
lambda r: r.registry().index("team_model"),
|
|
),
|
|
("model_group_alias", lambda r: r.model_group_alias, lambda r: r.registry().aliases()),
|
|
("pattern_router", lambda r: r.pattern_router, lambda r: r.registry().patterns()),
|
|
("team_pattern_routers", lambda r: r.team_pattern_routers, lambda r: r.registry().patterns(team=True)),
|
|
("routing_groups", lambda r: r.routing_groups, lambda r: r.registry().routing_groups()),
|
|
("auto_routers", lambda r: r.auto_routers, lambda r: r.registry().plugins("auto")),
|
|
("complexity_routers", lambda r: r.complexity_routers, lambda r: r.registry().plugins("complexity")),
|
|
("quality_routers", lambda r: r.quality_routers, lambda r: r.registry().plugins("quality")),
|
|
("adaptive_routers", lambda r: r.adaptive_routers, lambda r: r.registry().plugins("adaptive")),
|
|
("routing_plugins", lambda r: r.routing_plugins, lambda r: r.registry().plugins("routing")),
|
|
(
|
|
"prompt_caching_pin_cache",
|
|
lambda r: r.prompt_caching_pin_cache,
|
|
lambda r: r.registry().plugins("prompt_caching_pins"),
|
|
),
|
|
("cache", lambda r: r.cache, lambda r: r.health().store),
|
|
("cooldown_cache", lambda r: r.cooldown_cache, lambda r: r.health().cooldowns),
|
|
("health_state_cache", lambda r: r.health_state_cache, lambda r: r.health().health_checks),
|
|
("deployment_latency_map", lambda r: r.deployment_latency_map, lambda r: r.health().latency),
|
|
("get_deployment", lambda r: r.get_deployment("dep-a"), lambda r: r.registry().deployment("dep-a")),
|
|
("has_model_id", lambda r: r.has_model_id("dep-b"), lambda r: r.registry().has_deployment("dep-b")),
|
|
("get_model_info", lambda r: r.get_model_info("dep-a"), lambda r: r.registry().deployment_dict("dep-a")),
|
|
("get_model_list", lambda r: r.get_model_list("gpt"), lambda r: r.registry().deployments("gpt")),
|
|
("get_model_list_all", lambda r: r.get_model_list(), lambda r: r.registry().deployments()),
|
|
("get_model_list_alias", lambda r: r.get_model_list("alias-gpt"), lambda r: r.registry().deployments("alias-gpt")),
|
|
(
|
|
"get_all_deployments",
|
|
lambda r: r.get_all_deployments("gpt"),
|
|
lambda r: r.registry().deployments("gpt", source="all"),
|
|
),
|
|
(
|
|
"config_deployments",
|
|
lambda r: list(r.config_deployments()),
|
|
lambda r: list(r.registry().deployments(source="config")),
|
|
),
|
|
(
|
|
"deployments_named",
|
|
lambda r: r.deployments_named("gpt"),
|
|
lambda r: r.registry().deployments("gpt", source="named"),
|
|
),
|
|
(
|
|
"get_model_list_from_model_alias",
|
|
lambda r: r.get_model_list_from_model_alias("alias-gpt"),
|
|
lambda r: r.registry().deployments("alias-gpt", source="alias"),
|
|
),
|
|
(
|
|
"get_deployment_by_model_group_name",
|
|
lambda r: r.get_deployment_by_model_group_name("gpt"),
|
|
lambda r: r.registry().deployments("gpt", source="first_in_group"),
|
|
),
|
|
("get_model_names", lambda r: r.get_model_names(), lambda r: r.registry().names()),
|
|
(
|
|
"get_model_names_team",
|
|
lambda r: r.get_model_names(team_id="team-1"),
|
|
lambda r: r.registry().names(team_id="team-1"),
|
|
),
|
|
("get_model_ids", lambda r: r.get_model_ids("gpt"), lambda r: r.registry().names("gpt", kind="ids")),
|
|
(
|
|
"get_model_ids_all",
|
|
lambda r: r.get_model_ids(exclude_team_models=True),
|
|
lambda r: r.registry().names(kind="ids", exclude_team_models=True),
|
|
),
|
|
(
|
|
"get_model_from_alias",
|
|
lambda r: r.get_model_from_alias("alias-gpt"),
|
|
lambda r: r.registry().resolve("alias-gpt", kind="alias"),
|
|
),
|
|
(
|
|
"is_recognized_model",
|
|
lambda r: r.is_recognized_model("gpt"),
|
|
lambda r: r.registry().resolve("gpt", kind="recognized"),
|
|
),
|
|
(
|
|
"routable_model_group",
|
|
lambda r: r.routable_model_group("alias-gpt"),
|
|
lambda r: r.registry().resolve("alias-gpt"),
|
|
),
|
|
(
|
|
"map_team_model",
|
|
lambda r: r.map_team_model("team-gpt", "team-1"),
|
|
lambda r: r.registry().resolve("team-gpt", kind="team_model", team_id="team-1"),
|
|
),
|
|
(
|
|
"get_model_group",
|
|
lambda r: r.get_model_group("dep-a"),
|
|
lambda r: r.registry().resolve("dep-a", kind="group_of_id"),
|
|
),
|
|
(
|
|
"get_search_tool_support",
|
|
lambda r: r.get_search_tool_support("gpt"),
|
|
lambda r: r.registry().resolve("gpt", kind="search_tool_support"),
|
|
),
|
|
("get_model_access_groups", lambda r: r.get_model_access_groups(), lambda r: r.registry().access_groups()),
|
|
(
|
|
"get_deployment_credentials",
|
|
lambda r: r.get_deployment_credentials("dep-a"),
|
|
lambda r: r.registry().credentials("dep-a"),
|
|
),
|
|
(
|
|
"get_deployment_credentials_with_provider",
|
|
lambda r: r.get_deployment_credentials_with_provider("dep-a"),
|
|
lambda r: r.registry().credentials("dep-a", form="with_provider"),
|
|
),
|
|
(
|
|
"get_credential_deployment",
|
|
lambda r: r.get_credential_deployment("dep-a"),
|
|
lambda r: r.registry().credentials("dep-a", form="deployment"),
|
|
),
|
|
("get_configured_mode", lambda r: r.get_configured_mode("gpt"), lambda r: r.registry().group("gpt", field="mode")),
|
|
(
|
|
"get_configured_token_limits",
|
|
lambda r: r.get_configured_token_limits("gpt"),
|
|
lambda r: r.registry().group("gpt", field="token_limits"),
|
|
),
|
|
(
|
|
"get_fully_blocked_model_names",
|
|
lambda r: r.get_fully_blocked_model_names(),
|
|
lambda r: r.health().fully_blocked_model_names(),
|
|
),
|
|
("get_settings", lambda r: r.get_settings(), lambda r: r.settings().as_dict()),
|
|
("fallbacks", lambda r: r.fallbacks, lambda r: r.settings().fallbacks),
|
|
("cooldown_time", lambda r: r.cooldown_time, lambda r: r.settings().cooldown_time),
|
|
("routing_strategy", lambda r: r.routing_strategy, lambda r: r.settings().routing_strategy),
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("name, old, new", READS, ids=[r[0] for r in READS])
|
|
def test_deprecated_read_matches_entry_point(router, name, old, new):
|
|
with warnings.catch_warnings(record=True) as caught:
|
|
warnings.simplefilter("always")
|
|
old_value = old(router)
|
|
new_value = new(router)
|
|
if isinstance(old_value, (list, dict, set)) or hasattr(old_value, "__dict__"):
|
|
assert old_value == new_value
|
|
else:
|
|
assert old_value == new_value
|
|
assert any(issubclass(w.category, DeprecationWarning) for w in caught), name
|
|
|
|
|
|
def test_live_objects_not_copies(router):
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
assert router.model_list is router.registry().all()
|
|
assert router.fallbacks is router.settings().fallbacks
|
|
assert router.cache is router.health().store
|
|
assert router.model_group_alias is router.registry().aliases()
|
|
|
|
|
|
def test_assignment_goes_through_apply(router):
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
router.fallbacks = [{"gpt": ["openai/gpt-4.1"]}]
|
|
assert router.settings().fallbacks == [{"gpt": ["openai/gpt-4.1"]}]
|
|
router.search_tools = [{"search_tool_name": "s", "litellm_params": {"search_provider": "tavily"}}]
|
|
assert router.settings().search_tools[0]["search_tool_name"] == "s"
|
|
router.apply(ChangeSet(assign={"cooldown_time": 3}))
|
|
assert router.settings().cooldown_time == 3
|
|
with pytest.raises(ValueError, match="not assignable router settings"):
|
|
router.apply(ChangeSet(assign={"not_a_setting": 1}))
|
|
|
|
|
|
def test_apply_upsert_and_delete_bump_version(router):
|
|
v0 = router.registry().version
|
|
added = Deployment(
|
|
model_name="new-group",
|
|
litellm_params={"model": "openai/gpt-4o-mini", "api_key": "sk-n"},
|
|
model_info={"id": "dep-new"},
|
|
)
|
|
result = router.apply(ChangeSet(upsert=[added]))
|
|
assert result.version == v0 + 1
|
|
assert router.registry().has_deployment("dep-new")
|
|
assert router.registry().deployments("new-group")[0]["model_info"]["id"] == "dep-new"
|
|
result = router.apply(ChangeSet(delete=["dep-new"]))
|
|
assert result.version == v0 + 2
|
|
assert result.deleted[0] is not None
|
|
assert not router.registry().has_deployment("dep-new")
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
assert router.upsert_deployment(added) is added
|
|
assert router.upsert_deployment(added) is None # unchanged
|
|
assert router.delete_deployment("dep-new") is not None
|
|
|
|
|
|
def test_old_deployment_writes_also_advance_the_version(router):
|
|
v0 = router.registry().version
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
router.add_deployment(
|
|
Deployment(
|
|
model_name="added",
|
|
litellm_params={"model": "openai/gpt-4o-mini", "api_key": "sk-x"},
|
|
model_info={"id": "dep-added"},
|
|
)
|
|
)
|
|
v1 = router.registry().version
|
|
router.set_model_list([{"model_name": "only", "litellm_params": {"model": "openai/gpt-4o", "api_key": "sk-o"}}])
|
|
assert v0 < v1 < router.registry().version
|
|
assert router.registry().names() == ["only"]
|
|
v2 = router.registry().version
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
router.model_list = []
|
|
assert router.registry().version > v2
|
|
|
|
|
|
def test_update_settings_through_apply(router):
|
|
router.apply(ChangeSet(settings={"cooldown_time": "11", "not_allowed": 1}))
|
|
assert router.settings().cooldown_time == 11
|
|
|
|
|
|
def test_aapply_applies_changes_then_refreshes_model_info(monkeypatch):
|
|
monkeypatch.setattr(litellm, "model_cost", copy.deepcopy(litellm.model_cost))
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "local",
|
|
"litellm_params": {
|
|
"model": "hosted_vllm/local-model",
|
|
"api_base": "https://capacity.test/v1",
|
|
"api_key": "local-key",
|
|
},
|
|
"model_info": {"id": "dep-local"},
|
|
}
|
|
]
|
|
)
|
|
with pytest.raises(TypeError, match="needs aapply"):
|
|
router.apply(ChangeSet(refresh_model_info=True))
|
|
|
|
async def refresh() -> None:
|
|
handler = AsyncHTTPHandler()
|
|
await handler.client.aclose()
|
|
async with httpx.AsyncClient(
|
|
transport=httpx.MockTransport(
|
|
lambda request: httpx.Response(200, json={"data": [{"id": "local-model", "max_model_len": 4096}]})
|
|
)
|
|
) as client:
|
|
handler.client = client
|
|
await router.aapply(ChangeSet(assign={"cooldown_time": 4}, refresh_model_info=True, refresh_client=handler))
|
|
|
|
asyncio.run(refresh())
|
|
assert router.settings().cooldown_time == 4
|
|
assert router.registry().group("local", field="token_limits") == (4096, 4096)
|
|
|
|
|
|
def test_settings_snapshot_is_reused_until_a_setting_is_written(router):
|
|
first = router.settings()
|
|
assert router.settings() is first
|
|
router.apply(ChangeSet(assign={"cooldown_time": 7}))
|
|
assert router.settings() is not first
|
|
assert router.settings().cooldown_time == 7
|
|
router.apply(ChangeSet(settings={"cooldown_time": "8"}))
|
|
assert router.settings().cooldown_time == 8
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
router.fallbacks = [{"gpt": ["team-gpt"]}]
|
|
assert router.settings().fallbacks == [{"gpt": ["team-gpt"]}]
|
|
|
|
|
|
def test_deleting_a_setting_shows_as_unset(router):
|
|
router.apply(ChangeSet(assign={"cooldown_time": 3}))
|
|
assert router.settings().is_set("cooldown_time")
|
|
del router._core_cooldown_time
|
|
assert router.settings().is_set("cooldown_time") is (hasattr(router, "_core_cooldown_time"))
|
|
|
|
|
|
def test_settings_snapshot_is_frozen(router):
|
|
snapshot = router.settings()
|
|
assert isinstance(snapshot, RouterSettings)
|
|
with pytest.raises(dataclasses.FrozenInstanceError):
|
|
snapshot.cooldown_time = 1 # type: ignore[misc]
|
|
assert set(SETTING_FIELDS) <= set(RouterSettings.__dataclass_fields__)
|
|
|
|
|
|
def test_bind_to_request_logging_none_raises_as_before(router):
|
|
# The old member bound unconditionally, so None fails on the first callback registration.
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
with pytest.raises(AttributeError, match="add_dynamic_function_callbacks"):
|
|
router.bind_to_request_logging(None)
|
|
|
|
|
|
def test_select_picks_like_async_get_available_deployment(router):
|
|
picked = asyncio.run(router.select("gpt", {}))
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
old = asyncio.run(router.async_get_available_deployment(model="gpt", request_kwargs={}))
|
|
assert picked["model_name"] == old["model_name"] == "gpt"
|
|
healthy = asyncio.run(router.select("gpt", {}, kind="healthy"))
|
|
assert {d["model_info"]["id"] for d in healthy} == {"dep-a", "dep-b"}
|
|
|
|
|
|
def test_counter_and_internal_silence(router):
|
|
before = DEPRECATED_ROUTER_MEMBER_USES["get_model_ids"]
|
|
with warnings.catch_warnings(record=True) as caught:
|
|
warnings.simplefilter("always")
|
|
router.get_model_ids()
|
|
# Router internals call the core names; a completion must not warn.
|
|
asyncio.run(router.acompletion(model="gpt", messages=[{"role": "user", "content": "hi"}], mock_response="ok"))
|
|
assert DEPRECATED_ROUTER_MEMBER_USES["get_model_ids"] == before + 1
|
|
deprecations = [w for w in caught if issubclass(w.category, DeprecationWarning) and "Router." in str(w.message)]
|
|
assert [str(w.message).split(" ")[0] for w in deprecations] == ["Router.get_model_ids"]
|
|
assert deprecations[0].filename == __file__
|
|
|
|
|
|
def test_every_deprecated_member_names_a_replacement():
|
|
import inspect
|
|
|
|
from litellm.router_deprecated import _DeprecatedAttribute
|
|
|
|
for name, member in vars(DeprecatedRouterMembers).items():
|
|
if name.startswith("__"):
|
|
continue
|
|
if isinstance(member, _DeprecatedAttribute):
|
|
assert member.replacement
|
|
continue
|
|
func = member.__func__ if isinstance(member, staticmethod) else member
|
|
if callable(func):
|
|
assert "_note(" in inspect.getsource(func), name
|
|
|
|
|
|
def test_generate_model_id_is_the_module_function():
|
|
from litellm.router_api import deployment_id
|
|
|
|
params = {"model": "openai/gpt-4o", "api_key": "k", "extra": {"a": 1}}
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
assert Router.generate_model_id("g", params) == deployment_id("g", params)
|
|
|
|
|
|
def test_router_cannot_be_subclassed():
|
|
with pytest.raises(TypeError, match=r"^litellm\.Router cannot be subclassed"):
|
|
|
|
class Sub(litellm.Router):
|
|
pass
|
|
|
|
|
|
def test_core_classes_can_still_be_subclassed():
|
|
from litellm.router_rust.core import RustCore
|
|
|
|
class RustSub(RustCore):
|
|
pass
|
|
|
|
assert issubclass(RustSub, RustCore)
|
|
|
|
|
|
def test_old_name_writes_and_deletes_warn(router):
|
|
with pytest.warns(DeprecationWarning, match=r"^Router\.fallbacks is deprecated"):
|
|
router.fallbacks = []
|
|
with pytest.warns(DeprecationWarning, match=r"^Router\.fallbacks is deprecated"):
|
|
del router.fallbacks
|
|
|
|
|
|
def test_alias_attribute_writes_the_same_storage(router):
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
replacement = {}
|
|
router._routing_groups = replacement
|
|
assert router.routing_groups is replacement
|
|
assert router.registry().routing_groups() is replacement
|
|
|
|
|
|
def test_wrong_arity_errors_still_name_router(router):
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
with pytest.raises(TypeError, match=r"^Router\.get_deployment\(\) takes 2 positional arguments"):
|
|
router.get_deployment("dep-a", "extra") # pyright: ignore[reportCallIssue]
|
|
with pytest.raises(TypeError, match=r"^Router\.get_available_guardrail\(\) missing 1 required"):
|
|
router.get_available_guardrail() # pyright: ignore[reportCallIssue]
|
|
with pytest.raises(TypeError, match=r"^litellm\.router\.Router\.has_model_id\(\) argument after \*\*"):
|
|
router.has_model_id(**None) # pyright: ignore[reportCallIssue, reportArgumentType]
|
|
with pytest.raises(TypeError, match=r"^Router\.is_deployment_blocked\(\) missing 1 required"):
|
|
router.is_deployment_blocked() # pyright: ignore[reportCallIssue]
|
|
|
|
|
|
def test_registry_reads_without_a_core_forward(router):
|
|
registry = router.registry()
|
|
assert registry.includes(router.registry().deployment("dep-a").model_dump(), team_id=None) is True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"call, error",
|
|
[
|
|
(lambda r: r.registry().deployments(source="named"), TypeError),
|
|
(lambda r: r.registry().deployments("gpt", source="nope"), ValueError),
|
|
(lambda r: r.registry().index("nope"), ValueError),
|
|
(lambda r: r.registry().names(kind="litellm_models"), TypeError),
|
|
(lambda r: r.registry().names("gpt", kind="nope"), ValueError),
|
|
(lambda r: r.registry().resolve("team-gpt", kind="team_model"), TypeError),
|
|
(lambda r: r.registry().resolve(None, kind="alias"), TypeError),
|
|
(lambda r: r.registry().resolve("gpt", kind="nope"), ValueError),
|
|
(lambda r: r.registry().group("gpt", field="nope"), ValueError),
|
|
(lambda r: r.registry().credentials("dep-a", form="nope"), ValueError),
|
|
(lambda r: r.registry().plugins("nope"), ValueError),
|
|
(lambda r: asyncio.run(r.health().usage("gpt", kind="nope")), ValueError),
|
|
(lambda r: RouterSettings().as_dict(), AttributeError),
|
|
],
|
|
)
|
|
def test_entry_points_reject_unknown_kinds_and_missing_names(router, call, error):
|
|
with pytest.raises(error):
|
|
call(router)
|
|
|
|
|
|
def test_health_is_blocked_and_the_old_static_name_agree():
|
|
paused = Deployment(
|
|
model_name="m",
|
|
litellm_params={"model": "openai/gpt-4o-mini"}, # pyright: ignore[reportArgumentType]
|
|
model_info={"id": "d1", "blocked": True}, # pyright: ignore[reportArgumentType]
|
|
)
|
|
from litellm.router_api import RouterHealth
|
|
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
assert Router.is_deployment_blocked(paused) is RouterHealth.is_blocked(paused)
|
|
|
|
|
|
def test_old_members_refuse_objects_that_are_not_routers():
|
|
from litellm.router_deprecated import _as_router
|
|
|
|
with pytest.raises(TypeError, match=r"is not a litellm\.Router"):
|
|
_as_router(object()) # pyright: ignore[reportArgumentType]
|
|
|
|
|
|
def test_routing_context_forwards_to_the_router(router):
|
|
from litellm.router_api import RoutingContext
|
|
|
|
ctx = RoutingContext(router)
|
|
assert ctx.registry().version == router.registry().version
|
|
assert ctx.settings() is router.settings()
|
|
assert isinstance(ctx.health(), type(router.health()))
|
|
|
|
|
|
def test_attribute_errors_about_other_objects_pass_through_unchanged(router):
|
|
from litellm.router_deprecated import _public_attribute_error
|
|
|
|
other = AttributeError("'dict' object has no attribute 'x'", name="x", obj={})
|
|
assert _public_attribute_error(router, "_core_x", "x", other) is other
|
|
|
|
|
|
_MINTING_DEPLOYMENT = "dep-minting-org"
|
|
_OTHER_ORG_DEPLOYMENT = "dep-other-org"
|
|
_BOTH_ORGS = (_MINTING_DEPLOYMENT, _OTHER_ORG_DEPLOYMENT)
|
|
_GROUP_AFFINITY = ["encrypted_content_affinity"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"affinity, target_ids, reasoning_stays",
|
|
[
|
|
pytest.param({}, _BOTH_ORGS, False, id="no affinity, one target cannot decrypt"),
|
|
pytest.param({}, (_MINTING_DEPLOYMENT,), True, id="no affinity, the only target minted it"),
|
|
pytest.param(
|
|
{"optional_pre_call_checks": _GROUP_AFFINITY}, _BOTH_ORGS, True, id="global affinity, origin among targets"
|
|
),
|
|
pytest.param(
|
|
{"optional_pre_call_checks": _GROUP_AFFINITY},
|
|
(_OTHER_ORG_DEPLOYMENT,),
|
|
False,
|
|
id="global affinity, origin not among targets",
|
|
),
|
|
pytest.param(
|
|
{"optional_pre_call_checks": [], "model_group_affinity_config": {"pair": _GROUP_AFFINITY}},
|
|
_BOTH_ORGS,
|
|
True,
|
|
id="affinity on for this group",
|
|
),
|
|
pytest.param(
|
|
{"optional_pre_call_checks": [], "model_group_affinity_config": {"other-group": _GROUP_AFFINITY}},
|
|
_BOTH_ORGS,
|
|
False,
|
|
id="affinity on for another group only",
|
|
),
|
|
],
|
|
)
|
|
def test_decryptable_reasoning_keeps_encrypted_reasoning_only_when_the_router_sends_it_to_a_deployment_that_decrypts_it(
|
|
affinity, target_ids, reasoning_stays
|
|
):
|
|
from litellm.responses.utils import ResponsesAPIRequestUtils
|
|
|
|
r = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "pair",
|
|
"litellm_params": {"model": "openai/gpt-4o-mini", "api_base": api_base, "api_key": "sk-pair"},
|
|
"model_info": {"id": model_id},
|
|
}
|
|
for api_base, model_id in (
|
|
("https://minting-org.test/v1", _MINTING_DEPLOYMENT),
|
|
("https://other-org.test/v1", _OTHER_ORG_DEPLOYMENT),
|
|
)
|
|
],
|
|
**affinity,
|
|
)
|
|
summary = [{"type": "summary_text", "text": "multiply 17 by 23"}]
|
|
user_turn = {"type": "message", "role": "user", "content": "What is 17*23?"}
|
|
reasoning = {
|
|
"type": "reasoning",
|
|
"id": "rs_minted",
|
|
"encrypted_content": ResponsesAPIRequestUtils.wrap_encrypted_content_with_model_id(
|
|
"gAAAAA-blob", _MINTING_DEPLOYMENT
|
|
),
|
|
"summary": summary,
|
|
}
|
|
request_input = [user_turn, reasoning]
|
|
targets = tuple(r.registry().deployment(model_id) for model_id in target_ids)
|
|
try:
|
|
kept = r.registry().decryptable_reasoning("pair", request_input, targets)
|
|
finally:
|
|
r.retire()
|
|
|
|
readable_only = {"type": "reasoning", "summary": summary}
|
|
assert kept == (user_turn, reasoning if reasoning_stays else readable_only)
|
|
assert request_input == [user_turn, reasoning], "the caller's input must not change"
|
|
|
|
|
|
# Router-family helpers whose Router parameter became a RoutingContext keep the old spellings.
|
|
|
|
|
|
def test_renamed_router_keyword_still_works_and_warns(router):
|
|
from litellm.router_utils.router_callbacks.track_deployment_metrics import (
|
|
get_deployment_failures_for_current_minute,
|
|
increment_deployment_failures_for_current_minute,
|
|
)
|
|
|
|
deployment_id = router.registry().names(kind="ids")[0]
|
|
label = (
|
|
"litellm.router_utils.router_callbacks.track_deployment_metrics."
|
|
"increment_deployment_failures_for_current_minute"
|
|
)
|
|
before = DEPRECATED_ROUTER_MEMBER_USES[label]
|
|
|
|
with pytest.warns(DeprecationWarning, match=r"litellm_router_instance= is deprecated; pass ctx="):
|
|
increment_deployment_failures_for_current_minute(litellm_router_instance=router, deployment_id=deployment_id)
|
|
|
|
assert DEPRECATED_ROUTER_MEMBER_USES[label] == before + 1
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("error")
|
|
assert get_deployment_failures_for_current_minute(ctx=router._core_ctx, deployment_id=deployment_id) == 1
|
|
|
|
|
|
def test_router_passed_where_the_context_goes_is_converted_with_a_warning(router):
|
|
from litellm.router_utils.cooldown_handlers import get_cooldown_deployments
|
|
|
|
with pytest.warns(DeprecationWarning, match="passing a Router is deprecated"):
|
|
assert get_cooldown_deployments(router, None) == []
|
|
with pytest.warns(DeprecationWarning, match="a Router as ctx= is deprecated"):
|
|
assert get_cooldown_deployments(ctx=router, parent_otel_span=None) == []
|
|
|
|
|
|
def test_renamed_router_keyword_alongside_the_new_one_is_rejected(router):
|
|
from litellm.router_utils.cooldown_handlers import get_cooldown_deployments
|
|
|
|
with pytest.raises(TypeError, match="got both litellm_router_instance= and ctx="):
|
|
get_cooldown_deployments(ctx=router._core_ctx, litellm_router_instance=router, parent_otel_span=None)
|
|
|
|
|
|
def test_renamed_router_param_maps_to_a_differently_named_context(router):
|
|
from litellm.router_api import RoutingContext
|
|
from litellm.router_deprecated import renamed_router_param
|
|
|
|
@renamed_router_param("litellm_router", new="litellm_routing_ctx")
|
|
def helper(*, litellm_routing_ctx: RoutingContext) -> RoutingContext:
|
|
return litellm_routing_ctx
|
|
|
|
with pytest.warns(DeprecationWarning, match="litellm_router= is deprecated; pass litellm_routing_ctx="):
|
|
assert helper(litellm_router=router) is router._core_ctx
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("error")
|
|
assert helper(litellm_routing_ctx=router._core_ctx) is router._core_ctx
|
|
|
|
|
|
def test_renamed_router_attribute_reads_and_writes_the_context(router):
|
|
from litellm.router_utils.pre_call_checks.encrypted_content_affinity_check import (
|
|
EncryptedContentAffinityCheck,
|
|
)
|
|
|
|
check = EncryptedContentAffinityCheck(ctx=router._core_ctx)
|
|
with pytest.warns(DeprecationWarning, match=r"EncryptedContentAffinityCheck\.router: reading this attribute"):
|
|
assert check.router is router._core_ctx
|
|
|
|
other = _router()
|
|
with pytest.warns(DeprecationWarning, match="setting this attribute is deprecated; set .ctx"):
|
|
check.router = other
|
|
assert check.ctx is other._core_ctx
|
|
with warnings.catch_warnings():
|
|
warnings.simplefilter("ignore")
|
|
other.retire()
|