TheAlgorithms/Python · error · TypeError
argument should be a bytes-like object or ASCII string, not
Error message
argument should be a bytes-like object or ASCII string, not '{encoded_data.__class__.__name__}' What it means
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.
Source
Thrown at ciphers/base64_cipher.py:94
>>> c = "QQ=="
>>> base64_decode(a) == b64decode(a)
True
>>> base64_decode(b) == b64decode(b)
True
>>> base64_decode(c) == b64decode(c)
True
>>> base64_decode("abc")
Traceback (most recent call last):
...
AssertionError: Incorrect padding
"""
# Make sure encoded_data is either a string or a bytes-like object
if not isinstance(encoded_data, bytes) and not isinstance(encoded_data, str):
msg = (
"argument should be a bytes-like object or ASCII string, "
f"not '{encoded_data.__class__.__name__}'"
)
raise TypeError(msg)
# In case encoded_data is a bytes-like object, make sure it contains only
# ASCII characters so we convert it to a string object
if isinstance(encoded_data, bytes):
try:
encoded_data = encoded_data.decode("utf-8")
except UnicodeDecodeError:
raise ValueError("base64 encoded data should only contain ASCII characters")
padding = encoded_data.count("=")
# Check if the encoded string contains non base64 characters
if padding:
assert all(char in B64_CHARSET for char in encoded_data[:-padding]), (
"Invalid base64 character(s) found."
)
else:
assert all(char in B64_CHARSET for char in encoded_data), (View on GitHub (pinned to f5988cc097)
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'')
Example fix
# before
base64_decode(payload.get("data")) # payload.get returns None when key missing
# after
base64_decode(payload.get("data", "")) Defensive patterns
Strategy: validation
Validate before calling
if not isinstance(encoded_data, (str, bytes)):
encoded_data = str(encoded_data).encode("ascii") Type guard
def is_base64_input(value) -> bool:
return isinstance(value, (str, bytes)) Try / catch
try:
decoded = base64_decode(encoded_data)
except TypeError:
decoded = base64_decode(str(encoded_data)) Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- a bytes-like object is required, not '{data.__class__.__name
- base64 encoded data should only contain ASCII characters
- Plugboard setting isn't type string ({type(pbstring)})
- number must be positive
- operation can not be conducted on an object of type {type(nu
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/0bd319ae99717e81.
Report an issue: GitHub.