From 6c26971cd42b522ceacc3a1029d2c57fc687b1ab Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Fri, 17 Oct 2025 13:20:07 -0700 Subject: [PATCH] [Bug Fix] Tags as metadata dicts were raising exceptions (#15625) * fix get_tags_from_request_body * Revert "fix get_tags_from_request_body" This reverts commit 1c044dad999500b5c11ec96f528212c248f72f61. * fix get_tags_from_request_body * test_get_tags_from_request_body_with_dict_tags --- .../proxy/common_utils/http_parsing_utils.py | 15 ++++++++--- .../common_utils/test_http_parsing_utils.py | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) 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)