From 91f140716fd607bc9fd3bf69570ae580578cb9b5 Mon Sep 17 00:00:00 2001 From: Abhinavexist Date: Sat, 18 Jul 2026 03:56:19 +0530 Subject: [PATCH] fix: forward computed MIME as file() format (#3) file()/video() computed the MIME only for the blacklist check and set format only when the caller passed one. For an https-URL file/video part with no explicit format, the server had no data-URL MIME to read and fell back to application/octet-stream, undercutting file/video support. Forward the computed MIME as format (the server uses file.format verbatim as mediaType). video() and auto_part inherit this via file(). Unknown extensions still yield no format, preserving the octet-stream fallback. --- src/interfaze/_inputs.py | 41 +++++++++++++++++++++++++-------- tests/test_inputs_and_client.py | 14 +++++++++++ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/interfaze/_inputs.py b/src/interfaze/_inputs.py index c43069a..714c4e1 100644 --- a/src/interfaze/_inputs.py +++ b/src/interfaze/_inputs.py @@ -10,14 +10,35 @@ BytesLike = Union[bytes, bytearray] _EXT_MIME = { - "png": "image/png", "jpg": "image/jpeg", "jpeg": "image/jpeg", "webp": "image/webp", - "gif": "image/gif", "bmp": "image/bmp", "heic": "image/heic", "heif": "image/heif", - "pdf": "application/pdf", "csv": "text/csv", "tsv": "text/tab-separated-values", - "xml": "application/xml", "json": "application/json", "txt": "text/plain", - "md": "text/markdown", "markdown": "text/markdown", "yaml": "application/yaml", "yml": "application/yaml", - "wav": "audio/wav", "mp3": "audio/mpeg", "m4a": "audio/mp4", "ogg": "audio/ogg", "flac": "audio/flac", - "mp4": "video/mp4", "mov": "video/quicktime", "webm": "video/webm", "avi": "video/x-msvideo", - "mkv": "video/x-matroska", "3gp": "video/3gpp", + "png": "image/png", + "jpg": "image/jpeg", + "jpeg": "image/jpeg", + "webp": "image/webp", + "gif": "image/gif", + "bmp": "image/bmp", + "heic": "image/heic", + "heif": "image/heif", + "pdf": "application/pdf", + "csv": "text/csv", + "tsv": "text/tab-separated-values", + "xml": "application/xml", + "json": "application/json", + "txt": "text/plain", + "md": "text/markdown", + "markdown": "text/markdown", + "yaml": "application/yaml", + "yml": "application/yaml", + "wav": "audio/wav", + "mp3": "audio/mpeg", + "m4a": "audio/mp4", + "ogg": "audio/ogg", + "flac": "audio/flac", + "mp4": "video/mp4", + "mov": "video/quicktime", + "webm": "video/webm", + "avi": "video/x-msvideo", + "mkv": "video/x-matroska", + "3gp": "video/3gpp", } @@ -63,8 +84,8 @@ def file(src: str, *, filename: Optional[str] = None, format: Optional[str] = No f: Dict[str, Any] = {"file_data": src} if filename: f["filename"] = filename - if format: - f["format"] = format + if mime: + f["format"] = mime return {"type": "file", "file": f} diff --git a/tests/test_inputs_and_client.py b/tests/test_inputs_and_client.py index 34eb8d7..b65891d 100644 --- a/tests/test_inputs_and_client.py +++ b/tests/test_inputs_and_client.py @@ -22,6 +22,19 @@ def test_file_part(): ) +def test_file_forwards_computed_mime(): + assert inputs.file("https://x.com/d.pdf")["file"]["format"] == "application/pdf" + + +def test_video_forwards_mp4_mime(): + part = inputs.video("https://x.com/clip.mp4") + assert part["type"] == "file" and part["file"]["format"] == "video/mp4" + + +def test_file_unknown_ext_omits_format(): + assert "format" not in inputs.file("https://x.com/page")["file"] + + def test_audio_uses_input_audio(): part = inputs.audio("https://x.com/a.wav") assert part["type"] == "input_audio" and part["input_audio"]["format"] == "wav" @@ -51,6 +64,7 @@ def test_auto_part_routing(): assert inputs.auto_part("https://x.com/a.wav")["type"] == "input_audio" assert inputs.auto_part("https://x.com/a.pdf")["type"] == "file" assert inputs.auto_part("https://x.com/a.mp4")["type"] == "file" + assert inputs.auto_part("https://x.com/a.mp4")["file"]["format"] == "video/mp4" # ---- client surface ----