Can't upload txt/pdf file with golang lib

I am trying to use snippet code from this doc gemini golang doc under " Locally stored PDFs" section.

My code:

func UploadPDFGemini() {

	ctx := context.Background()
	client, _ := genai.NewClient(ctx, &genai.ClientConfig{
		APIKey:  os.Getenv("MY_API_KEY_PASTED_HERE"),
		Backend: genai.BackendGeminiAPI,
	})

	pdfBytes, _ := os.ReadFile("sber.pdf")

	print("\n\n\n")
	print(len(pdfBytes))
	print("\n\n\n")

	parts := []*genai.Part{
		&genai.Part{
			InlineData: &genai.Blob{
				MIMEType: "application/pdf",
				Data:     pdfBytes,
			},
		},
		genai.NewPartFromText("Summarize this document"),
	}
	contents := []*genai.Content{
		genai.NewContentFromParts(parts, genai.RoleUser),
	}

	result, _ := client.Models.GenerateContent(
		ctx,
		"gemini-2.0-flash",
		contents,
		nil,
	)

	fmt.Println(result.Text())
}

And I am getting this error all the time, have no idea how to fix it. Also tried with .txt file and I am getting same error. I am sure I did read file in to bytes but then it crashes when trying perform request with bytes payload.

Simple Text generation capabilities works fine without file upload payload.

Please help have no idea how to fix it.

pdf or txt I placed in project directory

Hi @l111 ,

Welcome to the Forum !!
I have checked from my end it’s working fine, I have shared the reference code below.
Code -

package main

import (
“context”
“fmt”
“log”
“os”

genai "google.golang.org/genai"

)

func uploadPDFGemini() {
ctx := context.Background()
apiKey := os.Getenv(“GOOGLE_APPLICATION_CREDENTIALS”)
if apiKey == “” {
fmt.Println(“GOOGLE_API_KEY is not set”)
return
}
client, err := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: apiKey,
Backend: genai.BackendGeminiAPI,
})

if err != nil {
	log.Fatal(err)
}
pdfBytes, err := os.ReadFile("sample.pdf")
if err != nil {
	log.Fatalf("Failed to read PDF file: %v", err)
}

fmt.Printf("PDF size: %d bytes\n", len(pdfBytes))

parts := []*genai.Part{
	{
		InlineData: &genai.Blob{
			MIMEType: "application/pdf",
			Data:     pdfBytes,
		},
	},
	genai.NewPartFromText("Summarize this document"),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

result, err := client.Models.GenerateContent(
	ctx,
	"gemini-2.0-flash",
	contents,
	nil,
)
if err != nil {
	log.Fatalf("Error generating content: %v", err)
}

fmt.Println("Response:")
fmt.Println(result.Candidates[0].Content.Parts[0].Text)

}