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.
This commit is contained in:
Cesar Garcia 2025-12-02 00:58:27 -03:00 • committed by GitHub
parent 965406c643
commit 01dfc3561a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 3 deletions

View file

@ -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] = []

View file

@ -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)