I am using Postman and trying to get a response from Gemini API when trying to make a prompt including an image.
I am sending the requesto to: https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent
{
"contents":[
{
"parts":[
{"text": "What is this picture?"},
{
"inline_data": {
"mime_type":"image/jpeg",
"data": "https://i.ibb.co/3mX1qcB/Document-sans-titre-page-0001.jpg"
}
}
]
}
]
}
I am getting this response :
{
"error": {
"code": 400,
"message": "Invalid value at 'contents[0].parts[1].inline_data.data' (TYPE_BYTES), Base64 decoding failed for "https://pastebin.com/raw/kL4WEnnn"",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "contents[0].parts[1].inline_data.data",
"description": "Invalid value at 'contents[0].parts[1].inline_data.data' (TYPE_BYTES), Base64 decoding failed for "https://pastebin.com/raw/kL4WEnnn""
}
]
}
]
}
}
I have tried to convert the image to a base64 raw text, upload it to pastebin and give it in the request but I have the same error
can someone help me?
2
Answers
You’re trying to use an image located at a URL, but the
inline_data
Part
you’re trying to use is designed for you to pass a base64-encoded representation of the raw bytes of an image file.Instead, use the
FileData
Part
, which accepts "URI based data":How about the following patterns?
Pattern 1:
In this pattern,
inlineData
is used.In this case, it is required to convert the image data (
https://i.ibb.co/3mX1qcB/Document-sans-titre-page-0001.jpg
) to the base64 data. WhenFirst, I created text data including the base64 data from the URL as follows. The filename is
sampleRequestBody.txt
.When this is used with a curl command, it becomes as follows.
When this curl command is run, the result shown in the "Testing" section is obtained.
Pattern 2:
In this pattern,
fileData
is used. In this case, the following flow is run.1. Upload the image data to Gemini
By this, the following result is returned.
Please copy the value of
uri
from the returned value.2. Generate content
Using the value of
uri
, content is generated as follows. Here,fileData
property is used.Testing:
Both patterns return the following result. The generated text will be different.
References: