GoogleGenerativeAIFetchError: [500 Internal Server Error] Failed to convert server response to JSON
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.
@BleedingDev, Thank you reporting this issue. Is this intermittent error or you are facing this error consistently?
@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...
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.
It just automagically fixed itself, don't know why. :) It works via Postman, gonna check if it works with AI SDK as well.
Having the same issue:
- 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,
});
- Now, it's throwing a 500 error. This error also occurs with
getFileanduploadFilecalls.
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 glad that somebody else has this issue. It solved and 15 minutes after I posted that it's solved, it broke again. :(
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.
Facing the same error. Is there a solution for this?
Why is this closed?
I consistently get this following this scenario.
- upload a file via uploadFile, wait for state to be ACTIVE (10s video)
- .getFile(fileName);, check state is ACTIVE before step 3
- use the file with genAI .getGenerativeModel({ model: "gemini-2.0-flash-exp" }) .generateContent(...
- call deleteFile from step 2 after response
- 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
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
Is there any update over this issue, i'm also getting the same response as @BleedingDev
Same here, also having the same issue when trying genai.list_files() after uploading a few ~30 second long videos.
@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.
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"
Thanks for raising this issue! I have opened a ticket with our API team to investigate, we'll keep you updated on our progress
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
@hkt74
To reproduce this consistently you can call listFiles with pageSize larger that total number of files
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.
Can vouch 100%.
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 theContent-Lengthheader 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 theFailed to convert server response to JSONerror - So the conclusion for me is that sending as
multipart/form-data;will break
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));
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.
I occasionally get this error as well
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.
@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.