Skip to content

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

@nyosegawa

Description

@nyosegawa

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:

https://github.com/google-gemini/generative-ai-python/blob/45fcbdfabe36d5ea198d8bb681130a0e32bad10b/google/generativeai/files.py#L60-L61

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions