{"record":{"id":"e97045f9c8dab2f3","repo":"sgl-project/sglang","slug":"invalid-image-image-file","errorCode":null,"errorMessage":"Invalid image: {image_file}","messagePattern":"Invalid image: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/utils/common.py","lineNumber":1896,"sourceCode":"    elif isinstance(image_file, str) and image_file.startswith((\"http://\", \"https://\")):\n        image = _load_image(image_file=image_file, gpu_image_decode=gpu_image_decode)\n    elif isinstance(image_file, str) and image_file.startswith(\"file://\"):\n        image = _load_image(\n            image_file=unquote(urlparse(image_file).path),\n            gpu_image_decode=gpu_image_decode,\n        )\n    elif isinstance(image_file, str) and image_file.lower().endswith(\n        image_extension_names\n    ):\n        image = _load_image(image_file=image_file, gpu_image_decode=gpu_image_decode)\n    elif isinstance(image_file, str) and image_file.startswith(\"data:\"):\n        image = _load_image(image_file=image_file, gpu_image_decode=gpu_image_decode)\n    elif isinstance(\n        image_file, str\n    ):  # Other formats, try to decode as base64 by default\n        image = _load_image(image_file=image_file, gpu_image_decode=gpu_image_decode)\n    else:\n        raise ValueError(f\"Invalid image: {image_file}\")\n    return image, image_size\n\n\ndef get_image_bytes(image_file: Union[str, bytes]) -> bytes:\n    \"\"\"Normalize various image inputs into raw bytes.\"\"\"\n    if isinstance(image_file, bytes):\n        return image_file\n    if image_file.startswith((\"http://\", \"https://\")):\n        timeout = int(os.getenv(\"REQUEST_TIMEOUT\", \"3\"))\n        return download_remote_media(image_file, timeout=timeout)\n    if image_file.startswith((\"file://\", \"/\")):\n        with open(image_file, \"rb\") as f:\n            return f.read()\n    if isinstance(image_file, str) and image_file.startswith(\"data:\"):\n        _, encoded = image_file.split(\",\", 1)\n        return pybase64.b64decode(encoded, validate=True)\n    if isinstance(image_file, str):\n        return pybase64.b64decode(image_file, validate=True)","sourceCodeStart":1878,"sourceCodeEnd":1914,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/utils/common.py#L1878-L1914","documentation":"The image loader's final else branch: image_file is neither bytes, a URL string, a base64 string, a local path, nor a data URI — it has an unsupported type (None, int, Image already handled earlier, list, dict). The message echoes the value for debugging bad multimodal payloads.","triggerScenarios":"Calling the top-level image loader (in fetch/decode pipeline) with None or a non-str/bytes object — e.g. request JSON {\"image\": null} or a list where a single image string was expected.","commonSituations":"Missing image key defaulting to None; frontend sending arrays; template bugs forwarding the wrong field; protobuf/JSON typing surprises.","solutions":["Validate the image field is str/bytes at the API layer before decoding","Return a 400 for null/missing image inputs instead of letting the worker raise","If multiple images are supported, iterate and pass one at a time"],"exampleFix":"# before\nimage, size = load_image(req.get('image'))  # None -> ValueError\n# after\nimg = req.get('image')\nif not isinstance(img, (str, bytes)): raise HTTPException(400, 'image must be a string or bytes')\nimage, size = load_image(img)","handlingStrategy":"type-guard","validationCode":"if not isinstance(image_file, (str, bytes)):\n    return HTTPException(400, 'image must be a string (url/path/base64) or bytes')\nimage, size = load_image(image_file)","typeGuard":"def is_image_input(v) -> bool:\n    return isinstance(v, (str, bytes)) and len(v) > 0","tryCatchPattern":"try:\n    image, size = load_image(image_file)\nexcept ValueError as e:\n    if 'Invalid image' in str(e):\n        return HTTPException(400, str(e))\n    raise","preventionTips":["Schema-validate multimodal fields (type + presence) at request parse","Unwrap single-element lists before calling image loaders"],"tags":["image","multimodal","validation","input-validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}