Fix mimetype_from_path returning empty string when puremagic cannot detect type#1358
Open
mahsumaktas wants to merge 1 commit intosimonw:mainfrom
Open
Fix mimetype_from_path returning empty string when puremagic cannot detect type#1358mahsumaktas wants to merge 1 commit intosimonw:mainfrom
mahsumaktas wants to merge 1 commit intosimonw:mainfrom
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1340.
Problem
puremagic.from_file()can return an empty string''(instead of raisingPureError) 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:Fix
Added a
mimetypes.guess_type()fallback inmimetype_from_path()for both cases: empty string return andPureError. 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 returnsNoneinstead of''when puremagic comes up empty (no path available there for a fallback).Changes
llm/utils.py(5 lines changed):import mimetypesmimetype_from_path: returnsmimetypes.guess_type()result when puremagic returns''or raisesPureErrormimetype_from_string: returnsNonewhen puremagic returns''tests/test_utils.py(65 lines added):Tests