BerriAI/litellm · error · Exception

gemini image conversion failed please run `pip install…

Error message

gemini image conversion failed please run `pip install Pillow`

What it means

Gemini vision conversion, base64 branch: litellm tried to import PIL to decode the base64 image data and the import failed, raising this dependency error. It is the Gemini-path twin of the generic Pillow error - the environment lacks the optional Pillow package.

Solutions

  1. pip install Pillow in the runtime environment
  2. Pin Pillow in requirements to keep it present across rebuilds
  3. If you cannot install it, pass image URLs supported without local decoding

Example fix

# before: PIL import fails on the base64 branch
# after
pip install Pillow
Defensive patterns

Strategy: validation

Validate before calling

def pillow_available() -> bool:
    try:
        import PIL  # noqa: F401
        return True
    except ImportError:
        return False

# fail fast at startup when sending base64 images to gemini vision models

Try / catch

try:
    litellm.completion(model="gemini/gemini-...", messages=messages)
except Exception as e:
    if "gemini image conversion failed" in str(e) and "Pillow" in str(e):
        raise RuntimeError("Install Pillow to send base64 images to Gemini") from e
    raise

Prevention

When it happens

Trigger: Sending base64 image data (img contains 'base64') to a Gemini vision model in an environment without Pillow installed; the URL branch uses _load_image_from_url and fails with the sibling error instead.

Common situations: Minimal deployments (slim Docker, serverless) without Pillow; upgrading environments and dropping the dependency; CI differing from local.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/84f71e402af376d8. Report an issue: GitHub.

Appendix: source

Thrown at litellm/litellm_core_utils/prompt_templates/factory.py:3241

                    if isinstance(element, dict):
                        if element["type"] == "text":
                            prompt += element["text"]
                        elif element["type"] == "image_url":
                            image_url = element["image_url"]["url"]
                            images.append(image_url)
        # processing images passed to gemini
        processed_images: Final = []
        for img in images:
            if "https:/" in img:
                # Case 1: Image from URL
                image = _load_image_from_url(img)
                processed_images.append(image)

            else:
                try:
                    from PIL import Image
                except Exception:
                    raise Exception("gemini image conversion failed please run `pip install Pillow`")

                if "base64" in img:
                    # Case 2: Base64 image data
                    import base64
                    import io

                    # Extract the base64 image data
                    base64_data = img.split("base64,")[1]

                    # Decode the base64 image data
                    image_data = base64.b64decode(base64_data)

                    # Load the image from the decoded data
                    image = Image.open(io.BytesIO(image_data))
                else:
                    # Case 3: Image filepath (e.g. temp.jpeg) given
                    image = Image.open(img)
                processed_images.append(image)

View on GitHub (pinned to 77b7c6c40c)