I am building a chat app with Gemini 1.5 pro Node JS API, when I try to send a message with an image I get this error:
{
field: 'contents[0].parts[2].inline_data.data',
description: `Invalid value at 'contents[0].parts[2].inline_data.data' (TYPE_BYTES), Base64 decoding failed for "data:image/jpeg;base64,/9j/4QAWRXhpZgAATU0AKgAAAAgAAAAAAAD/7QAwUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAABMcAnQAB1BpY29DVEYcA...The rest of the base64 data
NOTE: The error prints a big chunk of the base64 data in the terminal, so i can’t get all the way up to the beggining of the error that’s why you’re seeing parts[2]
instead of parts[1]
.
Here is the code:
if (msg.attachments.size > 0) {
let imageParts = [];
let index = 1;
msg.attachments.forEach((attachment) => {
var request = require("request").defaults({ encoding: null });
let image;
request.get(attachment.url, async function (error, response, body) {
if (!error && response.statusCode == 200) {
image = "data:" + response.headers["content-type"] + ";base64," + Buffer.from(body).toString("base64");
var mimeType = attachment.contentType;
imageParts.push({
inlineData: {
data: image,
mimeType,
},
});
if (msg.attachments.size === index) {
const generatedContent = await model.generateContent([
msg.content,
...imageParts,
]);
msg.reply(generatedContent.response.text());
} else {
index++;
}
}
});
});
}