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}". What it means
While extracting base64 image data from an image_url value, litellm expected either a plain URL (fetched and converted) or a data URI matching data:image/...;base64,...; the split on those markers failed and the generic except re-raises this formatted error. Note that the underlying fetch failure ('Error: Unable to fetch image from URL') is re-raised as-is and does not surface here.
Solutions
- Use the full data URI form: "image_url": "data:image/jpeg;base64,<b64>"
- Or pass a plain https:// URL and let litellm fetch and convert it
- If it persists, print the string you are passing - it is likely neither a URL nor a well-formed data URI
Example fix
# before: raw base64, no data URI prefix
{"type": "image_url", "image_url": {"url": "iVBORw0KGgoAAAANSU..."}}
# after
{"type": "image_url", "image_url": {"url": "data:image/png;base64,iVBORw0KGgoAAAANSU..."}} Defensive patterns
Strategy: validation
Validate before calling
import re
DATA_URI_RE = re.compile(r"^data:image/[a-zA-Z0-9.+-]+;base64,[A-Za-z0-9+/=]+$")
URL_RE = re.compile(r"^https?://\S+$")
def is_usable_image_url(u: str) -> bool:
return bool(DATA_URI_RE.match(u) or URL_RE.match(u))
def to_data_uri(mime: str, raw: bytes) -> str:
import base64
return f"data:{mime};base64,{base64.b64encode(raw).decode()}"
# wrap every image_url value in to_data_uri() at construction time Try / catch
try:
litellm.completion(model=model, messages=messages)
except Exception as e:
if "Image url not in expected format" in str(e):
# rebuild the offending image_url as a proper data URI and retry
messages = rebuild_image_urls_as_data_uris(messages)
else:
raise Prevention
- Build image_url values with a single helper that always emits data:<mime>;base64,<b64> or an https URL
- Never pass raw base64 without the data URI prefix
- Validate data URIs with a regex at the input boundary
When it happens
Trigger: Passing image_url as raw base64 without the data: prefix, a data URI missing the ';base64,' marker, or a malformed string that is neither a fetchable URL nor a parseable data URI.
Common situations: Hand-building image_url dicts and encoding images yourself without the data URI wrapper; truncated or mangled base64 strings from env vars or configs.
Related errors
- Image url not in expected format. Example Expected input -…
- Unsupported image format
- gemini image conversion failed please run `pip install…
- HF response not in expected format
- image conversion failed please run `pip install Pillow`
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/6d9d6debaea54423.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/prompt_templates/factory.py:183
return prompt
def convert_to_ollama_image(openai_image_url: str):
try:
if openai_image_url.startswith("http"):
openai_image_url = convert_url_to_base64(url=openai_image_url)
if openai_image_url.startswith("data:image/"):
# Extract the base64 image data
base64_data = openai_image_url.split("data:image/")[1].split(";base64,")[1]
else:
base64_data = openai_image_url
return base64_data
except Exception as e:
if "Error: Unable to fetch image from URL" in str(e):
raise e
raise Exception(
"""Image url not in expected format. Example Expected input - "image_url": "data:image/jpeg;base64,{base64_image}". """
)
def _handle_ollama_system_message(messages: list, prompt: str, msg_i: int) -> tuple[str, int]:
system_content_str = ""
## MERGE CONSECUTIVE SYSTEM CONTENT ##
while msg_i < len(messages) and messages[msg_i]["role"] == "system":
msg_content = convert_content_list_to_str(messages[msg_i])
system_content_str += msg_content
msg_i += 1
return system_content_str, msg_i
def ollama_pt(
model: str, messages: listView on GitHub (pinned to 77b7c6c40c)