Converting stateless chat history from Google Generative AI to Vertex AI platform

I’m working on moving my chatbot application from Google’s generative AI library to Vertex AI but keep running into issues.

When I try to use the same conversation format that worked perfectly with the old library, I get this error message:

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

My original approach was using a stateless conversation system where I maintain message history manually:

conversation_history = [
    {
        "role": "user", 
        "parts": [initialize_assistant(input_data)]
    }, 
    {
        "role": "model", 
        "parts": ["Hello! I'm Alex, your database assistant. Ready to help you build queries."]
    }
]

conversation_history.append({"role": "user", "parts": [user_input]})

ai_response = gemini_model.generate_content(conversation_history)

This approach let me keep full control over the conversation flow without relying on session management. But now with Vertex AI, the same code structure throws that MergeFrom error.

I’ve looked at Google’s migration guides but they mostly show examples using the chat interface instead of the stateless message array approach I prefer.

Is there a way to maintain this stateless conversation pattern in Vertex AI, or do I need to completely restructure my code to use their chat session system? Any suggestions would be really helpful!

Been there with this migration nightmare. The Part object issue is annoying but fixable.

I created a helper function for the conversion:

def format_message(role, content):
    return {
        "role": role,
        "parts": [{"text": content}]
    }

Then rebuild your history:

conversation_history = [
    format_message("user", initialize_assistant(input_data)),
    format_message("model", "Hello! I'm Alex, your database assistant.")
]

Vertex AI’s pickier about message structure. Each part needs to be a dict with a “text” key, not just a string.

I prefer this stateless approach too. Makes debugging easier and you can modify conversation context on the fly without dealing with session state.

Watch out though - Vertex AI counts tokens differently than the old library. Your conversation history might hit limits faster. I started chunking longer conversations by keeping only the last N exchanges plus the initial system message.

The migration docs push chat sessions because that’s what most people use, but stateless works fine once you nail the message format.

I’ve hit this exact migration headache before. The error happens because Vertex AI wants Part objects, not plain strings in the parts array.

Two ways to fix it:

  1. Wrap your strings in Part objects:
from vertexai.generative_models import Part

conversation_history = [
    {
        "role": "user", 
        "parts": [Part.from_text(initialize_assistant(input_data))]
    }, 
    {
        "role": "model", 
        "parts": [Part.from_text("Hello! I'm Alex, your database assistant.")]
    }
]
  1. Use the content parameter instead of parts for simple text messages.

Honestly though, I’d skip both. Manual conversation history gets messy fast when you scale up or add context switching.

I handle all my chatbot migrations through Latenode now. It handles these API differences automatically and keeps your stateless approach without the Part object headaches or session management.

You can set up workflows that manage conversation history automatically while keeping full control. Plus when Google changes their API again (they will), you just update one connection instead of rewriting code.

Check it out at https://latenode.com

Had the same migration pain last month. Vertex AI wants proper content structure, not just strings. Use Content objects directly instead of raw dicts - vertexai.generative_models.Content with your parts wrapped properly. Saves a lot of headache vs manual formatting.