litellm/tests/unit/router_strategy/adaptive_router/test_hooks.py
Kazuki Hashimoto c8ed5ccca8
fix(router): unregister strategy selectors from global callbacks on discard (#45795)
* fix(router): unregister strategy selectors from global callbacks on discard

Router.discard only removed its own bound methods from litellm's callback
lists, so the strategy selectors it registered (least-busy, usage-based,
latency-based, cost-based and routing group selectors) stayed behind and
kept receiving events after the router was gone. Reuse the identity based
unregister that update_settings already uses so a discarded router takes
only its own selectors with it

* fix(router): purge discarded selectors from every event callback list

function_setup copies litellm.callbacks into the success and failure
lists on every request, so a router that served traffic still leaked its
selectors there after discard. Route the cleanup through
remove_callback_from_all_lists, which now also covers input_callback, and
fold the selector gathering into discard so the router coverage check
sees no untested helper

* fix(router): keep live routers, in-flight hooks and user_config limits intact on discard

discard() now rebuilds every global callback list without the router's selectors, optional pre-call checks and own usage callbacks instead of removing them in place, so a callback loop already running never skips a hook. It then re-registers what the remaining live routers need, so a same-strategy survivor keeps its deduped handler. The proxy's per-request user_config router is discarded only after its call returns.

* fix(router): bind user_config handlers to the request and tighten discard

When a user_config request carries a logging object, its router is
discarded before the request runs and its handlers ride on that
logging object, so v1 rpm and v2 tpm limits count every request and
concurrent requests count once. Without one, the router is discarded
after the request as before. The router's pre-call loops also read
the request-scoped handlers.

discard() now refills only the handler keys it vacated, guards the
callback lists and the live-router set with a lock, covers routers
built without a model_list, and drops its Slack alerting, adaptive
router hook and budget limiter sync task

* fix(proxy): reuse one user_config router per config and bind its handlers to each request

Every user_config request now goes through a cached router built once per
config. Its strategy handlers leave the global callback lists and are bound
to the request's logging object instead, so limits count each request once
and nothing is left behind after the request.

* fix(router): keep the proxy budget limiter on user_config requests

A user_config router now swaps in only its strategy selectors, so the proxy's
RouterBudgetLimiting stays on the request and keeps counting its spend. Usage
logged after a strategy is retired, such as a request still in flight when its
user_config router is evicted, now schedules one more push to Redis.

* fix(proxy): init the retire fields on the key model budget limiter

The key model budget limiter builds its own state instead of calling the base
init, so with the proxy's Redis attached every spend increment raised on the
missing retire flag and the key's model budget never counted spend.

* fix(router): bind the deployment callbacks to user_config requests

A cached user_config router serves requests through each request's logging
object but bound only its selectors and policies there, so a failing
deployment was never cooled down and its usage counters missed requests.

* fix(router): retry an evicted router's budget writes and keep the adaptive header on user_config requests

A retired budget limiter now keeps retrying its queued Redis increments every
sync interval until Redis accepts them, instead of giving up after one failed
write.

The x-litellm-adaptive-router-model header now comes from one stateless hook in
litellm.callbacks that holds no router, so user_config adaptive routers, whose
per-router hooks are request-scoped, still answer with the header.

* fix(router): lock request callback setup against router cleanup and drain in-flight budget writes on retire

function_setup now edits the global callback lists under the same lock that
router cleanup uses, so a router dropping its callbacks on another thread can
no longer shift the indexes function_setup pops or put a dropped handler back.

A budget limiter retired while a Redis write is still in flight now keeps
retrying that batch if the write fails, instead of leaving the spend queued
with no task left to send it.

* fix(utils): set up loggers after releasing the callback lists lock

function_setup held the callback lists lock while set_callbacks ran its one-time
logger setup, which can block on a Slack package install, so other threads and
Router.discard waited on it. The lock now covers only the list reads and writes.

* test(router): annotate the retire test's locals as Final

* fix(logging): skip replaced global callbacks on the input path too

replace_global_callback already kept a same-key global logger out of
success and failure dispatch. pre_call and post_call still sent it the
input events, so a logger with an input hook could count a request start
it never saw finish. Both now share one filter with the success path

* test(logging): type the input-path regression test

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-21 02:39:36 +00:00

376 lines
13 KiB
Python

"""Unit tests for the AdaptiveRouterPostCallHook."""
from unittest.mock import AsyncMock, MagicMock
import pytest
from litellm.router_strategy.adaptive_router.config import (
ADAPTIVE_ROUTER_CHOSEN_MODEL_KEY,
SIGNAL_GATE_MIN_MESSAGES,
)
from litellm.router_strategy.adaptive_router.hooks import (
AdaptiveRouterPostCallHook,
AdaptiveRouterResponseHeaderHook,
_recent_tool_results,
_resolve_session_key,
)
from litellm.router_strategy.adaptive_router.signals import Turn
def _make_hook() -> AdaptiveRouterPostCallHook:
fake_router = MagicMock()
fake_router.record_turn = AsyncMock()
return AdaptiveRouterPostCallHook(adaptive_router=fake_router)
def _resp_with_content(text: str, tool_calls=None):
"""Build a ModelResponse-like object with a single assistant message."""
msg = MagicMock()
msg.content = text
msg.tool_calls = tool_calls or []
choice = MagicMock()
choice.message = msg
resp = MagicMock()
resp.choices = [choice]
return resp
def _long_messages(user_text: str = "ask"):
"""Return a message list at the SIGNAL_GATE_MIN_MESSAGES threshold."""
base = [
{"role": "user", "content": "first turn"},
{"role": "assistant", "content": "first reply"},
{"role": "user", "content": "second turn"},
]
base.append({"role": "user", "content": user_text})
# Pad to threshold if needed.
while len(base) < SIGNAL_GATE_MIN_MESSAGES:
base.append({"role": "user", "content": "filler"})
return base
def _kwargs(
*,
messages=None,
chosen="fast",
extra_metadata=None,
extra_litellm_params=None,
):
metadata = {ADAPTIVE_ROUTER_CHOSEN_MODEL_KEY: chosen} if chosen else {}
if extra_metadata:
metadata.update(extra_metadata)
lp = {"metadata": metadata}
if extra_litellm_params:
lp.update(extra_litellm_params)
return {
"model": "anthropic/claude-opus-4-7",
"messages": messages if messages is not None else _long_messages(),
"litellm_params": lp,
}
# ---- _resolve_session_key ------------------------------------------------
def test_resolve_session_key_honors_litellm_session_id_on_litellm_params():
key = _resolve_session_key({"litellm_params": {"litellm_session_id": "sess-A"}})
assert key == "sess-A"
def test_resolve_session_key_honors_metadata_session_id():
key = _resolve_session_key(
{"litellm_params": {"metadata": {"session_id": "sess-B"}}}
)
assert key == "sess-B"
def test_resolve_session_key_returns_none_when_no_messages():
assert _resolve_session_key({"litellm_params": {}}) is None
assert _resolve_session_key({"litellm_params": {}, "messages": []}) is None
def test_resolve_session_key_derives_stable_hash_from_first_message():
# `_resolve_session_key` requires at least SIGNAL_GATE_MIN_MESSAGES
# messages before it will derive a hash (matches the signal-processing
# gate) — otherwise the session is too short to attribute.
msgs = _long_messages("Hello, world")
k1 = _resolve_session_key({"messages": msgs})
k2 = _resolve_session_key({"messages": list(msgs)})
assert k1 == k2
assert k1 and len(k1) == 64 # sha256 hex
def test_resolve_session_key_does_not_prefix_sk():
key = _resolve_session_key({"messages": _long_messages()})
assert key and not key.startswith("sk_")
def test_resolve_session_key_segments_by_identity_fields():
"""Same first message but different api keys must yield different keys."""
msgs = _long_messages("same prompt")
k_team_a = _resolve_session_key(
{
"messages": msgs,
"litellm_params": {
"metadata": {
"user_api_key_hash": "hash-A",
"user_api_key_team_id": "team-1",
}
},
}
)
k_team_b = _resolve_session_key(
{
"messages": msgs,
"litellm_params": {
"metadata": {
"user_api_key_hash": "hash-B",
"user_api_key_team_id": "team-2",
}
},
}
)
assert k_team_a != k_team_b
def test_resolve_session_key_changes_when_first_message_changes():
k1 = _resolve_session_key({"messages": _long_messages("alpha")})
k2 = _resolve_session_key({"messages": _long_messages("beta")})
assert k1 != k2
# ---- _record gating -----------------------------------------------------
@pytest.mark.asyncio
async def test_hook_skips_when_below_signal_gate():
"""Conversations shorter than SIGNAL_GATE_MIN_MESSAGES should be ignored."""
hook = _make_hook()
short = [{"role": "user", "content": "hi"}]
assert len(short) < SIGNAL_GATE_MIN_MESSAGES # sanity
kwargs = _kwargs(messages=short)
await hook.async_log_success_event(kwargs, _resp_with_content("ok"), 0.0, 1.0)
hook.adaptive_router.record_turn.assert_not_awaited()
@pytest.mark.asyncio
async def test_hook_tracks_short_conversation_with_explicit_session_id():
hook = _make_hook()
kwargs = _kwargs(
messages=[{"role": "user", "content": "hi"}],
extra_litellm_params={"litellm_session_id": "explicit-short"},
)
await hook.async_log_success_event(
kwargs,
_resp_with_content("hello"),
0.0,
1.0,
)
assert hook.adaptive_router.record_turn.await_args.kwargs["session_id"] == (
"explicit-short"
)
@pytest.mark.asyncio
async def test_hook_skips_when_no_messages():
hook = _make_hook()
kwargs = _kwargs(messages=[])
await hook.async_log_success_event(kwargs, _resp_with_content("ok"), 0.0, 1.0)
hook.adaptive_router.record_turn.assert_not_awaited()
@pytest.mark.asyncio
async def test_hook_skips_when_chosen_model_missing_from_metadata():
hook = _make_hook()
kwargs = _kwargs(chosen=None)
await hook.async_log_success_event(kwargs, _resp_with_content("ok"), 0.0, 1.0)
hook.adaptive_router.record_turn.assert_not_awaited()
@pytest.mark.asyncio
async def test_hook_records_when_model_changes():
hook = _make_hook()
kwargs = _kwargs(chosen="fast")
await hook.async_log_success_event(kwargs, _resp_with_content("ok"), 0.0, 1.0)
hook.adaptive_router.record_turn.assert_awaited_once()
@pytest.mark.asyncio
async def test_hook_records_turn():
hook = _make_hook()
kwargs = _kwargs(chosen="smart", messages=_long_messages("ask"))
await hook.async_log_success_event(
kwargs, _resp_with_content("answer here"), 0.0, 1.0
)
call = hook.adaptive_router.record_turn.await_args
assert call.kwargs["model_name"] == "smart"
turn: Turn = call.kwargs["turn"]
assert turn.user_content == "ask"
assert turn.assistant_content == "answer here"
assert turn.response_status == 200
@pytest.mark.asyncio
async def test_hook_uses_explicit_session_id_when_provided():
"""Explicit `litellm_session_id` is forwarded as the session key."""
hook = _make_hook()
kwargs = _kwargs(
chosen="fast",
extra_litellm_params={"litellm_session_id": "explicit-sess"},
)
await hook.async_log_success_event(kwargs, _resp_with_content("ok"), 0.0, 1.0)
assert hook.adaptive_router.record_turn.await_args.kwargs["session_id"] == (
"explicit-sess"
)
@pytest.mark.asyncio
async def test_hook_passes_tool_calls_through():
hook = _make_hook()
tc = {"name": "search", "arguments": '{"q":"x"}'}
kwargs = _kwargs(chosen="fast")
await hook.async_log_success_event(
kwargs, _resp_with_content("calling tool", tool_calls=[tc]), 0.0, 1.0
)
turn: Turn = hook.adaptive_router.record_turn.await_args.kwargs["turn"]
assert turn.tool_calls == [tc]
# ---- _recent_tool_results ------------------------------------------------
def test_recent_tool_results_empty_when_no_messages():
assert _recent_tool_results(None) == []
assert _recent_tool_results([]) == []
def test_recent_tool_results_collects_trailing_tool_messages():
"""Tool messages at the tail of the conversation are extracted in order."""
messages = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": None, "tool_calls": [{"id": "t1"}]},
{"role": "tool", "tool_call_id": "t1", "content": "result A"},
{"role": "tool", "tool_call_id": "t2", "content": "result B"},
]
results = _recent_tool_results(messages)
assert [r["content"] for r in results] == ["result A", "result B"]
assert all(r["is_error"] is False for r in results)
def test_recent_tool_results_propagates_is_error_flag():
messages = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": None, "tool_calls": [{"id": "t1"}]},
{"role": "tool", "content": "boom", "is_error": True},
]
results = _recent_tool_results(messages)
assert results == [{"content": "boom", "is_error": True}]
def test_recent_tool_results_stops_at_first_non_tool_message():
"""Only the trailing run of tool messages counts — prior rounds are
considered already attributed."""
messages = [
{"role": "user", "content": "hi"},
{"role": "tool", "content": "stale"}, # earlier round, ignored
{"role": "assistant", "content": "intermediate"},
{"role": "user", "content": "follow-up"},
{"role": "tool", "content": "current"},
]
results = _recent_tool_results(messages)
assert [r["content"] for r in results] == ["current"]
def test_recent_tool_results_empty_when_no_trailing_tool_message():
messages = [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "hello"},
]
assert _recent_tool_results(messages) == []
@pytest.mark.asyncio
async def test_hook_passes_tool_results_to_turn_for_failure_detection():
"""A trailing tool message with `is_error` must reach `Turn.tool_results`
so the failure-signal path fires."""
hook = _make_hook()
messages = _long_messages()
messages.append(
{"role": "assistant", "content": None, "tool_calls": [{"id": "t1"}]}
)
messages.append(
{"role": "tool", "tool_call_id": "t1", "content": "500", "is_error": True}
)
kwargs = _kwargs(chosen="fast", messages=messages)
await hook.async_log_success_event(kwargs, _resp_with_content("ok"), 0.0, 1.0)
turn: Turn = hook.adaptive_router.record_turn.await_args.kwargs["turn"]
assert turn.tool_results == [{"content": "500", "is_error": True}]
@pytest.mark.asyncio
async def test_hook_swallows_exceptions_from_record_turn():
hook = _make_hook()
hook.adaptive_router.record_turn.side_effect = RuntimeError("boom")
kwargs = _kwargs(chosen="fast")
# Must NOT raise — signal recording must never break a request.
await hook.async_log_success_event(kwargs, _resp_with_content("ok"), 0.0, 1.0)
@pytest.mark.asyncio
async def test_hook_failure_event_uses_status_code_from_exception():
hook = _make_hook()
exc = MagicMock()
exc.status_code = 429
kwargs = _kwargs(chosen="fast")
kwargs["exception"] = exc
await hook.async_log_failure_event(kwargs, None, 0.0, 1.0)
turn: Turn = hook.adaptive_router.record_turn.await_args.kwargs["turn"]
assert turn.response_status == 429
# ---- async_post_call_success_hook (response header surfacing) ----------
@pytest.mark.asyncio
async def test_post_call_response_headers_hook_returns_chosen_model_header():
"""The header hook returns the `x-litellm-adaptive-router-model` header
so proxy header construction picks it up (works for both streaming and
non-streaming; `async_post_call_success_hook` is too late for streaming)."""
headers = await AdaptiveRouterResponseHeaderHook().async_post_call_response_headers_hook(
data={"metadata": {"adaptive_router_chosen_model": "smart"}},
user_api_key_dict=MagicMock(),
response=MagicMock(),
)
assert headers == {"x-litellm-adaptive-router-model": "smart"}
@pytest.mark.asyncio
async def test_post_call_response_headers_hook_noop_when_metadata_missing_key():
headers = await AdaptiveRouterResponseHeaderHook().async_post_call_response_headers_hook(
data={"metadata": {"litellm_session_id": "sess-A"}},
user_api_key_dict=MagicMock(),
response=MagicMock(),
)
assert headers is None
@pytest.mark.asyncio
async def test_post_call_response_headers_hook_noop_when_no_metadata():
headers = await AdaptiveRouterResponseHeaderHook().async_post_call_response_headers_hook(
data={},
user_api_key_dict=MagicMock(),
response=MagicMock(),
)
assert headers is None
@pytest.mark.asyncio
async def test_post_call_response_headers_hook_noop_when_metadata_not_dict():
headers = await AdaptiveRouterResponseHeaderHook().async_post_call_response_headers_hook(
data={"metadata": "not-a-dict"},
user_api_key_dict=MagicMock(),
response=MagicMock(),
)
assert headers is None