{"record":{"id":"2e8f079624dc7a8e","repo":"BerriAI/litellm","slug":"unsupported-image-type-for-gemini-image-edit","errorCode":null,"errorMessage":"Unsupported image type for Gemini image edit.","messagePattern":"Unsupported image type for Gemini image edit\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/gemini/image_edit/transformation.py","lineNumber":196,"sourceCode":"\n        return inline_parts\n\n    def _read_all_bytes(self, image: FileTypes) -> bytes:\n        if isinstance(image, bytes):\n            return image\n        if isinstance(image, BytesIO):\n            current_pos = image.tell()\n            image.seek(0)\n            data = image.read()\n            image.seek(current_pos)\n            return data\n        if isinstance(image, BufferedReader):\n            current_pos = image.tell()\n            image.seek(0)\n            data = image.read()\n            image.seek(current_pos)\n            return data\n        raise ValueError(\"Unsupported image type for Gemini image edit.\")\n","sourceCodeStart":178,"sourceCodeEnd":197,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/gemini/image_edit/transformation.py#L178-L197","documentation":"Type validation inside GeminiImageEditConfig._read_all_bytes. Only three input shapes are accepted: raw bytes, io.BytesIO, and io.BufferedReader (an open file opened in binary mode). Any other value in the FileTypes union — a file-path string, a (filename, bytes, content_type) tuple, a PIL Image, a TextIOWrapper (file opened in text mode), or another IO wrapper — reaches the final raise.","triggerScenarios":"litellm.image_edit(..., image='/path/to/img.png') with a path string; image=('cat.png', data, 'image/png') tuple form; image=open('img.png') without 'rb'; passing a PIL.Image object; passing the raw httpx.Response from a download.","commonSituations":"Reusing OpenAI client code where a path string works; files opened in text mode; frameworks handing over Werkzeug/FileStorage or UploadFile objects; forgetting .read() on an already-open file handle of an unsupported wrapper type.","solutions":["Read the file first: image=open(path, 'rb').read() and pass bytes","Or pass an open binary file: image=open(path, 'rb') (BufferedReader)","Convert foreign objects: BytesIO(pil_img.tobytes()) needs proper encoding — prefer pil_img.save(buf, format='PNG') then pass buf","Ensure files are opened in binary mode ('rb'), never text mode"],"exampleFix":"# before\nresp = litellm.image_edit(model='gemini/gemini-2.5-flash-image', prompt=p, image='assets/cat.png', api_key=KEY)\n# ValueError: Unsupported image type for Gemini image edit.\n\n# after\nresp = litellm.image_edit(\n    model='gemini/gemini-2.5-flash-image',\n    prompt=p,\n    image=open('assets/cat.png', 'rb').read(),  # bytes\n    api_key=KEY,\n)","handlingStrategy":"type-guard","validationCode":"from io import BytesIO, BufferedReader\n\ndef is_supported_gemini_image(img) -> bool:\n    return isinstance(img, (bytes, BytesIO, BufferedReader))\n\nif not all(is_supported_gemini_image(i) for i in images):\n    images = [i.read() if hasattr(i, 'read') else open(i, 'rb').read() for i in images]","typeGuard":"from io import BytesIO, BufferedReader\nfrom typing import Any\n\ndef is_gemini_editable_image(img: Any) -> bool:\n    \"\"\"True when GeminiImageEditConfig._read_all_bytes accepts the value.\"\"\"\n    return isinstance(img, (bytes, BytesIO, BufferedReader))","tryCatchPattern":null,"preventionTips":["Normalize images to bytes at the edge: image=open(path, 'rb').read()","Never pass path strings, (name, bytes, mime) tuples, PIL Images, or TextIOWrapper files","Open files in binary mode ('rb') only","Reuse the is_gemini_editable_image guard in request validation"],"tags":["gemini","image-edit","type-validation","file-io"],"backgroundTag":"unsupported-argument-type","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}