From 01dfc3561acb1baf60209786fc24e19d77384b08 Mon Sep 17 00:00:00 2001 From: Cesar Garcia <128240629+Chesars@users.noreply.github.com> Date: Tue, 2 Dec 2025 00:58:27 -0300 Subject: [PATCH] Fix AttributeError when metadata is null in request body (#17263) (#17306) Handle the case where metadata is explicitly set to null/None in the request body. This was causing a 401 error with "'NoneType' object has no attribute 'get'" when calling /v1/batches with metadata: null. The fix uses `or {}` instead of a default dict value since the key exists but has a None value. --- .../proxy/common_utils/http_parsing_utils.py | 2 +- .../common_utils/test_http_parsing_utils.py | 23 +++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) 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)