I found the explanation to my issue here:
TLDR;
when encoding bytes you receive a bytes object, not a string object.
it was resolved by changing the above encode_media
function as follows:
def encode_media(media):
"""base64 encode media data"""
encoded_data = base64.b64encode(media.get_bytes())
return encoded_data.decode('utf-8')
This returns the string representation of the encoded bytes.