BerriAI/litellm · error · Exception

Image url not in expected format. Example Expected input -…

Error message

Image url not in expected format. Example Expected input - "image_url": "data:image/jpeg;base64,{base64_image}". Supported formats - ['image/jpeg', 'image/png', 'image/gif', 'image/webp']. Error: {e}

What it means

While parsing an image into a base64 chunk (media_type + data) for Anthropic-style params, something failed and the generic except wraps it. The supported set is explicit in the message: image/jpeg, image/png, image/gif, image/webp. ImageFetchError is re-raised untouched, so this wrapper covers parse/format problems, not fetch failures.

Solutions

  1. Convert the image to a supported format (PNG/JPEG/GIF/WEBP) before sending
  2. Use a data URI with one of the supported mimes: data:image/png;base64,...
  3. Pass a hosted image URL instead of inline base64 where the provider supports it

Example fix

# before: HEIC not supported
{"type": "image_url", "image_url": {"url": "data:image/heic;base64,..."}}

# after: convert to PNG first
from PIL import Image
import base64, io
buf = io.BytesIO()
Image.open("photo.heic").convert("RGB").save(buf, format="PNG")
b64 = base64.b64encode(buf.getvalue()).decode()
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
Defensive patterns

Strategy: validation

Validate before calling

import re

SUPPORTED_IMAGE_MIMES = {"image/jpeg", "image/png", "image/gif", "image/webp"}
DATA_URI_RE = re.compile(r"^data:([a-zA-Z0-9./+-]+);base64,(.+)$")

def data_uri_is_supported(u: str) -> bool:
    m = DATA_URI_RE.match(u)
    return bool(m) and m.group(1).lower() in SUPPORTED_IMAGE_MIMES

# before the call, convert unsupported images:
# if not data_uri_is_supported(url): re-encode with PIL to PNG

Try / catch

try:
    litellm.completion(model=model, messages=messages)
except litellm.ImageFetchError:
    raise
except Exception as e:
    if "Image url not in expected format" in str(e) and "Supported formats" in str(e):
        messages = reencode_images_to_png(messages)
    else:
        raise

Prevention

When it happens

Trigger: A data URI whose mime is outside the supported set (image/heic, image/avif, image/svg+xml); malformed or truncated base64 payloads; escaped slashes (\/) the parser could not normalize; unexpected string shapes hitting the parser.

Common situations: iPhone HEIC photos; next-gen formats like AVIF/JPEG-XL; hand-assembled data URIs with typos in the mime or base64.

Related errors


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

Appendix: source

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

        if openai_image_url.startswith("http"):
            openai_image_url = convert_url_to_base64(url=openai_image_url)
        # Extract the media type and base64 data
        media_type, base64_data = openai_image_url.split("data:")[1].split(";base64,")

        if format:
            media_type = format
        else:
            media_type = media_type.replace("\\/", "/")

        return GenericImageParsingChunk(
            type="base64",
            media_type=media_type,
            data=base64_data,
        )
    except litellm.ImageFetchError:
        raise
    except Exception as e:
        raise Exception(
            f"""Image url not in expected format. Example Expected input - "image_url": "data:image/jpeg;base64,{{base64_image}}". Supported formats - ['image/jpeg', 'image/png', 'image/gif', 'image/webp']. Error: {e}"""
        )


def create_anthropic_image_param(
    image_url_input: str | dict,
    format: str | None = None,
    is_bedrock_invoke: bool = False,
) -> AnthropicMessagesImageParam:
    """
    Create an AnthropicMessagesImageParam from an image URL input.

    Supports both URL references (for HTTP/HTTPS URLs) and base64 encoding.
    """
    # Extract URL and format from input
    if isinstance(image_url_input, str):
        image_url = image_url_input
    else:

View on GitHub (pinned to 77b7c6c40c)