-
-
Notifications
You must be signed in to change notification settings - Fork 757
Description
MIME type detection fails when puremagic returns empty string
Problem
When attaching files to LLM requests, the mimetype_from_path() function in llm/utils.py can fail to detect MIME types for certain files, causing attachment validation to fail with an error like:
Error: This model does not support attachments of type '', only audio/flac, image/heif, ...
Root Cause
The mimetype_from_path() function relies on puremagic.from_file() to detect MIME types. However, when puremagic cannot detect a file type, it can return an empty string '' instead of raising a PureError exception. The current code only handles the exception case:
def mimetype_from_path(path) -> Optional[str]:
try:
type_ = puremagic.from_file(path, mime=True)
return MIME_TYPE_FIXES.get(type_, type_)
except puremagic.PureError:
return NoneWhen puremagic returns an empty string, it gets passed through as the MIME type, which then fails validation in _validate_attachments().
Solution
Add a fallback to Python's standard mimetypes.guess_type() when puremagic returns an empty string or raises an exception. This ensures that common file types (like .mp4, .jpg, etc.) are correctly detected even when puremagic fails.
Proposed Fix
The fix adds:
- Import of the
mimetypesmodule - Check for empty string return value from
puremagic - Fallback to
mimetypes.guess_type()in both the empty string case and the exception handler
This maintains backward compatibility while providing a more robust MIME type detection mechanism.
Testing
The fix has been tested with files that previously failed MIME type detection (e.g., .mp4 files) and now correctly returns video/mp4 using the fallback mechanism.