Skip to content

Commit f0a2df1

Browse files
committed
fix: content-type and accept parsing
More flexibility than an exact string match since there can be some additional params Signed-off-by: Raphael Glon <[email protected]>
1 parent e85fb98 commit f0a2df1

File tree

1 file changed

+17
-14
lines changed
  • src/huggingface_inference_toolkit/serialization

1 file changed

+17
-14
lines changed

src/huggingface_inference_toolkit/serialization/base.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
content_type_mapping = {
66
"application/json": Jsoner,
7-
"application/json; charset=utf-8": Jsoner,
87
"text/csv": None,
98
"text/plain": None,
109
# image types
@@ -39,9 +38,11 @@
3938

4039
class ContentType:
4140
@staticmethod
42-
def get_deserializer(content_type):
43-
if content_type in content_type_mapping:
44-
return content_type_mapping[content_type]
41+
def get_deserializer(content_type: str):
42+
# Extract media type from content type
43+
extracted = content_type.split(";")[0]
44+
if extracted in content_type_mapping:
45+
return content_type_mapping[extracted]
4546
else:
4647
message = f"""
4748
Content type "{content_type}" not supported.
@@ -51,13 +52,15 @@ def get_deserializer(content_type):
5152
raise Exception(message)
5253

5354
@staticmethod
54-
def get_serializer(accept):
55-
if accept in content_type_mapping:
56-
return content_type_mapping[accept]
57-
else:
58-
message = f"""
59-
Accept type "{accept}" not supported.
60-
Supported accept types are:
61-
{", ".join(list(content_type_mapping.keys()))}
62-
"""
63-
raise Exception(message)
55+
def get_serializer(accept: str):
56+
extracts = accept.split(",")
57+
for extract in extracts:
58+
extracted = extract.split(";")[0]
59+
if extracted in content_type_mapping:
60+
return content_type_mapping[extracted]
61+
message = f"""
62+
Accept type "{accept}" not supported.
63+
Supported accept types are:
64+
{", ".join(list(content_type_mapping.keys()))}
65+
"""
66+
raise Exception(message)

0 commit comments

Comments
 (0)