{"record":{"id":"73b1ee90d82aa6fc","repo":"HKUDS/Vibe-Trading","slug":"expected-data-mime-base64-payload","errorCode":null,"errorMessage":"expected data:<mime>;base64,<payload>","messagePattern":"expected data:<mime>;base64,<payload>","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/utils/media_decode.py","lineNumber":47,"sourceCode":"def save_base64_data_url(data_url: str, output_dir: Path, *, max_bytes: int = 0) -> Path:\n    \"\"\"Decode a base64 data URL and save it to *output_dir*.\n\n    Args:\n        data_url: A ``data:<mime>;base64,...`` URL.\n        output_dir: Directory where the decoded file should be written.\n        max_bytes: Optional maximum decoded byte length. ``0`` disables the\n            limit.\n\n    Returns:\n        The path of the decoded file.\n\n    Raises:\n        FileSizeExceeded: If decoded data exceeds ``max_bytes``.\n        ValueError: If the URL is malformed or uses an unsupported MIME type.\n    \"\"\"\n    match = _DATA_URL_RE.match(data_url)\n    if not match:\n        raise ValueError(\"expected data:<mime>;base64,<payload>\")\n    mime = match.group(1).strip().lower()\n    ext = _EXT_BY_MIME.get(mime)\n    if not ext:\n        raise ValueError(f\"unsupported data URL MIME type: {mime}\")\n\n    payload = match.group(3).strip()\n    try:\n        decoded = base64.b64decode(payload, validate=True)\n    except (binascii.Error, ValueError) as exc:\n        raise ValueError(\"invalid base64 payload\") from exc\n\n    if max_bytes and len(decoded) > max_bytes:\n        raise FileSizeExceeded(f\"decoded media exceeds {max_bytes} bytes\")\n\n    output_dir.mkdir(parents=True, exist_ok=True)\n    path = output_dir / f\"{uuid.uuid4().hex}{ext}\"\n    path.write_bytes(decoded)\n    return path","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/utils/media_decode.py#L29-L65","documentation":"save_base64_data_url persists a data: URL payload to disk. It first validates the URL shape against _DATA_URL_RE, which expects the form data:<mime>;base64,<payload>. If the regex does not match — missing 'data:' scheme, missing ';base64' marker, or malformed structure — it raises this ValueError.","triggerScenarios":"Calling save_base64_data_url with strings like 'data:image/png,' (no payload marker), 'image/png;base64,AAA...', 'data:image/png,AAAA' (missing ;base64), or arbitrary non-data-URL text passed in a media field.","commonSituations":"Frontends sending raw base64 without the data: prefix, URLs constructed by string concatenation with a typo, envelope media fields populated with http URLs instead of data URLs, or trailing/leading whitespace breaking the match.","solutions":["Ensure the input is a full data URL: data:<mime>;base64,<payload>","If you hold raw base64, construct the data URL explicitly with the correct MIME type","Strip surrounding whitespace before passing the value","Log the first ~40 chars of the offending value to spot structural typos quickly"],"exampleFix":"# before\npath = save_base64_data_url(raw_b64, out_dir)  # raw base64, no data: prefix\n\n# after\npath = save_base64_data_url(f\"data:image/png;base64,{raw_b64}\", out_dir)","handlingStrategy":"validation","validationCode":"import re\n_DATA_URL_RE = re.compile(r\"^data:([\\w./+-]+);base64,(.+)$\", re.DOTALL)\n\ndef is_valid_data_url(value: str) -> bool:\n    return bool(_DATA_URL_RE.match(value.strip()))\n\nassert is_valid_data_url(data_url), \"expected data:<mime>;base64,<payload>\"","typeGuard":"def is_data_url(value: str) -> bool:\n    return isinstance(value, str) and value.strip().lower().startswith(\"data:\") and \";base64,\" in value","tryCatchPattern":"try:\n    path = save_base64_data_url(data_url, out_dir)\nexcept ValueError as e:\n    if \"expected data:\" in str(e):\n        return reject_attachment(\"malformed data URL\")\n    raise","preventionTips":["Build data URLs with a single helper instead of string concatenation","Reject http(s) URLs early if the field is data-URL-only","Trim whitespace on envelope media fields before processing"],"tags":["data-url","base64","media","validation"],"backgroundTag":"malformed-data-url","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}