{"record":{"id":"a032222eab9d3e3c","repo":"BerriAI/litellm","slug":"url-does-not-point-to-a-valid-image-content-type","errorCode":null,"errorMessage":"URL does not point to a valid image (content-type: {content_type})","messagePattern":"URL does not point to a valid image \\(content-type: (.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/litellm_core_utils/prompt_templates/factory.py","lineNumber":3190,"sourceCode":"\n\ndef _load_image_from_url(image_url):\n    try:\n        from PIL import Image\n    except Exception:\n        raise Exception(\"image conversion failed please run `pip install Pillow`\")\n    from io import BytesIO\n\n    try:\n        # Send a GET request to the image URL\n        client: Final = HTTPHandler(concurrent_limit=1)\n        response: Final[httpx.Response] = safe_get(client, image_url)\n        response.raise_for_status()  # Raise an exception for HTTP errors\n\n        # Check the response's content type to ensure it is an image\n        content_type: Final = response.headers.get(\"content-type\")\n        if not content_type or \"image\" not in content_type:\n            raise ValueError(f\"URL does not point to a valid image (content-type: {content_type})\")\n\n        # Load the image from the response content\n        return Image.open(BytesIO(response.content))\n\n    except Exception as e:\n        raise e\n\n\ndef _gemini_vision_convert_messages(messages: list):\n    \"\"\"\n    Converts given messages for GPT-4 Vision to Gemini format.\n\n    Args:\n        messages (list): The messages to convert. Each message can be a dictionary with a \"content\" key. The content can be a string or a list of elements. If it is a string, it will be concatenated to the prompt. If it is a list, each element will be processed based on its type:\n            - If the element is a dictionary with a \"type\" key equal to \"text\", its \"text\" value will be concatenated to the prompt.\n            - If the element is a dictionary with a \"type\" key equal to \"image_url\", its \"image_url\" value will be added to the list of images.\n\n    Returns:","sourceCodeStart":3172,"sourceCodeEnd":3208,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/litellm_core_utils/prompt_templates/factory.py#L3172-L3208","documentation":"Inside _load_image_from_url: the URL was fetched successfully but the response's content-type header is missing or does not contain 'image', so litellm refuses to hand the bytes to PIL. This guards against parsing HTML error pages or XML auth errors as images.","triggerScenarios":"An image URL whose server responds 200 with text/html (SPA/redirect page), application/xml (S3 error), or no content-type at all; signed URLs past expiry; endpoints that proxy images through HTML wrappers.","commonSituations":"Expired presigned S3/CDN links; auth walls in front of image hosts; misconfigured static servers omitting content-type headers.","solutions":["Verify with curl -I <url> that the response content-type is image/*","Fix URL signing/auth so the image bytes are returned directly","Re-host the image somewhere serving correct image/* headers","Download the image yourself and pass base64 data instead of the URL"],"exampleFix":"# before: URL serves text/html\nimage = _load_image_from_url(\"https://example.com/pic?token=expired\")\n\n# after: fetch bytes yourself and pass base64 / data URI\nimport base64, httpx\ndata = httpx.get(\"https://example.com/pic\", headers={\"Authorization\": \"Bearer ...\"}).content\nb64 = base64.b64encode(data).decode()","handlingStrategy":"validation","validationCode":"import httpx\n\ndef url_serves_image(url: str, timeout: float = 10.0) -> bool:\n    try:\n        r = httpx.get(url, follow_redirects=True, timeout=timeout, headers={\"Range\": \"bytes=0-0\"})\n        ct = r.headers.get(\"content-type\", \"\")\n        return \"image\" in ct\n    except httpx.HTTPError:\n        return False\n\n# guard each image URL before the LLM call; re-encode or reject failures","typeGuard":null,"tryCatchPattern":"try:\n    litellm.completion(model=model, messages=messages)\nexcept ValueError as e:\n    if \"URL does not point to a valid image\" in str(e):\n        # fetch bytes yourself with proper auth and inline as base64\n        messages = inline_image_bytes(messages)\n    else:\n        raise","preventionTips":["curl -I image URLs in integration tests to confirm image/* content-types","Keep signed URLs fresh; refresh before they expire","Pass base64 data for images behind auth walls instead of relying on litellm's fetch"],"tags":["image","url","content-type","validation"],"backgroundTag":"invalid-image-url","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}