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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(router): read no tags, instead of raising, from a non-dict metadata bucket
mateo-berrice3d30a43414
diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.pyindex 2462c282041..de1092bc02f 100644--- a/litellm/litellm_core_utils/core_helpers.py+++ b/litellm/litellm_core_utils/core_helpers.py@@ -1,7 +1,7 @@# What is this?## Helper utilitiesimport copy-from collections.abc import Iterable+from collections.abc import Iterable, Mappingfrom typing import TYPE_CHECKING, Any, Final, Literalimport httpx@@ -181,7 +181,7 @@ def add_missing_spend_metadata_to_litellm_metadata(litellm_metadata: dict, metaddef get_metadata_variable_name_from_kwargs(- kwargs: dict,+ kwargs: Mapping[str, object],) -> Literal["metadata", "litellm_metadata"]:"""Helper to return what the "metadata" field should be called in the request datadiff --git a/litellm/router_strategy/tag_based_routing.py b/litellm/router_strategy/tag_based_routing.pyindex 40ed89ebfd8..1120323b4f9 100644--- a/litellm/router_strategy/tag_based_routing.py+++ b/litellm/router_strategy/tag_based_routing.py@@ -584,8 +584,26 @@ async def get_deployments_for_tag(return healthy_deployments+def _tags_in_metadata(metadata: object) -> list[str]:+ """+ Tags out of a metadata bucket the caller controls the shape of.++ A request can send its metadata (and its ``tags``) as anything the JSON body+ allowed, an unparsed string or null included, so any shape that is not a list+ of string tags carries no tags rather than raising.+ """+ if not isinstance(metadata, Mapping):+ return []+ typed_metadata: Final[Mapping[str, object]] = metadata+ tags: Final = typed_metadata.get("tags")+ if isinstance(tags, str) or not isinstance(tags, Sequence):+ return []+ typed_tags: Final[Sequence[object]] = tags+ return [tag for tag in typed_tags if isinstance(tag, str)]++def _get_tags_from_request_kwargs(- request_kwargs: dict[Any, Any] | None = None,+ request_kwargs: Mapping[Any, Any] | None = None,metadata_variable_name: Literal["metadata", "litellm_metadata"] | None = None,) -> list[str]:"""@@ -604,12 +622,11 @@ def _get_tags_from_request_kwargs(return []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(resolved_variable_name, {}) or {}- tags = _metadata.get("tags", [])- return tags if tags is not None else []+ return _tags_in_metadata(request_kwargs[resolved_variable_name])+ if "litellm_params" in request_kwargs:+ litellm_params: Final = request_kwargs["litellm_params"]+ if not isinstance(litellm_params, Mapping):+ return []+ typed_litellm_params: Final[Mapping[str, object]] = litellm_params+ return _tags_in_metadata(typed_litellm_params.get(resolved_variable_name))return []diff --git a/tests/test_litellm/router_strategy/test_router_tag_routing.py b/tests/test_litellm/router_strategy/test_router_tag_routing.pyindex 93011bb29cc..73491490b14 100644--- a/tests/test_litellm/router_strategy/test_router_tag_routing.py+++ b/tests/test_litellm/router_strategy/test_router_tag_routing.py@@ -423,6 +423,33 @@ def test_get_tags_from_request_kwargs_various_inputs():assert _get_tags_from_request_kwargs({"foo": "bar"}) == []+ "request_kwargs",+ [+ {"metadata": "not-a-dict"},+ {"litellm_metadata": "not-a-dict"},+ {"litellm_metadata": ["not", "a", "dict"]},+ {"litellm_params": "not-a-dict"},+ {"litellm_params": {"metadata": "not-a-dict"}},+ {"metadata": {"tags": "free"}},+ {"metadata": {"tags": {"free": "paid"}}},+ ],+)+def test_get_tags_from_request_kwargs_reads_no_tags_from_a_non_dict_shape(request_kwargs):+ """Metadata and `tags` are request-controlled, so a client can send either as a+ string, a list or null. Every shape that cannot hold string tags reads as untagged+ instead of raising, because callers run on the hot request path."""+ from litellm.router_strategy.tag_based_routing import _get_tags_from_request_kwargs++ assert _get_tags_from_request_kwargs(request_kwargs) == []+++def test_get_tags_from_request_kwargs_keeps_only_string_tags():+ from litellm.router_strategy.tag_based_routing import _get_tags_from_request_kwargs++ assert _get_tags_from_request_kwargs({"metadata": {"tags": ["free", 7, None, "paid"]}}) == ["free", "paid"]++# --- _split_tags unit tests ---