Skip to content

Commit 01dfc35

Browse files
authored
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.
1 parent 965406c commit 01dfc35

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

litellm/proxy/common_utils/http_parsing_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def get_tags_from_request_body(request_body: dict) -> List[str]:
309309
List of tag names (strings), empty list if no valid tags found
310310
"""
311311
metadata_variable_name = get_metadata_variable_name_from_kwargs(request_body)
312-
metadata = request_body.get(metadata_variable_name, {})
312+
metadata = request_body.get(metadata_variable_name) or {}
313313
tags_in_metadata: Any = metadata.get("tags", [])
314314
tags_in_request_body: Any = request_body.get("tags", [])
315315
combined_tags: List[str] = []

tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,27 @@ def test_get_tags_from_request_body_with_dict_tags():
606606
}
607607
}
608608
}
609-
609+
610610
result = get_tags_from_request_body(request_body=request_body)
611-
611+
612+
assert result == []
613+
assert isinstance(result, list)
614+
615+
616+
def test_get_tags_from_request_body_with_null_metadata():
617+
"""
618+
Test that function handles null metadata gracefully without crashing.
619+
620+
This is a regression test for https://github.com/BerriAI/litellm/issues/17263
621+
When metadata is explicitly set to null/None, the function should return
622+
an empty list instead of raising AttributeError.
623+
"""
624+
request_body = {
625+
"model": "gpt-4",
626+
"metadata": None # OpenAI API accepts metadata: null
627+
}
628+
629+
result = get_tags_from_request_body(request_body=request_body)
630+
612631
assert result == []
613632
assert isinstance(result, list)

0 commit comments

Comments
 (0)