litellm/tests/unit/router_utils/test_baseline_request.py
tin-berri 0338498067
fix(router): preserve native baseline identity and accounting (#44960)
* fix(router): preserve native baseline identity and accounting

* fix(router): preserve injected system caches in native baselines

* fix(router): prepare native baselines through shared request owners

* fix(router): capture native baseline fields from the provider schema

* refactor(router): reuse native provider parameter discovery

* fix(router): keep long native baselines and abstain after compaction

Message history no longer counts against the settings snapshot budget, so long
and non-ASCII native sessions keep modeled baselines. Selected-tier compaction
now abstains because the baseline would otherwise inherit the compacted history.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(router): judge implicit caching against the selected request

The implicit-cache guard compared the selected response's cache usage with the
projected baseline's breakpoints, so selected-tier cache markers made a usable
unmarked baseline plan look like unexplained caching. The guard now checks the
selected wire request. Also removes a stamp-reuse branch that could never run
because routing clears the stamp first; every pass already captures caller
settings from fresh kwargs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 11:00:31 -07:00

59 lines
2.3 KiB
Python

from typing import Final
from litellm.router_utils.baseline_request import baseline_request, capture_baseline_parameters
def test_baseline_snapshot_owns_nested_caller_settings_and_overrides_routed_settings() -> None:
reasoning: Final = {"effort": "medium"}
snapshot: Final = capture_baseline_parameters({"reasoning": reasoning, "verbosity": "low"})
assert snapshot is not None
reasoning["effort"] = "high"
projected: Final = baseline_request(
{"messages": [{"role": "user", "content": "hello"}], "reasoning": reasoning, "verbosity": "high"},
snapshot,
{"verbosity": "medium"},
)
assert projected == {
"messages": [{"role": "user", "content": "hello"}],
"reasoning": {"effort": "medium"},
"verbosity": "low",
}
def test_oversized_snapshot_fails_closed_before_json_validation() -> None:
assert capture_baseline_parameters({"output_config": {"format": "x" * 5_000_000}}) is None
def test_snapshot_retains_extra_body_settings_but_no_credentials() -> None:
assert capture_baseline_parameters({"api_key": "private", "extra_body": {"verbosity": "low"}}) == {
"extra_body": {"verbosity": "low"}
}
def test_chat_projection_applies_extra_body_after_top_level_parameters() -> None:
snapshot: Final = capture_baseline_parameters({"verbosity": "high", "extra_body": {"verbosity": "low"}})
assert snapshot is not None
assert baseline_request({}, snapshot, {}) == {"verbosity": "low"}
def test_baseline_projection_keeps_caller_tools_and_request_parameter_precedence() -> None:
from litellm.router import Router
deployment: Final = {
"tools": [{"type": "function", "function": {"name": "configured"}}],
"tool_choice": "required",
"max_tokens": 64,
}
caller: Final = {
"tools": [{"type": "function", "function": {"name": "caller"}}],
"tool_choice": "auto",
"max_tokens": 128,
}
actual_request: Final = dict(caller)
Router._merge_tools_from_deployment({"litellm_params": deployment}, actual_request)
snapshot: Final = capture_baseline_parameters(caller)
assert snapshot is not None
assert baseline_request({"tools": [{"name": "routed-only"}], "max_tokens": 4}, snapshot, deployment) == {
**deployment,
**actual_request,
}