{"record":{"id":"9dd16c2472cda9e1","repo":"PaddlePaddle/PaddleOCR","slug":"invalid-base64-input-e-ensure-the-string-is-co","errorCode":null,"errorMessage":"Invalid Base64 input: {e}. Ensure the string is complete and correctly padded.","messagePattern":"Invalid Base64 input: (.+?)\\. Ensure the string is complete and correctly padded\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mcp_server/paddleocr_mcp/utils.py","lineNumber":50,"sourceCode":"\ndef is_base64(value: str) -> bool:\n    pattern = r\"^[A-Za-z0-9+/]+={0,2}$\"\n    return bool(re.fullmatch(pattern, value))\n\n\ndef extract_base64_payload(input_data: str) -> str:\n    if input_data.startswith(\"data:\"):\n        if \",\" not in input_data:\n            raise ValueError(\"Invalid data URL: expected a comma after the MIME type.\")\n        return input_data.split(\",\", 1)[1]\n    return input_data\n\n\ndef decode_base64_payload(payload: str) -> bytes:\n    try:\n        return base64.b64decode(payload, validate=True)\n    except Exception as e:\n        raise ValueError(\n            f\"Invalid Base64 input: {e}. \"\n            \"Ensure the string is complete and correctly padded.\"\n        ) from e\n\n\ndef infer_file_type_from_bytes(data: bytes) -> Optional[str]:\n    import puremagic\n\n    mime = puremagic.from_string(data, mime=True)\n    if mime.startswith(\"image/\"):\n        return \"image\"\n    if mime == \"application/pdf\":\n        return \"pdf\"\n    return None\n","sourceCodeStart":32,"sourceCodeEnd":65,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/mcp_server/paddleocr_mcp/utils.py#L32-L65","documentation":"ValueError from decode_base64_payload in paddleocr_mcp/utils.py when base64.b64decode(payload, validate=True) raises. validate=True rejects any non-alphabet character, so whitespace, URL-safe characters (-_,), missing padding, or embedded newlines all fail; the message chains the underlying binascii error and reminds about padding.","triggerScenarios":"Base64 copied with line wraps/newlines; URL-safe base64 (using - and _) passed without conversion; truncated payload missing 1-2 padding '=' chars; payload still percent-encoded or wrapped in a data URL fragment.","commonSituations":"Clients embedding base64 in JSON where whitespace survived; output of base64.urlsafe_b64encode fed directly; LLM tool calls truncating long strings.","solutions":["Strip whitespace/newlines: `payload = \"\".join(payload.split())`.","Fix padding: `payload += \"=\" * (-len(payload) % 4)`.","Convert URL-safe to standard: `payload.replace(\"-\", \"+\").replace(\"_\", \"/\")` before sending.","Re-encode the source bytes to guarantee a clean string."],"exampleFix":"// before\npayload = \"iVBORw0KGgoAAAANSUhEUg...\\nAAA=\"  # contains newline\ndecode_base64_payload(payload)  # ValueError\n\n// after\npayload = \"\".join(payload.split())\npayload += \"=\" * (-len(payload) % 4)\ndecode_base64_payload(payload)","handlingStrategy":"validation","validationCode":"import base64\n\ndef base64_decodable(payload: str) -> bool:\n    cleaned = \"\".join(payload.split())\n    cleaned += \"=\" * (-len(cleaned) % 4)\n    try:\n        base64.b64decode(cleaned, validate=True)\n        return True\n    except Exception:\n        return False","typeGuard":"import re\n\ndef is_standard_base64(value: str) -> bool:\n    \"\"\"True for non-empty standard-alphabet base64 with valid padding.\"\"\"\n    return bool(re.fullmatch(r\"[A-Za-z0-9+/]+={0,2}\", value)) and len(value) % 4 == 0","tryCatchPattern":"try:\n    data = decode_base64_payload(payload)\nexcept ValueError as e:\n    if \"Base64\" in str(e):\n        cleaned = \"\".join(payload.split())\n        cleaned += \"=\" * (-len(cleaned) % 4)\n        data = decode_base64_payload(cleaned)  # single deterministic repair\n    else:\n        raise","preventionTips":["Normalize base64 before sending: strip whitespace, repair padding, convert URL-safe alphabet.","Never wrap base64 across lines when embedding in tool arguments.","Re-encode from source bytes instead of hand-editing base64 strings."],"tags":["python","mcp","base64","validation"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}