{"record":{"id":"0bd319ae99717e81","repo":"TheAlgorithms/Python","slug":"argument-should-be-a-bytes-like-object-or-ascii-st","errorCode":null,"errorMessage":"argument should be a bytes-like object or ASCII string, not '{encoded_data.__class__.__name__}'","messagePattern":"argument should be a bytes-like object or ASCII string, not '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ciphers/base64_cipher.py","lineNumber":94,"sourceCode":"    >>> c = \"QQ==\"\n    >>> base64_decode(a) == b64decode(a)\n    True\n    >>> base64_decode(b) == b64decode(b)\n    True\n    >>> base64_decode(c) == b64decode(c)\n    True\n    >>> base64_decode(\"abc\")\n    Traceback (most recent call last):\n      ...\n    AssertionError: Incorrect padding\n    \"\"\"\n    # Make sure encoded_data is either a string or a bytes-like object\n    if not isinstance(encoded_data, bytes) and not isinstance(encoded_data, str):\n        msg = (\n            \"argument should be a bytes-like object or ASCII string, \"\n            f\"not '{encoded_data.__class__.__name__}'\"\n        )\n        raise TypeError(msg)\n\n    # In case encoded_data is a bytes-like object, make sure it contains only\n    # ASCII characters so we convert it to a string object\n    if isinstance(encoded_data, bytes):\n        try:\n            encoded_data = encoded_data.decode(\"utf-8\")\n        except UnicodeDecodeError:\n            raise ValueError(\"base64 encoded data should only contain ASCII characters\")\n\n    padding = encoded_data.count(\"=\")\n\n    # Check if the encoded string contains non base64 characters\n    if padding:\n        assert all(char in B64_CHARSET for char in encoded_data[:-padding]), (\n            \"Invalid base64 character(s) found.\"\n        )\n    else:\n        assert all(char in B64_CHARSET for char in encoded_data), (","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/base64_cipher.py#L76-L112","documentation":"Raised by base64_decode() in ciphers/base64_cipher.py when encoded_data is neither bytes nor str. The decoder only accepts those two types; anything else (int, list, None) triggers this TypeError before any Base64 work happens.","triggerScenarios":"Calling base64_decode(None) when a variable was never assigned; passing a list of code points or an int; chained calls where a previous step returned a non-str/bytes value.","commonSituations":"Optional values that default to None flowing into the decoder; deserialized JSON data that turned the encoded string into another type; API responses parsed into unexpected shapes.","solutions":["Ensure the value is str or bytes before calling: base64_decode(encoded if isinstance(encoded, (str, bytes)) else str(encoded).encode())","Fix the upstream producer so it really yields str/bytes","Guard optionals: base64_decode(encoded or b'')"],"exampleFix":"# before\nbase64_decode(payload.get(\"data\"))  # payload.get returns None when key missing\n\n# after\nbase64_decode(payload.get(\"data\", \"\"))","handlingStrategy":"validation","validationCode":"if not isinstance(encoded_data, (str, bytes)):\n    encoded_data = str(encoded_data).encode(\"ascii\")","typeGuard":"def is_base64_input(value) -> bool:\n    return isinstance(value, (str, bytes))","tryCatchPattern":"try:\n    decoded = base64_decode(encoded_data)\nexcept TypeError:\n    decoded = base64_decode(str(encoded_data))","preventionTips":["Check isinstance(value, (str, bytes)) before decoding","Guard optional values: value or '' before the call","Log the offending type when the check fails to find upstream producers"],"tags":["base64","type-error","decoding","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}