{"record":{"id":"ba4fb0912d9e47ff","repo":"sgl-project/sglang","slug":"failed-to-decode-base64-image-expected-format-d","errorCode":null,"errorMessage":"Failed to decode base64 image. Expected format: `data:[<media-type>];base64,<data>`","messagePattern":"Failed to decode base64 image\\. Expected format: `data:\\[<media-type>\\];base64,<data>`","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/utils/image_io.py","lineNumber":15,"sourceCode":"# SPDX-License-Identifier: Apache-2.0\nimport base64\nimport os\nimport re\n\n\ndef save_base64_image_to_path(base64_data: str, target_path: str) -> str:\n    b64_format_hint = (\n        \"Failed to decode base64 image. \"\n        \"Expected format: `data:[<media-type>];base64,<data>`\"\n    )\n\n    match = re.match(r\"data:(.*?)(;base64)?,(.*)\", base64_data)\n    if not match:\n        raise ValueError(b64_format_hint)\n    media_type = match.group(1)\n    is_base64 = match.group(2)\n    if not is_base64:\n        raise ValueError(f\"{b64_format_hint} (missing ;base64 marker)\")\n    data = match.group(3)\n    if not data:\n        raise ValueError(f\"{b64_format_hint} (empty data payload)\")\n\n    if media_type.startswith(\"image/\"):\n        ext = media_type.split(\"/\")[-1].lower()\n        if ext == \"jpeg\":\n            ext = \"jpg\"\n    else:\n        ext = \"jpg\"\n    target_path = f\"{target_path}.{ext}\"\n    os.makedirs(os.path.dirname(target_path), exist_ok=True)\n\n    try:","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/utils/image_io.py#L1-L33","documentation":"save_base64_image_to_path expects a data-URI of the form data:[<media-type>];base64,<data>, and the input did not match the regex data:(.*?)(;base64)?,(.*) at all. This is the generic malformed-input error for inline base64 images (distinct from the missing-marker and empty-payload variants).","triggerScenarios":"Passing an image string that is not a data URI: a bare base64 blob without the data: prefix, a URL, or arbitrary text — via _maybe_url_image or prepare_warmup_image_path.","commonSituations":"Frontends sending raw base64 without the data: prefix, copying base64 from files that strip the header, passing an https:// URL where inline data was expected.","solutions":["Prefix the payload as data:image/png;base64,<b64data>","If you have a URL, use the URL image path instead of the base64 API","Strip whitespace/newlines before constructing the data URI"],"exampleFix":"// before\nsave_base64_image_to_path(\"iVBORw0KGgo...\", \"/tmp/img\")\n// after\nsave_base64_image_to_path(\"data:image/png;base64,iVBORw0KGgo...\", \"/tmp/img\")","handlingStrategy":"validation","validationCode":"import re\nDATA_URI_RE = re.compile(r\"data:.*?;base64,.+\", re.S)\ndef is_valid_data_uri(s: str) -> bool:\n    return bool(DATA_URI_RE.fullmatch(s.strip()))","typeGuard":"def is_data_uri_image(s: str) -> bool:\n    return isinstance(s, str) and s.startswith(\"data:image/\") and \";base64,\" in s","tryCatchPattern":"try:\n    save_base64_image_to_path(uri, path)\nexcept ValueError as e:\n    if \"base64\" in str(e):\n        # reject or fetch from source instead\n        ...","preventionTips":["Validate data URIs at the API boundary","Keep the data: prefix when relaying client payloads","Log payload prefixes on rejection to diagnose quickly"],"tags":["base64","data-uri","image-input","validation"],"backgroundTag":"malformed-base64-data-uri","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}