{"record":{"id":"2fd1793ae021d564","repo":"sgl-project/sglang","slug":"failed-to-decode-base64-image-str-exc","errorCode":null,"errorMessage":"Failed to decode base64 image: {str(exc)}","messagePattern":"Failed to decode base64 image: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/utils/image_io.py","lineNumber":36,"sourceCode":"    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:\n        image_data = base64.b64decode(data)\n    except Exception as exc:\n        raise Exception(f\"Failed to decode base64 image: {str(exc)}\") from exc\n\n    with open(target_path, \"wb\") as f:\n        f.write(image_data)\n\n    return target_path\n","sourceCodeStart":18,"sourceCodeEnd":42,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/utils/image_io.py#L18-L42","documentation":"The payload claimed to be base64 but base64.b64decode raised (e.g. non-alphabet characters, wrong padding), so the raw bytes could not be recovered. This catches content-level corruption as opposed to format-level problems caught earlier.","triggerScenarios":"A data URI with ;base64 marker whose data contains invalid characters or incorrect padding — often from JSON escaping issues, URL-safe base64 (- and _) instead of standard alphabet, or copy/paste corruption.","commonSituations":"URL-safe base64 from other systems, payloads with whitespace/newlines or unicode quotes, truncated strings that still decode-format correctly.","solutions":["Normalize the payload: strip whitespace and replace URL-safe chars before decoding","Re-encode the source image with standard base64 (base64.b64encode)","Verify the string length is a multiple of 4 after cleanup"],"exampleFix":"// before\nsave_base64_image_to_path(\"data:image/png;base64,Ab-/==\", \"/tmp/img\")\n// after\nimport base64\nclean = data.replace(\"-\", \"+\").replace(\"_\", \"/\")\nsave_base64_image_to_path(f\"data:image/png;base64,{clean}\", \"/tmp/img\")","handlingStrategy":"try-catch","validationCode":"import base64\ndef is_decodable(b64: str) -> bool:\n    try:\n        base64.b64decode(b64, validate=True)\n        return True\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    save_base64_image_to_path(uri, path)\nexcept Exception as e:\n    if \"Failed to decode base64\" in str(e):\n        clean = \"\".join(b64.split()).replace(\"-\", \"+\").replace(\"_\", \"/\")\n        save_base64_image_to_path(f\"data:image/png;base64,{clean}\", path)","preventionTips":["Use standard (not URL-safe) base64","Strip whitespace/newlines from payloads","Add padding to multiples of 4"],"tags":["base64","decode-error","image-input"],"backgroundTag":"invalid-base64-encoding","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}