This seems like the right thing to do but just in case the input gets screwed up you'll avoid a confusing error with this:

    if isinstance(v, unicode):
        # Encode it as a byte-string.
        v = v.encode('utf-8')
    data[k] = base64.b64encode(v)