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]

[Bug Fix] Tags as metadata dicts were raising exceptions (#15625)

Ishaan Jaff6c26971cd42b
diff --git a/litellm/proxy/common_utils/http_parsing_utils.py b/litellm/proxy/common_utils/http_parsing_utils.py
index 807b895bd03..6b3b06e4af6 100644
--- a/litellm/proxy/common_utils/http_parsing_utils.py
+++ b/litellm/proxy/common_utils/http_parsing_utils.py
@@ -263,8 +263,17 @@ def get_tags_from_request_body(request_body: dict) -> List[str]:
"""
metadata_variable_name = get_metadata_variable_name_from_kwargs(request_body)
metadata = request_body.get(metadata_variable_name, {})
- tags_in_metadata: List[str] = metadata.get("tags", [])
- tags_in_request_body: List[str] = request_body.get("tags", [])
- combined_tags: List[str] = tags_in_metadata + tags_in_request_body
+ tags_in_metadata: Any = metadata.get("tags", [])
+ tags_in_request_body: Any = request_body.get("tags", [])
+ combined_tags: List[str] = []
+
+ ######################################
+ # Only combine tags if they are lists
+ ######################################
+ if isinstance(tags_in_metadata, list):
+ combined_tags.extend(tags_in_metadata)
+ if isinstance(tags_in_request_body, list):
+ combined_tags.extend(tags_in_request_body)
+ ######################################
return [tag for tag in combined_tags if isinstance(tag, str)]
diff --git a/tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py b/tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py
index cffed021134..a8df4273765 100644
--- a/tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py
+++ b/tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py
@@ -382,3 +382,30 @@ def test_get_tags_from_request_body_no_tags():
result = get_tags_from_request_body(request_body=request_body)
assert result == []
+
+
+def test_get_tags_from_request_body_with_dict_tags():
+ """
+ Test that function handles dict tags gracefully without crashing.
+ When tags is a dict instead of a list, it should be ignored and return empty list.
+ """
+ request_body = {
+ "model": "aws/anthropic/bedrock-claude-3-5-sonnet-v1",
+ "messages": [
+ {
+ "role": "user",
+ "content": "aloha"
+ }
+ ],
+ "metadata": {
+ "tags": {
+ "litellm_id": "litellm_ratelimit_test",
+ "llm_id": "llmid_ratelimit_test"
+ }
+ }
+ }
+
+ result = get_tags_from_request_body(request_body=request_body)
+
+ assert result == []
+ assert isinstance(result, list)