Skip to content

fix: detect actual MIME type and extension for base64 generated images (#29948) - #29950

Closed
Anurag-M1 wants to merge 1 commit into
open-webui:devfrom
Anurag-M1:fix/generated-image-mime-sniffing
Closed

Anurag-M1 wants to merge 1 commit into
open-webui:devfrom
Anurag-M1:fix/generated-image-mime-sniffing

Conversation

@Anurag-M1

Copy link
Copy Markdown

Pull Request

Checklist

  • This PR targets the dev branch.
  • This PR links to a well-described, confirmed Issue or active Discussion: Closes #29948.
  • A maintainer explicitly asked me to open this PR, or this PR only updates i18n/localization.
  • The change is one logical unit with no unrelated commits.
  • I matched nearby code patterns and avoided unnecessary new settings, abstractions, or dependencies.
  • I manually tested the changed workflow and any nearby behavior that could be affected.
  • I updated relevant docs, including the Open WebUI Docs Repository, if needed.
  • I added screenshots for UI changes, and a recording when motion or interaction matters.
  • I reviewed any AI-generated code before submitting it.
  • The PR title uses one of the prefixes listed below.

Title Prefix

  • fix: Bug fixes or corrections

Summary

Fixes #29948 where generated images returned as bare base64 (b64_json from OpenAI, Gemini bytesBase64Encoded, and compatible image generation backends) are always saved as image/png and generated-image.png regardless of their actual image format.

Details:

  1. Added sniff_image_mime(data: bytes) -> str to inspect image magic bytes (JPEG FF D8 FF, PNG 89 50 4E 47, WebP RIFF....WEBP, GIF GIF87a/GIF89a, BMP BM, TIFF) with a PIL fallback and safe default to image/png.
  2. Updated get_image_data() to inspect bare base64 decoded bytes using sniff_image_mime() instead of unconditionally hardcoding image/png. Also added fallback sniffing when data URIs provide generic octet-stream headers, and replaced character-stripping lstrip('data:') with removeprefix('data:') to prevent corrupted MIME strings.
  3. Expanded IMAGE_FILE_EXTENSIONS to support .gif and .bmp alongside .jpg, .png, and .webp.
  4. Updated upload_image() to check IMAGE_FILE_EXTENSIONS before mimetypes.guess_extension() so that JPEG files are consistently named .jpg instead of Python's default .jpe.

Testing

  1. Tested sniff_image_mime and get_image_data locally across JPEG, PNG, WebP, GIF, and BMP test images encoded as bare base64 and data URIs.
  2. Verified octet-stream header sniffing and arbitrary binary fallback to image/png.
  3. Verified extension mappings for all supported formats.
  4. Ran ruff format --check and ruff check matching the repository's GitHub Actions CI workflow (.github/workflows/backend.yaml). All checks passed with zero errors.

Changelog Entry

Fixed

Contributor License Agreement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants