BerriAI/litellm · error · ValueError
Unsupported image type for Gemini image edit.
Error message
Unsupported image type for Gemini image edit.
What it means
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.
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
Example fix
# before
resp = litellm.image_edit(model='gemini/gemini-2.5-flash-image', prompt=p, image='assets/cat.png', api_key=KEY)
# ValueError: Unsupported image type for Gemini image edit.
# after
resp = litellm.image_edit(
model='gemini/gemini-2.5-flash-image',
prompt=p,
image=open('assets/cat.png', 'rb').read(), # bytes
api_key=KEY,
) Defensive patterns
Strategy: type-guard
Validate before calling
from io import BytesIO, BufferedReader
def is_supported_gemini_image(img) -> bool:
return isinstance(img, (bytes, BytesIO, BufferedReader))
if not all(is_supported_gemini_image(i) for i in images):
images = [i.read() if hasattr(i, 'read') else open(i, 'rb').read() for i in images] Type guard
from io import BytesIO, BufferedReader
from typing import Any
def is_gemini_editable_image(img: Any) -> bool:
"""True when GeminiImageEditConfig._read_all_bytes accepts the value."""
return isinstance(img, (bytes, BytesIO, BufferedReader)) Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- GEMINI_API_KEY is not set
- Gemini image edit requires at least one image.
- image_response must be of type ImageResponse got type=
- Unsupported image type for Vertex AI Gemini image edit.
- Vertex AI Gemini image edit requires at least one image.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/2e8f079624dc7a8e.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/llms/gemini/image_edit/transformation.py:196
return inline_parts
def _read_all_bytes(self, image: FileTypes) -> bytes:
if isinstance(image, bytes):
return image
if isinstance(image, BytesIO):
current_pos = image.tell()
image.seek(0)
data = image.read()
image.seek(current_pos)
return data
if isinstance(image, BufferedReader):
current_pos = image.tell()
image.seek(0)
data = image.read()
image.seek(current_pos)
return data
raise ValueError("Unsupported image type for Gemini image edit.")
View on GitHub (pinned to 77b7c6c40c)