google-cloud-vertex-aigoogle-geminigoogle-generativeai

Migrating multi-turn chat conversations from Google/generative-ai to Vertex AI


I am trying to migrate my Gemini project from Google-generative-ai to Vertex AI.

However, I ran into this error:

TypeError: Parameter to MergeFrom() must be instance of same class: expected <class 'Part'> got <class 'str'>.

My code essentially uses a stateless multi-turn conversation, just like here in the example at their documentation.

messages.append({'role':'model', 'parts':[response.text]})    
messages.append({'role':'user',
                 'parts':["Okay, how about a more detailed explanation to a high school student?"]})    
response = model.generate_content(messages)    
to_markdown(response.text)

When trying it at Vertex AI however, it ran into the above error. The documentation provided by Google isn't helping, instead using the chat function over the stateless conversation.

Is there any way to keep the stateless message handling for chat in Vertex AI, or do I have to convert to the chat method?

Help would be appreciated, thank you!

Here's an example of how my code looks like. This works under GenAI/Gemini.:

messages = [
          {
             "role": "user", 
             "parts": [get_system_prompt(bytes_data)]
          }, 
          {
             "role": "model", 
             "parts": ["Hi, I'm Q, your SQL Query Building Helper. Let's get started."]
             }
         ]

messages.append({"role": "user", "parts": [prompt]})

model.generate_content(messages)

This is the result when I run under Vertex AI.

TypeError: Parameter to MergeFrom() must be instance of same class: expected <class 'Part'> got <class 'str'>.


Solution

  • The parts attribute is supposed to contain an array of Part objects that can contain different kinds of parts. There are text parts, inlineData parts that contain images and such, etc.

    For text parts, however, the AI Studio library allows a shortcut, where you can just provide a string and it will convert it into a text part. Vertex AI's library doesn't do this for you.

    The answer that will work on both is to make sure you send a text part. Something like this:

    messages = [
              {
                 "role": "user", 
                 "parts": [
                   {
                     "text": get_system_prompt(bytes_data)
                   }
                 ]
              }, 
              {
                 "role": "model", 
                 "parts": [
                   {
                     "text": "Hi, I'm Q, your SQL Query Building Helper. Let's get started."
                   }
                 ]
               }
             ]
    

    See https://ai.google.dev/api/rest/v1beta/Content#Part for details about what the Part object needs to look like (on the REST level).