"Request contains an invalid argument" when use uploaded PDF

Hi team,
I’m trying to use and upload uri from a PDF (previosly uploaded by API to https://generativelanguage.googleapis.com/v1beta/files/) and always receive an 400 Error ( “code”: 400, “message”: “Request contains an invalid argument.”, “status”: “INVALID_ARGUMENT” ) when add the fileData to the request.

The request structure seems correct:
-if I send empty the fileUri field I receive a correct error (Invalid or unsupported file uri).
-If I send with the mimeType empyt also a correct error (Unable to submit request because it has a mimeType parameter with value application/json)
-If I inform them properly → Request contains an invalid argument

I’m using gemini-2.0-flash-exp

Any idea?

Thanks!

Hi @Dani_R , Welcome to the forum.

You probably used the wrong endpoint. Try using https://generativelanguage.googleapis.com/upload/v1beta/files.

Just prepare a Colab gist, you can also go through this.

Hope it helps…

Hi @GUNAND_MAYANGLAMBAM
Thanks for your help,
The endpoint seems correct and I receive the url for the uploaded file.
The issue it’s with the prompt request passing this fileUri.

I will try again

Thanks!

Hello @Dani_R,

I am experiencing the same issue. Were you able to resolve it? Could you offer assistance?

I await your response.

Thank you.

1 Like

Sorry @Diego_Retamal , I was not able to receive a correct response and I made the test finally in JS with the library @google/generative-ai and his methods

Hello @Dani_R, I resolved the issue. My mistake was in the initial file upload; I needed to first initiate the file with the appropriate Content-Length and Content-Type headers, followed by the upload of the file’s content.

Regards,

Hi,

I’m having the same problem here. Did you find a solution?

Thanks

Same issue here with Gemini Flash 2 and a jpeg file. I just copy the curl command (“Get code”) that is output from Google AI Studio. The file seems to be uploaded correctly but then it fails after. It works fine in Google Studio but I always get this error with the curl command (I tried also in Postman and Java):
{
“error”: {
“code”: 400,
“message”: “Request contains an invalid argument.”,
“status”: “INVALID_ARGUMENT”
}
}

Same error here testing Google AI Studio code (not mine)

API_KEY=“YOUR_API_KEY”

TODO: Make the following files available on the local file system.

FILES=(“image_math_prompt.png”)
MIME_TYPES=(“image/png”)
for i in “${!FILES[@]}”; do
NUM_BYTES=$(wc -c < “${FILES[$i]}”)
curl “https://generativelanguage.googleapis.com/upload/v1beta/files?key=${API_KEY}
-H “X-Goog-Upload-Command: start, upload, finalize”
-H “X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}”
-H “X-Goog-Upload-Header-Content-Type: ${MIME_TYPES[$i]}”
-H “Content-Type: application/json”
-d “{‘file’: {‘display_name’: ‘${FILES[$i]}’}}”
–data-binary “@${FILES[$i]}”

TODO: Read the file.uri from the response, store it as FILE_URI_${i}

done

curl
-X POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${API_KEY}
-H ‘Content-Type: application/json’
-d @<(echo ‘{
“contents”: [
{
“role”: “user”,
“parts”: [
{
“fileData”: {
“fileUri”: “${FILE_URI_0}”,
“mimeType”: “image/png”
}
}
]
},
{
“role”: “user”,
“parts”: [
{
“text”: “Solve for x. Explain each step.”
}
]
}
],
“generationConfig”: {
“temperature”: 1,
“topK”: 40,
“topP”: 0.95,
“maxOutputTokens”: 8192,
“responseMimeType”: “text/plain”
}
}’)