Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(router): consume router-selecting tags on litellm_metadata-shaped requests too

mateo-berribff10db90f14
diff --git a/litellm/router_strategy/tag_based_routing.py b/litellm/router_strategy/tag_based_routing.py
index 0d22ebe0e49..323aac23aca 100644
--- a/litellm/router_strategy/tag_based_routing.py
+++ b/litellm/router_strategy/tag_based_routing.py
@@ -14,6 +14,7 @@ from typing import TYPE_CHECKING, Any, Final, Literal
from litellm._logging import verbose_logger
from litellm.constants import CONSUMED_REQUEST_TAGS_MODEL_GROUP_METADATA_KEY
+from litellm.litellm_core_utils.core_helpers import get_metadata_variable_name_from_kwargs
from litellm.types.router import RouterErrors
if TYPE_CHECKING:
@@ -578,26 +579,30 @@ async def get_deployments_for_tag(
def _get_tags_from_request_kwargs(
request_kwargs: dict[Any, Any] | None = None,
- metadata_variable_name: Literal["metadata", "litellm_metadata"] = "metadata",
+ metadata_variable_name: Literal["metadata", "litellm_metadata"] | None = None,
) -> list[str]:
"""
Helper to get tags from request kwargs
Args:
request_kwargs: The request kwargs to get tags from
+ metadata_variable_name: Which metadata dict holds proxy metadata; resolved
+ from the kwargs when not pinned, so /v1/messages-shaped requests
+ (``litellm_metadata``) read the same bucket the proxy wrote tags to
Returns:
List[str]: The tags from the request kwargs
"""
if request_kwargs is None:
return []
- if metadata_variable_name in request_kwargs:
- metadata: Final = request_kwargs[metadata_variable_name] or {}
+ resolved_variable_name: Final = metadata_variable_name or get_metadata_variable_name_from_kwargs(request_kwargs)
+ if resolved_variable_name in request_kwargs:
+ metadata: Final = request_kwargs[resolved_variable_name] or {}
tags = metadata.get("tags", [])
return tags if tags is not None else []
elif "litellm_params" in request_kwargs:
litellm_params: Final = request_kwargs["litellm_params"] or {}
- _metadata: Final = litellm_params.get(metadata_variable_name, {}) or {}
+ _metadata: Final = litellm_params.get(resolved_variable_name, {}) or {}
tags = _metadata.get("tags", [])
return tags if tags is not None else []
return []
diff --git a/tests/test_litellm/router_strategy/test_router_tag_routing.py b/tests/test_litellm/router_strategy/test_router_tag_routing.py
index 7918e1c63cf..f5651e383c5 100644
--- a/tests/test_litellm/router_strategy/test_router_tag_routing.py
+++ b/tests/test_litellm/router_strategy/test_router_tag_routing.py
@@ -2874,6 +2874,22 @@ async def test_router_selecting_tag_is_not_reapplied_to_the_routed_tier():
assert response._hidden_params["model_id"] == "tier-gemini-flash"
+async def test_router_selecting_tag_is_consumed_on_litellm_metadata_shaped_requests():
+ # /v1/messages (and other litellm_metadata endpoints) store proxy metadata,
+ # including x-litellm-tags header tags, under "litellm_metadata"; consumption
+ # must read and stamp that same bucket instead of only "metadata".
+ router = _tagged_marker_router()
+
+ deployment = await router.async_get_available_deployment(
+ model="gpt4o",
+ request_kwargs={"litellm_metadata": {"tags": ["route"], "inherited_tags": []}},
+ messages=[{"role": "user", "content": "What is the capital of France?"}],
+ )
+
+ assert deployment["model_info"]["id"] == "tier-gemini-flash"
+
+
@pytest.mark.asyncio()
async def test_tagged_request_direct_to_plain_group_still_rejected():
# Sent straight to the tier, no router selection consumed the tag, so strict
diff --git a/tests/test_litellm/test_router.py b/tests/test_litellm/test_router.py
index 71102f36aba..fdccc46e5cb 100644
--- a/tests/test_litellm/test_router.py
+++ b/tests/test_litellm/test_router.py
@@ -7519,6 +7519,17 @@ class TestConsumedRequestTagsStamp:
assert request_kwargs["metadata"][CONSUMED_REQUEST_TAGS_MODEL_GROUP_METADATA_KEY] == "gemini-flash"
+ @pytest.mark.asyncio
+ async def test_stamps_into_litellm_metadata_when_the_request_uses_that_bucket(self):
+ from litellm.constants import CONSUMED_REQUEST_TAGS_MODEL_GROUP_METADATA_KEY
+
+ router = self._router()
+ request_kwargs = {"litellm_metadata": {"tags": ["route"]}}
+
+ await router.async_pre_routing_hook(model="gpt4o", request_kwargs=request_kwargs)
+
+ assert request_kwargs["litellm_metadata"][CONSUMED_REQUEST_TAGS_MODEL_GROUP_METADATA_KEY] == "gemini-flash"
+
@pytest.mark.asyncio
async def test_fallback_reentry_with_a_plain_group_clears_the_stale_stamp(self):
from litellm.constants import CONSUMED_REQUEST_TAGS_MODEL_GROUP_METADATA_KEY