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 AttributeError when metadata is null in request body (#17263) (#17306)

Cesar Garcia01dfc3561acb
diff --git a/litellm/proxy/common_utils/http_parsing_utils.py b/litellm/proxy/common_utils/http_parsing_utils.py
index 59b3ec20b4a..259755f5ef9 100644
--- a/litellm/proxy/common_utils/http_parsing_utils.py
+++ b/litellm/proxy/common_utils/http_parsing_utils.py
@@ -309,7 +309,7 @@ def get_tags_from_request_body(request_body: dict) -> List[str]:
List of tag names (strings), empty list if no valid tags found
"""
metadata_variable_name = get_metadata_variable_name_from_kwargs(request_body)
- metadata = request_body.get(metadata_variable_name, {})
+ metadata = request_body.get(metadata_variable_name) or {}
tags_in_metadata: Any = metadata.get("tags", [])
tags_in_request_body: Any = request_body.get("tags", [])
combined_tags: List[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 85858866dda..2361decc5af 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
@@ -606,8 +606,27 @@ def test_get_tags_from_request_body_with_dict_tags():
}
}
}
-
+
result = get_tags_from_request_body(request_body=request_body)
-
+
+ assert result == []
+ assert isinstance(result, list)
+
+
+def test_get_tags_from_request_body_with_null_metadata():
+ """
+ Test that function handles null metadata gracefully without crashing.
+
+ This is a regression test for https://github.com/BerriAI/litellm/issues/17263
+ When metadata is explicitly set to null/None, the function should return
+ an empty list instead of raising AttributeError.
+ """
+ request_body = {
+ "model": "gpt-4",
+ "metadata": None # OpenAI API accepts metadata: null
+ }
+
+ result = get_tags_from_request_body(request_body=request_body)
+
assert result == []
assert isinstance(result, list)