{"record":{"id":"8c071acad2cc1d78","repo":"reflex-dev/reflex","slug":"unknown-mime-type-for-image-image-format-defa","errorCode":null,"errorMessage":"Unknown mime type for {image} {image_format}. Defaulting to image/png","messagePattern":"Unknown mime type for (.+?) (.+?)\\. Defaulting to image/png","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/reflex-base/src/reflex_base/utils/serializers.py","lineNumber":507,"sourceCode":"\n        Returns:\n            The serialized image.\n        \"\"\"\n        buff = io.BytesIO()\n        image_format = getattr(image, \"format\", None) or \"PNG\"\n        image.save(buff, format=image_format)\n        image_bytes = buff.getvalue()\n        base64_image = base64.b64encode(image_bytes).decode(\"utf-8\")\n        try:\n            # Newer method to get the mime type, but does not always work.\n            mime_type = image.get_format_mimetype()  # pyright: ignore [reportAttributeAccessIssue]\n        except AttributeError:\n            try:\n                # Fallback method\n                mime_type = MIME[image_format]\n            except KeyError:\n                # Unknown mime_type: warn and return image/png and hope the browser can sort it out.\n                warnings.warn(  # noqa: B028\n                    f\"Unknown mime type for {image} {image_format}. Defaulting to image/png\"\n                )\n                mime_type = \"image/png\"\n\n        return f\"data:{mime_type};base64,{base64_image}\"\n","sourceCodeStart":489,"sourceCodeEnd":513,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/utils/serializers.py#L489-L513","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["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.","Normalize the format string to a key present in the MIME mapping (e.g. 'jpeg', 'png', 'gif', 'svg', 'webp').","If the format is legitimately common, check reflex_base/utils/serializers.py MIME dict and upgrade Reflex / open an issue to add the mapping."],"exampleFix":"# before\nrx.image(src=exotic_tiff_pil_image)  # warns, defaults to image/png\n\n# after\nimport io\nbuf = io.BytesIO()\nexotic_tiff_pil_image.convert('RGB').save(buf, format='PNG')\nrx.image(src=buf.getvalue())","handlingStrategy":"validation","validationCode":"from reflex_base.utils.serializers import MIME\n\nfmt = (getattr(img, 'format', None) or image_format or '').lower()\nif fmt not in MIME:\n    import io\n    buf = io.BytesIO()\n    img.convert('RGB').save(buf, format='PNG')\n    src = buf.getvalue()\nelse:\n    src = img  # safe to pass directly","typeGuard":"def has_known_mime(img) -> bool:\n    fmt = getattr(img, 'format', None)\n    try:\n        return fmt is not None and fmt.lower() in MIME\n    except Exception:\n        return False","tryCatchPattern":null,"preventionTips":["Standardize uploaded/user images to PNG or JPEG server-side before adding them to state.","Keep a normalization step in any pipeline that accepts arbitrary uploads.","If a new format matters to you, extend/patch the MIME mapping and upstream the change."],"tags":["image-serialization","mime-type","pillow","data-uri","warning"],"backgroundTag":"unknown-mime-type-fallback","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}