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 None
When 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
mimetypes module
- 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.
MIME type detection fails when puremagic returns empty string
Problem
When attaching files to LLM requests, the
mimetype_from_path()function inllm/utils.pycan fail to detect MIME types for certain files, causing attachment validation to fail with an error like:Root Cause
The
mimetype_from_path()function relies onpuremagic.from_file()to detect MIME types. However, whenpuremagiccannot detect a file type, it can return an empty string''instead of raising aPureErrorexception. The current code only handles the exception case:When
puremagicreturns 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()whenpuremagicreturns an empty string or raises an exception. This ensures that common file types (like.mp4,.jpg, etc.) are correctly detected even whenpuremagicfails.Proposed Fix
The fix adds:
mimetypesmodulepuremagicmimetypes.guess_type()in both the empty string case and the exception handlerThis 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.,
.mp4files) and now correctly returnsvideo/mp4using the fallback mechanism.