Hello, I am using the gemini flash 2.0 experimental model with the Go SDK to analyze video files.
Everything works well most times but sometimes the video fails to be processed.
This is the error I get:
state: FileStateFailed, error: rpc error: code = InvalidArgument desc = The file failed to be processed.
This is my code:
// uploadFile uploads a file to gemini and waits for it to finish processing
func (g *GeminiClient) uploadFile(ctx context.Context, file io.ReadCloser) (*genai.File, error) {
fileBytes, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("could not read file: %w", err)
}
fileType, err := filetype.Match(fileBytes)
if err != nil {
return nil, fmt.Errorf("could not determine file type: %w", err)
}
uploadedFile, err := g.gemini.UploadFile(ctx, "", bytes.NewReader(fileBytes), &genai.UploadFileOptions{
MIMEType: fileType.MIME.Value,
})
if err != nil {
return nil, fmt.Errorf("could not upload video: %w", err)
}
counter := 0
for uploadedFile.State == genai.FileStateProcessing {
// Check if we've reached the maximum number of attempts
if counter >= g.opts.MaxPollAttempts {
return nil, fmt.Errorf("video was still processing after %d attempts", g.opts.MaxPollAttempts)
}
counter++
// Don't wait on the first attempt
if counter > 1 {
time.Sleep(g.opts.PollInterval)
}
// Fetch the file from the API again.
uploadedFile, err = g.gemini.GetFile(ctx, uploadedFile.Name)
if err != nil {
return nil, fmt.Errorf("could not get file: %w", err)
}
}
if uploadedFile.State != genai.FileStateActive {
return nil, fmt.Errorf("failed to process file: state: %s, error: %s", uploadedFile.State, uploadedFile.Error)
}
return uploadedFile, nil
}
I am currently using a retry with exponential backoff to mitigate this failure. But still after 10+ tries it fails.
I am open to any suggestions.
Thank you in advance!