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

Error 400: Unsupported MIME type when trying to upload files to GenerateContent (.docx)

Open masihur1989 opened this issue 1 year ago • 4 comments

Description of the bug:

Using the below snippet getting error while trying to upload files to generate content. Using model gemini-1.5-flash

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"github.com/joho/godotenv"
	"google.golang.org/api/option"
)

func main() {
	fmt.Println("Hello World!")

	err := godotenv.Load()
	if err != nil {
		log.Fatal("Error loading .env key")
	}
	// Retrieve API key
	api_key := os.Getenv("GEMINI_API_KEY")
	// Initialize Gemini client
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(api_key))
	if err != nil {
		log.Fatal("Error Creatign Client: ", err)
	}
	defer client.Close()

	// Use Client.UploadFile to Upload a file to the service.
	// Pass it an io.Reader.
	f, err := os.Open("files/ResumeSample03.docx")
	if err != nil {
		log.Fatal("File Opening Error: ", err)
	}
	defer f.Close()
	// You can choose a name, or pass the empty string to generate a unique one.
	file, err := client.UploadFile(ctx, "", f, nil)
	if err != nil {
		log.Fatal("File Uploading Error: :", err)
	}

	// Connect to Gemini model
	model := client.GenerativeModel("gemini-1.5-flash")

	resp, err := model.GenerateContent(ctx, genai.FileData{URI: file.URI})
	if err != nil {
		log.Fatal("Error Getting Response: ", err)
	}

	printResponse(resp)
}

func printResponse(resp *genai.GenerateContentResponse) {
	for _, cand := range resp.Candidates {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}

Actual vs expected behavior:

Getting Below errors.

For pdf format googleapi: Error 400: Unsupported MIME type: application/pdf

For docx format. googleapi: Error 400: Unsupported MIME type: application/zip

Any other information you'd like to share?

No response

masihur1989 avatar Jul 07 '24 22:07 masihur1989

Looks like a dupe of #120

I looked in to it. And ai studio seem to generate code in Python and JS that looks very similar to this when a PDF is uploaded.

also look like an example for processing PDF file exist in the vertex sdk for go. https://cloud.google.com/vertex-ai/generative-ai/docs/samples/generativeaionvertexai-gemini-pdf

moficodes avatar Jul 07 '24 23:07 moficodes

PDFs are supported now -- see examples

eliben avatar Aug 06 '24 13:08 eliben

@eliben thanks for the update. I have checked and pdf is working fine. Is there any timeline at this time for supporting docx?

masihur1989 avatar Aug 13 '24 03:08 masihur1989

This is a general API issue, not specific to the Go SDK. I suggest asking in https://discuss.ai.google.dev/

eliben avatar Aug 14 '24 00:08 eliben

@jba completed? why?? I am having the same erro!

0wwafa avatar Jul 11 '25 18:07 0wwafa