Skip to content

Commit e4a87f2

Browse files
Aman071106jlowin
andauthored
fix : canonical mime type mapping from formats to remove inconsistency #4627 (#4628)
* fix : canonical mime type mapping from formats to remove inconsistency * Apply ruff format to _get_mime_type --------- Co-authored-by: Jeremiah Lowin <153965+jlowin@users.noreply.github.com>
1 parent 4ebb3fd commit e4a87f2

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

fastmcp_slim/fastmcp/utilities/types.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,21 @@ def __init__(
335335

336336
def _get_mime_type(self) -> str:
337337
"""Get MIME type from format or guess from file extension."""
338+
mapping = {
339+
"wav": "audio/wav",
340+
"mp3": "audio/mpeg",
341+
"ogg": "audio/ogg",
342+
"m4a": "audio/mp4",
343+
"flac": "audio/flac",
344+
}
345+
338346
if self._format:
339-
return f"audio/{self._format.lower()}"
347+
return mapping.get(self._format.lower(), f"audio/{self._format.lower()}")
340348

341349
if self.path:
342-
suffix = self.path.suffix.lower()
343-
return {
344-
".wav": "audio/wav",
345-
".mp3": "audio/mpeg",
346-
".ogg": "audio/ogg",
347-
".m4a": "audio/mp4",
348-
".flac": "audio/flac",
349-
}.get(suffix, "application/octet-stream")
350+
return mapping.get(
351+
self.path.suffix.lower().lstrip("."), "application/octet-stream"
352+
)
350353
return "audio/wav" # default for raw binary data
351354

352355
def to_audio_content(

tests/utilities/test_types.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,25 @@ def test_audio_initialization_with_data(self):
282282
assert audio.data == b"test"
283283
assert audio._mime_type == "audio/wav" # Default for raw data
284284

285+
def test_mime_type_from_format(self):
286+
"""Test MIME type normalization from audio format."""
287+
288+
expected = {
289+
"wav": "audio/wav",
290+
"mp3": "audio/mpeg",
291+
"ogg": "audio/ogg",
292+
"m4a": "audio/mp4",
293+
"flac": "audio/flac",
294+
}
295+
296+
for fmt, mime in expected.items():
297+
audio = Audio(data=b"test", format=fmt)
298+
assert audio._mime_type == mime
299+
285300
def test_audio_initialization_with_format(self):
286301
"""Test audio initialization with a specific format."""
287302
audio = Audio(data=b"test", format="mp3")
288-
assert audio._mime_type == "audio/mp3"
303+
assert audio._mime_type == "audio/mpeg"
289304

290305
def test_missing_data_and_path_raises_error(self):
291306
"""Test that error is raised when neither path nor data is provided."""
@@ -335,7 +350,7 @@ def test_to_audio_content(self, tmp_path, monkeypatch):
335350
content = audio.to_audio_content()
336351

337352
assert content.type == "audio"
338-
assert content.mime_type == "audio/mp3"
353+
assert content.mime_type == "audio/mpeg"
339354
assert content.data == base64.b64encode(test_data).decode()
340355

341356
def test_to_audio_content_error(self, monkeypatch):

0 commit comments

Comments
 (0)