google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.62k stars 322 forks source link

upload_file returns 0 byte file with no mime_type (high severity) #628

Closed mikewadhera closed 1 week ago

mikewadhera commented 1 week ago

Description of the bug:

genai.upload_file(path=path, mime_type=mime_type)

Any code that was once working downstream of this, including generate_content now fails

Looks like this was from a production push? Started today 11/11/24 around noon PDT.

Also #627 likely related

Actual vs expected behavior:

Expected: a file with proper size_bytes and mime_type

Actual: size_bytes always zero and mime_type always ''

Any other information you'd like to share?

Looks like this was from a production push? Started today 11/11/24 around noon PDT.

Also #627 likely related

sahilshah commented 1 week ago

We are seeing this as well (also Hi Mike!)

nharada1 commented 1 week ago

Same issue here, even manually setting the mime_type doesn't do anything

sahilshah commented 1 week ago

Even the example notebooks on Gemini docs are broken e.g. https://ai.google.dev/gemini-api/docs/vision?lang=python

teyou commented 1 week ago

same here.

javivallejo commented 1 week ago

Following up for visibility. I reached out on Twitter and got a response, so they should be aware. https://x.com/OfficialLoganK/status/1856130496058495019

adboio commented 1 week ago

+1 seeing this as well.

my errors started with a generic 400:

400 Request contains an invalid argument.

after digging around a bit and changing some params, i'm now seeing this for every content gen call that includes a file:

400 Unable to submit request because it has an empty mimeType parameter in fileData. Add a value to the parameter and try again. Learn more: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini

i'm uploading with an explicit mime-type:

model = genai.GenerativeModel("gemini-1.5-flash")
media_file = genai.upload_file(media_filepath, mime_type=mime_type)

sample file response

genai.File({
'name': 'files/emcdwwnbjjpz',
'display_name': 'video.mp4',
'mime_type': '',
'sha256_hash': '',
'size_bytes': '0',
'state': 'ACTIVE',
'uri': 'https://generativelanguage.googleapis.com/v1beta/files/emcdwwnbjjpz',
'create_time': '2024-11-12T01:12:51.359555Z',
'expiration_time': '2024-11-14T01:12:51.344144581Z',
'update_time': '2024-11-12T01:12:51.359555Z'})
adboio commented 1 week ago

is this only happening with the python SDK? can we call the API directly as a temp fix here?

sidpremkumar commented 1 week ago

is this only happening with the python SDK? can we call the API directly as a temp fix here?

failing with the node sdk as well @adboio

adboio commented 1 week ago

related post on google cloud forums: https://www.googlecloudcommunity.com/gc/AI-ML/Gemini-API-method-models-generateContent-returns-error-code-400/m-p/831749

and issue in another repo https://github.com/GoogleCloudPlatform/generative-ai/issues/1390

m-a-sch commented 1 week ago

So happy to not just be the only one. Who changed the server side of file_upload?

adboio commented 1 week ago

found a potential workaround! :rocket:

instead of calling upload_file, use the Blob type and pass file contents directly to the generate_content:

from google.generativeai import protos

model = genai.GenerativeModel("gemini-1.5-pro")

with open(media_filepath, 'rb') as fp:
    file_contents = fp.read()
    media_blob = protos.Blob(data=file_contents, mime_type=mime_type)

    response = model.generate_content([media_blob, prompt_text],
        generation_config=genai.GenerationConfig(
            response_mime_type="application/json", response_schema=Recipe
        )
    )

edit: welp, this only works up to a limit...

400 Request payload size exceeds the limit: 20971520 bytes. The file size is too large. Please use the File API to upload your files instead. Example: `f = genai.upload_file(path); m.generate_content(['tell me about this file:', f])`
hosopy commented 1 week ago

I'm having the same issue

adboio commented 1 week ago

my use-case is for video files, and the workaround i posted above does not work if the files are too large... i tried splitting the file into multiple parts and uploading the blobs individually (like generate_content([ blob1, blob2, blob3, "prompt" ])), but it seems the payload limit is on the entire payload, not individual parts... so i'm not sure of any workaround for larger files at this point

m-a-sch commented 1 week ago

My work around is to wait. Is this the first major outage of this beautiful product?

m-a-sch commented 1 week ago

It is fixed. Well done GOOGLE!

adboio commented 1 week ago

working for me now too!

davaram commented 1 week ago

It’s finally working again!

But, Why?

logankilpatrick commented 1 week ago

Hey! We rolled out a fix for this issue, apologies for the delay. I am going to also get a status page setup to make it easier to follow this stuff in the future.