{"record":{"id":"09d6788da8ccbae1","repo":"unslothai/unsloth","slug":"invalid-base64-payload","errorCode":null,"errorMessage":"invalid base64 payload","messagePattern":"invalid base64 payload","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"studio/backend/routes/data_recipe/seed.py","lineNumber":198,"sourceCode":"            columns_seen[str(key)] = None\n    return list(columns_seen.keys())\n\n\ndef _sanitize_filename(filename: str) -> str:\n    name = Path(filename).name.strip().replace(\"\\x00\", \"\")\n    if not name:\n        return \"seed_upload\"\n    return name\n\n\ndef _decode_base64_payload(content_base64: str) -> bytes:\n    raw = content_base64.strip()\n    if \",\" in raw and raw.lower().startswith(\"data:\"):\n        raw = raw.split(\",\", 1)[1]\n    try:\n        return base64.b64decode(raw, validate = True)\n    except binascii.Error as exc:\n        raise HTTPException(status_code = 400, detail = \"invalid base64 payload\") from exc\n\n\ndef _read_preview_rows_from_local_file(path: Path, preview_size: int) -> list[dict[str, Any]]:\n    try:\n        import pandas as pd\n    except ImportError as exc:\n        raise log_and_http_error(\n            exc,\n            500,\n            \"seed inspect dependencies unavailable\",\n            event = \"data_recipe.seed.dependencies_unavailable\",\n            log = logger,\n        ) from exc\n\n    ext = path.suffix.lower()\n    try:\n        if ext == \".csv\":\n            df = pd.read_csv(path, nrows = preview_size, encoding = \"utf-8-sig\")","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/routes/data_recipe/seed.py#L180-L216","documentation":"HTTP 400 raised by _decode_base64_payload when base64.b64decode(raw, validate=True) raises binascii.Error — the payload contains characters outside the base64 alphabet or has wrong padding. The function first strips an optional data-URL prefix (data:...;base64,) before decoding.","triggerScenarios":"POST /seed upload with content_base64 that is not strictly base64: whitespace inside the string, URL-safe base64 (- and _) instead of standard (+ and /), missing padding, or a data-URL prefix not separated by a comma.","commonSituations":"Client uses btoa/atob incorrectly, sends base64url (common from crypto APIs), truncates the string, or double-encodes. Python clients passing raw bytes instead of str also fail.","solutions":["Send standard (not URL-safe) base64 with correct padding = signs.","If you have a data-URL, include the comma: 'data:text/csv;base64,XXXX'.","In Python, use base64.b64encode(data).decode('ascii'); in JS, btoa(binaryString) on the whole file.","Log the payload length and first/last chars client-side to spot truncation or embedded newlines."],"exampleFix":"# before (Python client)\nrequests.post(url, json={'content_base64': str(b64encode(blob))})  # sends \"b'xxxx'\"\n\n# after\nrequests.post(url, json={'content_base64': b64encode(blob).decode('ascii')})","handlingStrategy":"validation","validationCode":"const B64 = /^[A-Za-z0-9+/]+={0,2}$/;\nfunction toStandardBase64(blob) {\n  const buf = new Uint8Array(blob);\n  let bin = ''; buf.forEach(b => bin += String.fromCharCode(b));\n  return btoa(bin); // standard base64, padded\n}\nconst payload = toStandardBase64(file);\nif (!B64.test(payload)) throw new Error('encoding bug');","typeGuard":"function isStandardBase64(s: string): boolean {\n  return /^[A-Za-z0-9+/]+={0,2}$/.test(s) && s.length % 4 === 0;\n}","tryCatchPattern":"Catch the 400 response; if detail === 'invalid base64 payload', re-encode the file with a tested encoder and retry once. Do not retry with the same body.","preventionTips":["Encode with a single, tested helper; never hand-roll base64.","If you hold base64url, convert: replace - with +, _ with /, then pad to a multiple of 4.","Include the data-URL comma if you send a prefixed string."],"tags":["base64","upload","validation","http-400"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}