Gpt-4-vision-preview: 400 Invalid base64 image_url

Im trying to use node.js to accept a base64 encoded image as a parameter and send that to openai api. Im sending a base64 encoded image in postman to the server like this:

{
  "base64Image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAw8AAAEoCAYAAAAe6s1xAAAA..."
}

and here is my code but it returns a 400 Invalid base64 image_url

async function imageToResponse(req, res) {
  const base64Image = req.body.base64Image;

  try {
    const response = await openai.chat.completions.create({
      model: 'gpt-4-vision-preview',
      messages: [
        { role: 'system', content: "you are a model to help identify everything in this image" },
        { role: 'user', content: [
          {
              type: "image_url", 
              image_url: 
              {
                "url": "data:image/png;base64,{base64Image}"
              }
            }
          ]
        },
      ],
    });

    console.log(response.data);
    res.status(200).json(response.data);
  } catch (error) {
    console.error('Error calling the OpenAI API:', error);
    res.status(500).json({ error: error.message });
  }
}

Be sure to search this forum for ā€œbase64ā€, because I remember someone having an issue with that which was easily solvable, but I can’t remember what the issue was.

1 Like

Okay thank you. I’m having trouble finding someone that has the same issue im having. Im wondering if its the way im sending it in the post body from postman.

Maybe it’s expecting ā€œdetailā€ property like here:

EDIT: Also you could search thru GitHub for examples, or even just describe the method to ChatGPT itself and ask it to generate a complete working example code for you. When I’m stuck on something sometimes I just can describe the issue to ChatGPT and it will tell me what I’m doing wrong, or write the full code for me.

1 Like