reflex-dev/reflex · warning

Unknown mime type for {image} {image_format}. Defaulting to

Error message

Unknown mime type for {image} {image_format}. Defaulting to image/png

What it means

serialize_image builds a data URI (data:<mime>;base64,...) from a PIL/static image. It first tries image.get_format_mimetype() (static images); on AttributeError it falls back to looking up the format string in the MIME mapping. If the image's format key isn't in the MIME dict, it warns and defaults to image/png, relying on the browser to sniff the real type.

Source

Thrown at packages/reflex-base/src/reflex_base/utils/serializers.py:507

        Returns:
            The serialized image.
        """
        buff = io.BytesIO()
        image_format = getattr(image, "format", None) or "PNG"
        image.save(buff, format=image_format)
        image_bytes = buff.getvalue()
        base64_image = base64.b64encode(image_bytes).decode("utf-8")
        try:
            # Newer method to get the mime type, but does not always work.
            mime_type = image.get_format_mimetype()  # pyright: ignore [reportAttributeAccessIssue]
        except AttributeError:
            try:
                # Fallback method
                mime_type = MIME[image_format]
            except KeyError:
                # Unknown mime_type: warn and return image/png and hope the browser can sort it out.
                warnings.warn(  # noqa: B028
                    f"Unknown mime type for {image} {image_format}. Defaulting to image/png"
                )
                mime_type = "image/png"

        return f"data:{mime_type};base64,{base64_image}"

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Convert the image to a well-known format before passing it to rx.image: img.convert('RGB').save(buf, format='PNG') and pass the buffered result.
  2. Normalize the format string to a key present in the MIME mapping (e.g. 'jpeg', 'png', 'gif', 'svg', 'webp').
  3. If the format is legitimately common, check reflex_base/utils/serializers.py MIME dict and upgrade Reflex / open an issue to add the mapping.

Example fix

# before
rx.image(src=exotic_tiff_pil_image)  # warns, defaults to image/png

# after
import io
buf = io.BytesIO()
exotic_tiff_pil_image.convert('RGB').save(buf, format='PNG')
rx.image(src=buf.getvalue())
Defensive patterns

Strategy: validation

Validate before calling

from reflex_base.utils.serializers import MIME

fmt = (getattr(img, 'format', None) or image_format or '').lower()
if fmt not in MIME:
    import io
    buf = io.BytesIO()
    img.convert('RGB').save(buf, format='PNG')
    src = buf.getvalue()
else:
    src = img  # safe to pass directly

Type guard

def has_known_mime(img) -> bool:
    fmt = getattr(img, 'format', None)
    try:
        return fmt is not None and fmt.lower() in MIME
    except Exception:
        return False

Prevention

When it happens

Trigger: Passing a PIL Image whose .format is an uncommon value not present in reflex_base.utils.serializers.MIME (e.g. some TIFF/PPM/ICO variants or an exotic plugin format), or a static asset whose format string isn't a MIME map key, to a component expecting an image src (rx.image(...)) so it gets serialized.

Common situations: Loading images via Pillow plugins that report formats Reflex doesn't map; converting images in memory (format lost/odd); passing a raw format string that doesn't match ('jpg' vs 'jpeg' style mismatches from custom preprocessing).

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/8c071acad2cc1d78. Report an issue: GitHub.