Skip to content

Fix mimetype_from_path returning empty string when puremagic cannot detect type#1358

Open
mahsumaktas wants to merge 1 commit intosimonw:mainfrom
mahsumaktas:fix/mimetype-empty-string-fallback
Open

Fix mimetype_from_path returning empty string when puremagic cannot detect type#1358
mahsumaktas wants to merge 1 commit intosimonw:mainfrom
mahsumaktas:fix/mimetype-empty-string-fallback

Conversation

@mahsumaktas
Copy link

Fixes #1340.

Problem

puremagic.from_file() can return an empty string '' (instead of raising PureError) when it cannot identify a file type. The previous code only handled the exception case, so the empty string passed through as the MIME type. This produced errors like:

Error: This model does not support attachments of type '', only audio/flac, image/heif, ...

Fix

Added a mimetypes.guess_type() fallback in mimetype_from_path() for both cases: empty string return and PureError. This covers common extensions (e.g. .mp4, .wav, .pdf) when magic number detection fails.

Applied the same empty-string guard to mimetype_from_string() so it returns None instead of '' when puremagic comes up empty (no path available there for a fallback).

Changes

llm/utils.py (5 lines changed):

  • Added import mimetypes
  • mimetype_from_path: returns mimetypes.guess_type() result when puremagic returns '' or raises PureError
  • mimetype_from_string: returns None when puremagic returns ''

tests/test_utils.py (65 lines added):

  • 8 new tests covering both functions across puremagic success, empty string, PureError, unknown extension, and MIME_TYPE_FIXES cases

Tests

tests/test_utils.py::TestMimetypeFromPath::test_puremagic_returns_valid_type PASSED
tests/test_utils.py::TestMimetypeFromPath::test_puremagic_returns_empty_string_falls_back_to_mimetypes PASSED
tests/test_utils.py::TestMimetypeFromPath::test_puremagic_raises_falls_back_to_mimetypes PASSED
tests/test_utils.py::TestMimetypeFromPath::test_unknown_extension_returns_none PASSED
tests/test_utils.py::TestMimetypeFromPath::test_mime_type_fix_applied PASSED
tests/test_utils.py::TestMimetypeFromString::test_puremagic_returns_valid_type PASSED
tests/test_utils.py::TestMimetypeFromString::test_puremagic_returns_empty_string_returns_none PASSED
tests/test_utils.py::TestMimetypeFromString::test_puremagic_raises_returns_none PASSED

93 passed in 0.69s

…etect type

When puremagic.from_file() cannot identify a file type, it sometimes
returns an empty string instead of raising PureError. This caused the
empty string to be passed through as the MIME type, resulting in errors
like 'This model does not support attachments of type "", only ...'.

The fix adds a mimetypes.guess_type() fallback in mimetype_from_path()
for both the empty string case and the PureError case. This covers
common file types by extension when magic number detection fails.

The same guard is applied to mimetype_from_string() to prevent an empty
string from leaking as a MIME type (no path fallback is available there).

Fixes simonw#1340
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MIME type detection fails when puremagic returns empty string

1 participant