Hi,
I consistently receive a 500 error when I include a file in my API call. I’ve tested every available model and they all return the error. The “fileUri” is correct and exists. I’ve doubled checked it by listing the files stored in my Gemini uploads.
Here’s my cURL request.
curl --location 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent?key={mykey}' \
--header 'Content-Type: application/json' \
--data '{
"contents": [
{
"parts": [
{
"text": "What is this picture?"
},
{
"fileData": {
"mime_type": "image/jpeg",
"fileUri": "https://generativelanguage.googleapis.com/v1beta/files/6ex7hs0zb9kp"
}
}
]
}
]
}'
I need to get this running for a project, but I can’t seem to find any solution online.
Thanks,
Mikhaeel
Hi @Mikhaeel ,
Could you try with the below cURL request.
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"parts": [
{
"text": "What is this picture?"
},
{
"fileData": {
"fileUri": "https://generativelanguage.googleapis.com/v1beta/files/29ar4fcyhnty",
"mimeType": "image/jpeg"
}
}
],
"role": "user"
}
]
}'
Hey @GUNAND_MAYANGLAMBAM,
Thanks for your reply.
Your cURL request returns a 403 Forbidden error. I assume that’s because I don’t have access to your fileUri.
Yes @Mikhaeel , you need to replace it with your fileUri
. I have just provided you with a sample request.
Oh my mistake.
I tested your request with my fileUri, but I still receive the 500 error.
@GUNAND_MAYANGLAMBAM Would you mind sharing a cURL request for uploading a file. Maybe I’m doing something wrong when uploading my file URI.
Try below request for uploading a file:
!wget “https://storage.googleapis.com/generativeai-downloads/images/jetpack.jpg”
curl -X POST "https://generativelanguage.googleapis.com/upload/v1beta/files?key=$GOOGLE_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F "file=@jetpack.jpg;type=image/jpeg"
How would this request look if I was uploading the raw base64 encoded file data?
How are you doing it?
The error roughly sounds like it could be expecting a jpg file, but not actually finding one there.
I’ve done the uploads with something like this:
file=blue-square.png
file64=`base64 ${file}`
body=$(
cat <<EOF > /tmp/file-upload-2.txt
--separator-1719347666612
Content-Type: application/json; charset=UTF-8
{
}
--separator-1719347666612
Content-Type: image/png
Content-Transfer-Encoding: base64
${file64}
--separator-1719347666612--
EOF
)
curl \
-X POST \
-H "Content-Type: multipart/related; boundary=separator-1719347666612" \
-H "X-Goog-Upload-Protocol: multipart" \
--data-binary @/tmp/file-upload-2.txt \
"https://generativelanguage.googleapis.com/upload/v1beta/files?key=${API_KEY}"
Which still uses an intermediary temp file. (Which in this case is a little silly, I’ll admit, since it is reading it in from a file first, but I was doing this to specifically test the base64 case.)
I was doing something wrong in my request. I was putting the base64 data in the body of my request instead of attaching the actual file.
Seems to be working fine now.
Thanks for everyone’s help!
1 Like
Glad you figured this out and you’re back on track!
1 Like
Thanks, I appreciate your time!
The inline data request supports that. It is not as versatile as the file API, but is also useful for specific scenarios.