generative-ai-js icon indicating copy to clipboard operation
generative-ai-js copied to clipboard

GoogleGenerativeAIFetchError: [500 Internal Server Error] Failed to convert server response to JSON

Open BleedingDev opened this issue 1 year ago • 26 comments

Description of the bug:

I am using GoogleAIFileManager from @google/generative-ai/server at version 0.13.0, but it was broken even in version 0.12.

No matter the file I try to upload, so that I can use it later on for the model I get this error:

GoogleGenerativeAIFetchError: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/files: [500 Internal Server Error] Failed to convert server response to JSON

It was working well in few days ago, but then out of nowhere I started getting errors like this and nothing helped.

Actual vs expected behavior:

Expected

Upload file without any problem.

Actual

GoogleGenerativeAIFetchError: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/files: [500 Internal Server Error] Failed to convert server response to JSON

Any other information you'd like to share?

Not just upload file, but also delete file method is broken and it all leads to the function makeFilesRequest.

But it is probably not in the SDK as even Postman fails with direct POST/GET request to the API. image

BleedingDev avatar Jun 19 '24 21:06 BleedingDev

@BleedingDev, Thank you reporting this issue. Is this intermittent error or you are facing this error consistently?

singhniraj08 avatar Jun 20 '24 06:06 singhniraj08

@singhniraj08 I am facing it consistently. I tried changing API keys, updating the library, changing IP address in case I did something wrong to be blocked...

BleedingDev avatar Jun 20 '24 06:06 BleedingDev

Since you're experiencing this even when bypassing the API and sending a direct request to the service, I would suggest posting on the discussion forum https://discuss.ai.google.dev/ to try to reach the backend team. It doesn't seem like anything we can address in the SDK.

hsubox76 avatar Jun 24 '24 18:06 hsubox76

It just automagically fixed itself, don't know why. :) It works via Postman, gonna check if it works with AI SDK as well.

BleedingDev avatar Jun 25 '24 12:06 BleedingDev

Having the same issue:

  1. Initially, this code was returning an empty object, when the TypeScript type expects an object with specific keys (files, nextPageToken):
const response = await fileManager.listFiles({
    pageSize: 100,
    pageToken: nextPageToken,
});
  1. Now, it's throwing a 500 error. This error also occurs with getFile and uploadFile calls.
GoogleGenerativeAIFetchError: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/files/a-esfun1pkg-ergifdrhrn: [500 Internal Server Error] Failed to convert server response to JSON
    at handleResponseNotOk (file:///Users/alp/Desktop/dev/projects/goblins-v3/packages/server/node_modules/@google/generative-ai/dist/server/index.mjs:127:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async makeRequest (file:///Users/alp/Desktop/dev/projects/goblins-v3/packages/server/node_modules/@google/generative-ai/dist/server/index.mjs:100:9)
    at async GoogleAIFileManager.getFile (file:///Users/alp/Desktop/dev/projects/goblins-v3/packages/server/node_modules/@google/generative-ai/dist/server/index.mjs:335:26)

Not sure if the timeline of the bugs is related, but thought that it'd be useful to include considering it seems to have something to do with the return type.

FYI: I was on 0.12.0 as well. Upgrading to 0.13.0 didn't resolve the issue.

Karavil avatar Jun 25 '24 18:06 Karavil

@Karavil glad that somebody else has this issue. It solved and 15 minutes after I posted that it's solved, it broke again. :(

BleedingDev avatar Jun 26 '24 11:06 BleedingDev

I am also having this issue. It appears uploadFile works but getFile throws [500 Internal Server Error] Failed to convert server response to JSON. I'm splitting my file into 5 parts and 3 out of the 5 return this error.

jaredworkman21 avatar Jul 17 '24 14:07 jaredworkman21

Facing the same error. Is there a solution for this?

jaydedevyn avatar Dec 18 '24 00:12 jaydedevyn

Why is this closed?

jaydedevyn avatar Dec 20 '24 21:12 jaydedevyn

I consistently get this following this scenario.

  1. upload a file via uploadFile, wait for state to be ACTIVE (10s video)
  2. .getFile(fileName);, check state is ACTIVE before step 3
  3. use the file with genAI .getGenerativeModel({ model: "gemini-2.0-flash-exp" }) .generateContent(...
  4. call deleteFile from step 2 after response
  5. try to upload another file.... always the same 500 ISE
GoogleGenerativeAIFetchError: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/files/9xe0iylwxcur: [500 Internal Server Error] Failed to convert server response to JSON
   at handleResponseNotOk (file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/node_modules/@google/generative-ai/dist/server/index.mjs:127:11)
   at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
   at async makeRequest (file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/node_modules/@google/generative-ai/dist/server/index.mjs:100:9)
   at async GoogleAIFileManager.getFile (file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/node_modules/@google/generative-ai/dist/server/index.mjs:352:26)
   at async checkProgress (file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/server/upload.mjs:58:20)
   at async file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/server/index.mjs:38:22 {
 status: 500,
 statusText: 'Internal Server Error',
 errorDetails: undefined

relates to https://github.com/scottleedavis/ai-portland-rocketleague/issues/1

scottleedavis avatar Dec 29 '24 02:12 scottleedavis

Additionally, listFiles returns a similar error

import { GoogleAIFileManager } from "@google/generative-ai/server";

const fileManager = new GoogleAIFileManager(process.env.GEMINI_API_KEY);


const doit = async () => {
    const files = await fileManager.listFiles();
    console.log(files);
}

doit();
file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/node_modules/@google/generative-ai/dist/server/index.mjs:127
    throw new GoogleGenerativeAIFetchError(`Error fetching from ${url.toString()}: [${response.status} ${response.statusText}] ${message}`, response.status, response.statusText, errorDetails);
          ^

GoogleGenerativeAIFetchError: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/files: [500 Internal Server Error] Failed to convert server response to JSON
    at handleResponseNotOk (file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/node_modules/@google/generative-ai/dist/server/index.mjs:127:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async makeRequest (file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/node_modules/@google/generative-ai/dist/server/index.mjs:100:9)
    at async GoogleAIFileManager.listFiles (file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/node_modules/@google/generative-ai/dist/server/index.mjs:337:26)
    at async doit (file:///mnt/c/Users/scott/Documents/workspace/ai-portland-rocketleague/ScreenShareFeedback/server/delete-all.mjs:7:19) {
  status: 500,
  statusText: 'Internal Server Error',
  errorDetails: undefined
}

Node.js v20.18.1

scottleedavis avatar Dec 29 '24 03:12 scottleedavis

Is there any update over this issue, i'm also getting the same response as @BleedingDev

sharmaAshish-dev avatar Jan 18 '25 10:01 sharmaAshish-dev

Same here, also having the same issue when trying genai.list_files() after uploading a few ~30 second long videos.

mikies-copoulos-amelia avatar Feb 07 '25 02:02 mikies-copoulos-amelia

@BleedingDev @evansenter @logankilpatrick

This remains an ongoing issue for developers, it is an API issue, ie not related to any particular SDK:

https://github.com/google/generative-ai-go/issues/208

https://github.com/google-gemini/generative-ai-js/issues/177

https://discuss.ai.google.dev/t/http-500-error-for-file-apis/6362

And a bunch of other examples on the web, none are addressed with meaning information that points to a source of the issue. Are any actual Google customers using the video understanding feature that requires use of the media upload service?

Even some of the sample code posted in the repo and the Google website appear to be incorrect, suggesting that no one actually tested it to confirm it works.

The last link above shows the "Resolution" was to use a competing service, does Google not care ?

The gist of the issue is that the following endpoints return an error after successful media uploads:

https://generativelanguage.googleapis.com/v1beta/files/oej6an9qy2hg?key=... https://generativelanguage.googleapis.com/v1beta/files?key=...

Response { "error": { "code": 500, "message": "Failed to convert server response to JSON", "status": "INTERNAL" } }

I have for instance used a sample video from your repo to ensure it is not due to a bad file.

The list operation doesn't work so it is not possible to even know what's in storage. I created a new API key to no avail.

Devmond avatar Feb 19 '25 18:02 Devmond

After moving my server that uses this SDK to AWS I am experiencing this about 80% of the time. When I was developing locally I never experienced it.

EDIT: it happens 100% of the time on a deployed serverless function. I think the only time it worked there was the first time. As others have reported even calling:

const fileManager = new GoogleAIFileManager(process.env.GEMINI_API_KEY);
const list = await fileManager.listFiles()

Throws the error. I thought it could be because passing mime type video/quicktime might be the cause, because it is not listed in the supported Gemini mime types, but that's not the case. It happens with video/mp4 and the Gemini docs are incorrect because video/mov is not actually a real mime type.

I would say this issue should not be labeled "awaiting user response" but "awaiting google response"

whenmoon avatar Feb 25 '25 20:02 whenmoon

Thanks for raising this issue! I have opened a ticket with our API team to investigate, we'll keep you updated on our progress

hkt74 avatar Feb 25 '25 23:02 hkt74

Here's more info from my implementation. This function works locally, but when it runs on AWS it fails (process.env.ENVIRONMENT === 'production'). The file is stored on AWS S3 and then downloaded to temp storage attached to the lambda function:

const s3Client = new S3Client({ region: 'eu-west-3' });

export const uploadVideoToModel = async (
  processedVideoLocation: string,
  mimeType: string | false,
  displayName: string
): Promise<UploadFileResponse | Error | undefined> => {
  if (process.env.GEMINI_API_KEY) {
    const fileManager = new GoogleAIFileManager(process.env.GEMINI_API_KEY);

    if (mimeType) {
      try {
        let localFilePath = ''
        if (process.env.ENVIRONMENT === 'production') {
          const url = new URL(processedVideoLocation);

          const bucketName = url.hostname.split('.')[0];
          const key = url.pathname.substring(1);

          const s3Location = `${key}`

          localFilePath = path.join('/tmp', path.basename(key));
          console.log('Downloading to local path:', localFilePath);

          const downloadCommand = new GetObjectCommand({
            Bucket: bucketName,
            Key: s3Location
          });

          const { Body } = await s3Client.send(downloadCommand);
          
          const fileStream = fs.createWriteStream(localFilePath);

          await new Promise<void>((resolve, reject) => {
            (Body as any).pipe(fileStream);
            (Body as any).on('end', resolve);
            (Body as any).on('error', reject);
          });
        } else {
          localFilePath = processedVideoLocation
        }

        try {
          const uploadResponse = await fileManager.uploadFile(localFilePath, { mimeType, displayName });
          console.log(`Uploaded file ${uploadResponse.file.displayName} as: ${uploadResponse.file.uri}`);

          const name = uploadResponse.file.name;
          let file = await fileManager.getFile(name);
          while (file.state === FileState.PROCESSING) {
            process.stdout.write(".");
            await new Promise((resolve) => setTimeout(resolve, 2000));
            file = await fileManager.getFile(name);
          }

          if (file.state === FileState.FAILED) {
            throw Error("Video upload failed.");
          }

          console.log(`File ${file.displayName} is ready for inference as ${file.uri}`);

          fs.unlinkSync(localFilePath);

          return uploadResponse;
        } catch (error) {
          console.error('Video upload failed:', error);
        }
      } catch (error) {
        console.error('Video upload failed:', error);
        throw Error(`Video upload failed: ${error}`);
      }
    } else {
      throw Error("Could not determine mime type.");
    }
  } else {
    throw Error("Failed: missing API key.");
  }
}

Could it have something to do with the AWS region?

Edit: I changed the aws region is-east-1 but didn't make a difference

whenmoon avatar Feb 26 '25 22:02 whenmoon

@hkt74 To reproduce this consistently you can call listFiles with pageSize larger that total number of files

zomchak-code avatar Mar 11 '25 11:03 zomchak-code

This is happening to me.

Steps:

  • Get URL ( uri = URI("https://generativelanguage.googleapis.com/upload/v1beta/files?key=#{GEMINI_API_KEY}"))
  • Upload file
  • GET file, ex: https://generativelanguage.googleapis.com/v1beta/files/ykb3f2rhckm6?key=XXXXXX

I haven't seen it work and this is blocking me from using the API.

giorgenes avatar Apr 03 '25 23:04 giorgenes

Can vouch 100%.

CryptAlchemy avatar Apr 27 '25 23:04 CryptAlchemy

I will share my thoughts.

  • First it happens for me when I upload a video (mp4), not happen when uploading an audio
  • For me, it fails on Coldfusion cfhttp. It works on CURL, the same exact video.
  • On CURL, I can make it fail when instead of using --data-binary, do you use -F . Al so you has to remove the Content-Length header or you will get errors of not sending the right amount of bytes, or add --http1.1, but anyway it will give you after proccesing the Failed to convert server response to JSON error
  • So the conclusion for me is that sending as multipart/form-data; will break

rodriciru avatar May 16 '25 09:05 rodriciru

It also fails when using --data instead of --data-binary. Also fixed on my Coldfusion side by sending as pure binary by application/x-www-form-urlencoded : cfhttpparam(type = "body", value = fileReadBinary(arguments.filePath));

rodriciru avatar May 16 '25 09:05 rodriciru

I occasionally get this error as well, it would've been more helpful to get the raw error message instead of api trying to parse the JSON.

KeremTurgutlu avatar Jul 03 '25 15:07 KeremTurgutlu

I occasionally get this error as well

yuanyijie avatar Jul 06 '25 07:07 yuanyijie

Is there a alternative or fallback if we get this error, I am getting while uploading it to Files API.

(500 INTERNAL. {'error': {'code': 500, 'message': 'Failed to convert server response to JSON', 'status': 'INTERNAL'}})

This should be resolved else how will our application work.

Rishav-hub avatar Jul 19 '25 23:07 Rishav-hub

@hkt74 Is there an update on this issue? Developers are effectively blocked from using the following sequence: upload file, get file to confirm success, then generate content.

drew-beckmen avatar Oct 21 '25 14:10 drew-beckmen