Eli Tref
Eli Tref

Reputation: 142

[GoogleGenerativeAI Error]: GenerateContentRequest.contents[1].parts: contents.parts must not be empty

I'm working with GoogleGenerativeAI and I get this error periodically

[GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1/ /models/gemini-pro:generateContent: [400 Bad Request] * GenerateContentRequest.contents[1].parts: contents.parts must not be empty.

How to display GenerateContentRequest in the console to see what's wrong? I do not understand where the data is lost, especially since the error rarely occurs.

My code

const genAI = new GoogleGenerativeAI(apiKey)
const model = genAI.getGenerativeModel({
  model: "gemini-pro",
})

const chat = model.startChat({
      history: [],
      generationConfig: {
        maxOutputTokens: 10000,
      }
    })

let result = await chat.sendMessage(prompt)

Upvotes: 6

Views: 5113

Answers (2)

kitt
kitt

Reputation: 632

Disabling all safety settings can save this case in Gemini 1.5.

Upvotes: 1

Eli Tref
Eli Tref

Reputation: 142

Found an error in the chat object

This is a correct history

_history: [
     { role: 'user', parts: [Array] },
     { parts: [Array], role: 'model' }
   ],

This is a mistake

   _history: [ { role: 'user', parts: [Array] }, { parts: [], role: 'model' } ],
   _sendPromise: Promise {
     <rejected> GoogleGenerativeAIError: [400 Bad Request] * GenerateContentRequest.contents[1].parts: contents.parts must not be empty.
   },

That is, the missing parts were not transferred by Google.

Upvotes: 3

Related Questions