google-gemini / generative-ai-js

The official Node.js / Typescript library for the Google Gemini API
https://www.npmjs.com/package/@google/generative-ai
Apache License 2.0
679 stars 141 forks source link

GoogleAIFileManager.uploadFile timing out without returning file #169

Open smaira opened 3 months ago

smaira commented 3 months ago

Description of the bug:

I am trying to upload a local file to GoogleAIFileManager so I can pass it as a FilePart to model.generateContent(). The problem is the uploadFile method keeps timing out.

Error during file upload to Gemini: GoogleGenerativeAIFetchError: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/upload/v1beta/files: [408 Request Timeout]

Actual vs expected behavior:

As per the code I obtained from the GoogleAIStudio the uploadFile method should return a file:FileMetadataResponse so that I can check its status and proceed when it is "ACTIVE". But this is not happening. It just times out without returning anything.

Any other information you'd like to share?

No response

jjskuld commented 3 months ago

I am also seeing this issue. The strange thing is that when I'm using my local machine (MacBook Pro) it is timing out, however, when I push my code to Vercel, it is not timing out. It's still not clear to me why this is.

smaira commented 3 months ago

Hi, any update on when this bug can be picked up? Are there alternatives to AIFileManager that can be used in the interim?

hsubox76 commented 3 months ago

Unfortunately, it's difficult to fix a bug we can't reproduce or identify the cause of. How large is the file? How long does it take before timing out? One user mentioned that it times out from their local machine but not from their Vercel server. Could it be that the Vercel machine has a faster connection to our server?

My best guess without any further information is that the upload is taking long enough to pass the server's timeout limit, because of some combination of the file size and your connection speed. If you can confirm what that time interval is, that might help, but the only solution beyond that would be to increase the server's timeout limit, which is something I'd have to bring up to that team.

It's also possible there is a bug in the SDK, a hanging promise somewhere that swallows an error instead of rejecting, but it's difficult to diagnose that without a repro. If you're able to trace the code and see where it's hanging, and it's not on the fetch call, then that would point to that.

smaira commented 3 months ago

I understand, here are more details that may help.

  1. The file size is less than 30 KB. I am trying to upload a simple .docx file.
  2. The internet connection is not an issue. I am on a very high-speed connection.
  3. I traced the code and it times out after more than 3 minutes here :
    const response = await fetchFn(url.toString(), requestInit);
hsubox76 commented 3 months ago

I don't think docx is a supported mime type for GoogleAI file uploads. Have you tried converting it to plain text or maybe text/rtf? https://ai.google.dev/gemini-api/docs/prompting_with_media?lang=node

smaira commented 3 months ago

I have not tried the other format, but will give HTML a go. Shouldn't the call return an unsupported error rather than a timeout?

jjskuld commented 3 months ago

It shouldn't have anything to do with the file type. In our case we were uploading an audio wav file, which was only a few seconds long. When the same code is deployed to Vercel edge, it works as intended with no issues at all. However when running from my MacBook Pro dev machine it just hangs.

hsubox76 commented 3 months ago

I think you're experiencing two different issues. The original user seems to be uploading an unsupported file type, and there's a potential bug/issue to be reported there in asking for an error or feedback on unsupported file types instead of hanging. You seem to be having a separate issue possibly related to different runtime environments.

The issue where unsupported mimetypes hang instead of returning an error should probably be posted in the discussion forum https://discuss.ai.google.dev/ . I guess potentially it could be filed as a feature request to detect unsupported mime types in the SDK before making the request - I think I would make a separate issue in that case.

The issue where it works in one environment and doesn't work in another will take more digging to figure out what kind of issue it is. I would suggest making another issue for it so it doesn't get mixed up with this one. A couple of possibilities: they could be different regions, there could be firewall issues. Do you have any firewall/antivirus/proxy/etc on your local machine that might intercept requests? I'm guessing generateContent and other features work for you locally so it's probably not a region issue? Can you give us any more information about your local Macbook Pro environment that might help? Or your app build setup?

TinyMurky commented 1 month ago

I found that when I use node version 20.5.1, it won't work (408 timeout), but when I use 22.6.0 or 20.16.0 can upload successfully, hope this help!

manelsen commented 2 weeks ago

Updating node has just worked for me too.

Thank you so much.