Invalid base64 string returned from REST API

I am using this code

import base64
from io import BytesIO
from PIL import Image

def get_completion(inputs, parameters=None,ENDPOINT_URL=API_URL):
headers = {
“Authorization”: f"Bearer {API_TOKEN}",
“Content-Type”: “application/json”
data = { “inputs”: inputs }
if parameters is not None:
data.update({“parameters”: parameters})
response = requests.request(“POST”,
ENDPOINT_URL, headers=headers,
return response.content

Once I get the response, I am trying to convert it into PIL Image object, but I find the base64 string returned is invalid. (Checked by putting the returned base64 string on online conversion site)
My code -
result = get_completion(prompt, parameters=None,ENDPOINT_URL= API_URL)
#b64 = result.split(“,”)[1]

im_bytes = base64.b64decode(result)
im_file = BytesIO(im_bytes)
img =

Can someone tell me what is the mistake I am doing. Thanks in advance.

Just co clarify : I am getting this error - UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte
if i use return json.loads(response.content.decode(“utf-8”)) - without the decode, its giving invalid base64 string.
Also I tried using the same exact API URL and Key as in the examples, but that gives “Timeout error” , its going to some AWS hosted URL or something.
I could however, do the NLP examples using same API Token and giving URL to the HuggingFace models , that worked fine.
There is another alternative, to run it locally, using DiffusionPipeline, which I tried (it downloads a super huge model, 3 GB) but it also gives same issue, invalid base64 string. Please help.

I got the same error message, as you @Anirban_K, when I use the API endpoint that you specify above.

I solved it by modifying the course code to,

def get_completion(inputs, parameters=None, ENDPOINT_URL=os.environ['HF_API_TTI_BASE']):
    headers = {
      "Authorization": f"Bearer {hf_api_key}",
      "Content-Type": "application/json"
    data = { "inputs": inputs }
    if parameters is not None:
        data.update({"parameters": parameters})
    response = requests.request("POST",
    contents = response.content
    return contents # (no decoding)

prompt = "a dog in a park"
result = get_completion(prompt)

# Assuming 'result' contains the binary data of the image in PNG format
image =

# Convert the image binary data to base64 encoding
image_data = io.BytesIO(), format="PNG")
image_base64 = base64.b64encode(image_data.getvalue()).decode("utf-8")
html_code = f'<img src="data:image/png;base64,{image_base64}" />'
1 Like

This is very helpful. I am also able to make it work now. Thanks.