Unable to upload files to Gemini 2.0 : File not exists in Gemini API

Hello.

I’m trying to use REST file upload API.
First, I’m uploading the file: (it’s in PHP).

        $requestFile = $this->gaiHttpClient->request('POST', '/upload/v1beta/files', [
            'body' => [
                'file' => $input->getFile()
            ]
        ]);

It’s works well - i’m getting a “https://generativelanguage.googleapis.com/v1beta/files/xxxmyfilexxx” URI.
When I’m trying to get the file using API, it’s telling that the file is “ACTIVE”.

But, when I’m trying to do a prompt with this URI using REST "/v1beta/models/gemini-2.0-flash:generateContent, I got this error :

File https://generativelanguage.googleapis.com/v1beta/files/xxxmyfilexxx not exist in the Gemini API.

Does anyone already encoutered this error?

(using inlineData it’s working well but my files are >10mb size so I need to pass an URI)

Hi @findl

Welcome to the forum.

Are you using the FileData property instead of the InlineData? Also, do you specify the MIME type?
Kindly note that files uploaded to File API have a limited lifetime of 48 hours only, and are deleted automatically.

Here’s the payload I generate with my SDK for .NET (built from scratch using the REST calls). Maybe it gives you an idea.

{
  "model" : "models/gemini-2.0-flash-exp",
  "contents" : [ {
    "role" : "user",
    "parts" : [ {
      "text" : "Describe the image with a creative description"
    }, {
      "fileData" : {
        "fileUri" : "https://generativelanguage.googleapis.com/v1beta/files/temb1dhk7gbe",
        "mimeType" : "image/jpeg"
      }
    } ]
  } ]
}

BTW, the limit is 20MB for inline data, as per documentation.

Cheers.

The file header parameter ‘Content Type’ should be set to a file format like ‘video/mp4’ instead of the example ‘Content Type: application/json’ provided by Gemini

1 Like

‘generationConfig’ => [
‘temperature’ => 1,
‘topK’ => 40,
‘topP’ => 0.95,
‘maxOutputTokens’ => 8192,
‘responseModalities’ => [‘text’, ‘image’],
‘responseMimeType’ => ‘text/plain’
]

Make sure you have “responseModalities” with text and image. Happened to me, php, but solved it.

Hi @quangthien27

How does a parameter responseModalities which applies to the model’s response only have any kind of impact in the parameters used to send a request?

The important part is that the mimeType of the uploaded file has to match its content. If it doesn’t match, then the Gemini API won’t process the file.

Cheers.

You maybe right.

I just had the same issue as Findl had, but I’m pretty sure I had proper mime type sent along the request (true mime extracted from the uploaded file, not used file extension), then adding responseModalities just works (found it on Gemini doc).

It indeed doesn’t make sense as it’s an output param, or maybe it needs the image modality defined explicitly before it does look for the input file. Anw, that just worked for me :melting_face:

Hi @findl , Hope this will help you.

Solution 1: Use sdk api package of gemini (nodejs, python, golang, …)

Solution 2: With php (in here, I work with the Wordpress) and javascript
You can see gemini api docs (Files API  |  Gemini API  |  Google AI for Developers)
To upload file, you need to perform the following steps:

[PHP - Wordpress]

$file = $_FILES['upload_file'];

/**
 * Step 1: Initial resumable request defining metadata:
 */
$res1 = wp_remote_post(
    'https://generativelanguage.googleapis.com/upload/v1beta/files?key=' . $api_key,
    'headers' => [
        'Content-Type' => 'application/json',
        'X-Goog-Upload-Protocol' => 'resumable',
        'X-Goog-Upload-Command' => 'start',
        'X-Goog-Upload-Header-Content-Length' => $file['size'],
        'X-Goog-Upload-Header-Content-Type' => $file['type'],
    ],
    'body' => json_encode([
        'file' => [
            'display_name' => $file['name']
        ]
    ])
);

// res1 will response a headers (only headers).
// We need get upload url from resoponse header 'x-goog-upload-url'
$upload_url = $res1['headers']['x-goog-upload-url'];

/**
 * Step 2: Upload file
 */
$res2 = wp_remote_post(
    $upload_url, // upload_url get from res1 header above.
    'headers' => [
        'Content-Length' => $file['size'],
        'X-Goog-Upload-Command' => 'upload, finalize',
        'X-Goog-Upload-Offset' => 0,
    ],
    'body' => file_get_contents($file['tmp_name']) // Get content / buffer / binary of file upload
);

// After upload, you can check file uploaed at: [GET] https://generativelanguage.googleapis.com/v1beta/files?key=$api_key
// res2: 'name', 'mimeType', 'state', 'uri', ...

/**
 * Step 3: Generate Content
 */
$res3 = wp_remote_post(
    'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=' . $api_key,
    'headers' => [
        'Content-Length' => $file['size'],
        'X-Goog-Upload-Command' => 'upload, finalize',
        'X-Goog-Upload-Offset' => 0,
    ],
    'body' => json_encode([
        'contents' => [
            [
                'parts' => [
                    ['text' => 'YOUR promt text in here. Ex: Caption of this file'],
                    [
                        'file_data' => [
                            'mime_type' => $file['type'],
                            'file_uri' => $res2['file']['uri'], // Get uri from res2 uploaded.
                        ]
                    ]
                ]
            ]
        ]
    ])
);

[jQuery]

$(uploadFileEl).on('change', function (e) {
    var file = e.target.files[0];
    var formData = new FormData();
    formData.append('upload_file', file);

    $.ajax({
        url: 'YOUR_API_ENDPOINT/upload',
        method: 'POST',
        data: formData,
        processData: false,
        contentType: false,
        success: function (response) {
            console.log('Res:', response);
        },
        error: function (xhr) {
            console.error('Error:', xhr);
        },
    });
});