Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebP files not recognized by mimetypes.guess_type() in Python <3.13 #425

Closed
nyosegawa opened this issue Jul 3, 2024 · 2 comments · Fixed by #440
Closed

WebP files not recognized by mimetypes.guess_type() in Python <3.13 #425

nyosegawa opened this issue Jul 3, 2024 · 2 comments · Fixed by #440
Assignees
Labels
component:python sdk Issue/PR related to Python SDK type:feature request New feature request/enhancement

Comments

@nyosegawa
Copy link

Description of the bug:

When attempting to use a WebP image with the generate_content() function of the Gemini API, an "Unsupported MIME type" error is encountered. This occurs despite WebP being a widely supported image format.

Environment:

  • Python version: 3.10
  • Gemini API version: v0.7.1

Code to reproduce:

import requests
import google.generativeai as genai

response = requests.get("https://www.gstatic.com/webp/gallery/3.sm.webp")
with open("test.webp", 'wb') as file:
    file.write(response.content)

GOOGLE_API_KEY="****"
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-1.5-pro-latest')
sample_webp = genai.upload_file(path='test.webp')

response = model.generate_content(
    ["Describe the image with a creative description.", sample_webp],
)
print(response.text)

Error Message:

BadRequest: 400 POST https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?%24alt=json%3Benum-encoding%3Dint: Unsupported MIME type: application/octet-stream

Actual vs expected behavior:

Actual behavior:
The API returns a 400 BadRequest error with the message "Unsupported MIME type: application/octet-stream" when attempting to process a WebP image.

Expected behavior:
The API should recognize and process the WebP image format, allowing for text generation as it does with other supported image formats like JPEG or PNG.

Any other information you'd like to share?

Problem Description
The Gemini API is throwing an "Unsupported MIME type" error when attempting to process WebP images. This issue likely stems from the upload_file function:

if mime_type is None:
mime_type, _ = mimetypes.guess_type(path)

Current Workaround
Users can explicitly specify the MIME type when calling upload_file:

response = model.generate_content(
    ["Describe the image with a creative description.", 
     genai.types.FileData(
         mime_type="image/webp",
         data=sample_webp
     )]
)

Broader Context

This issue is likely to affect other users who don't specify the MIME type explicitly.
Python 3.13 will include a fix for this in the core mimetypes module (see python/cpython#111741). Users on Python 3.13+ won't encounter this error.
For users on Python versions lower than 3.13, a potential solution would be to add the WebP MIME type manually:

mimetypes.add_type('image/webp', '.webp')

Suggested Improvements

Update the library to include the manual addition of the WebP MIME type:
mimetypes.add_type('image/webp', '.webp')
This would ensure compatibility across all Python versions.
Consider updating the upload_file function to handle WebP files explicitly, even if mimetypes.guess_type fails.

@singhniraj08
Copy link

@nyosegawa, Thank you reporting this issue. Currently PNG, JPEG, WEBP, HEIC and HEIF image formats are supported by Python SDK. Supported image formats. We can keep this issue as a feature request to introduce .webp image file support.

@singhniraj08 singhniraj08 added type:feature request New feature request/enhancement status:triaged Issue/PR triaged to the corresponding sub-team component:python sdk Issue/PR related to Python SDK labels Jul 4, 2024
@nyosegawa
Copy link
Author

@singhniraj08 Got it. Thanks!
Looking forward to the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:python sdk Issue/PR related to Python SDK type:feature request New feature request/enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants