I’m working on integrating Google’s Gemini API into my Node.js application and running into a frustrating issue. When I try to use the chat history feature, I keep getting this weird error about the ‘in’ operator. The strange thing is that everything works fine when I don’t include any history at all.
Here’s the error message I’m seeing:
{"error": "Cannot use 'in' operator to search for 'text' in H"}
My configuration looks like this:
conversationHistory: [
{
role: "user",
parts: "Hi there, I own 3 cats at home.",
},
{
role: "model",
parts: "Nice to meet you. How can I help you today?",
},
],
modelSettings: {
maxOutputTokens: 150,
},
The full stack trace shows:
TypeError: Cannot use 'in' operator to search for 'text' in H
at validateChatHistory (node_modules/@google/generative-ai/dist/index.js:760:25)
at new ChatSession (node_modules/@google/generative-ai/dist/index.js:816:13)
at GenerativeModel.startChat (node_modules/@google/generative-ai/dist/index.js:1035:16)
Has anyone else faced this problem? What am I doing wrong with the history format?
Hit this exact issue last month building a customer support bot. Your parts structure is wrong.
You’re passing strings directly to parts, but Gemini wants an array of objects with a text property.
Fix your history format like this:
conversationHistory: [
{
role: "user",
parts: [{text: "Hi there, I own 3 cats at home."}],
},
{
role: "model",
parts: [{text: "Nice to meet you. How can I help you today?"}],
},
],
The ‘in’ operator error happens because validation tries to check for the text property in an object, but you’re passing a string.
This video shows the exact same error and fix:
Wrap your text in the proper object structure and you’re good. This got half our team when we started using the API.
Hit this exact issue two weeks ago migrating our chatbot from OpenAI. Gemini’s SDK is super picky about object structure. What’s happening: the SDK’s validateChatHistory function loops through your history array expecting objects, but you’re passing raw strings. It crashes at line 760 when it tries 'text' in someVariable on a string instead of an object. Beyond fixing the parts array, watch your role names. I used ‘assistant’ instead of ‘model’ and got completely different errors. Empty or undefined parts in your history will also throw similar cryptic errors. Debugging tip: log JSON.stringify(conversationHistory) right before startChat to verify your structure. Trust me, Gemini’s docs are terrible about explaining this requirement.
yeah, hit this same issue last week. the error message is total garbage - it’s not actually about the ‘in’ operator at all. gemini’s validation just freaks out when it gets a string instead of an object. also check your history array for any null or empty entries - those will break things without warning.
Hit this same TypeError six months back building a therapy chatbot. The error message is total garbage - makes you think it’s some weird JavaScript thing when it’s just a data structure issue. What threw me was the API worked fine for single messages but died the second I added conversation history. Wasted hours thinking it was a scope problem because of that ‘in’ operator message. Here’s what’s actually happening: Gemini’s validation expects each parts entry to be an object it can loop through, not a string. When it hits your string and tries to find a ‘text’ property, JavaScript throws the TypeError. Pro tip nobody mentioned - stay consistent with your structure across the entire conversation flow. I had some history entries formatted right and others wrong, which caused random failures that were even worse to debug.
Been dealing with API integrations for years and this exact error drove me crazy until I figured out the pattern. The issue is definitely in your parts structure like danielr mentioned.
But here’s the thing - manually handling all these API configurations and error handling gets old fast. I’ve been automating Gemini API integrations using Latenode and it handles all the proper formatting automatically.
What I love about it is you don’t have to worry about getting the parts structure right or debugging these validation errors. Just set up your conversation flow visually and it generates the correct API calls.
I built a whole customer chat system last quarter where Latenode manages the conversation history, handles the API responses, and even connects to our database to store chat logs. No more wrestling with JSON structures or debugging cryptic operator errors.
You can still fix your current code with the proper array format, but if you want to skip all this manual configuration headache, check out the automation approach: https://latenode.com