/n8n-tutorials

How to fix duplicate conversation threads with Gemini agents in n8n?

Fix duplicate conversation threads with Gemini agents in n8n by disabling auto-response in HTTP nodes and implementing conversation state management for seamless, coherent chat flows.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free consultation

How to fix duplicate conversation threads with Gemini agents in n8n?

To fix duplicate conversation threads with Gemini agents in n8n, disable the HTTP response node from auto-responding to initial messages and instead implement a proper conversation flow with state management. This ensures messages are properly threaded and prevents multiple conversation threads from being created.

 

Understanding the Problem with Duplicate Conversation Threads

 

When implementing Gemini agents in n8n workflows, you might encounter an issue where multiple conversation threads are created instead of maintaining a single coherent conversation. This typically happens because each new request to the Gemini AI node creates a fresh conversation context rather than continuing an existing one.

 

Step 1: Diagnose the Root Cause

 

Before implementing a fix, it's important to understand why duplicate conversation threads occur:

  • Missing conversation history management
  • Improper handling of response nodes
  • Absence of conversation state tracking
  • Webhook/HTTP nodes auto-responding without proper context

 

Step 2: Set Up Proper Conversation State Management

 

To fix this issue, you need to implement proper conversation state management:


// Example structure for conversation state in Function node
function storeConversation(conversationId, message, isUser = true) {
  // Get existing conversations from workflow data
  const conversations = $workflow.data.conversations || {};
  
  // Initialize this conversation if it doesn't exist
  if (!conversations[conversationId]) {
    conversations[conversationId] = [];
  }
  
  // Add the message to the conversation history
  conversations[conversationId].push({
    role: isUser ? 'user' : 'model',
    parts: [{ text: message }]
  });
  
  // Store updated conversations back to workflow data
  $workflow.data.conversations = conversations;
  
  return conversations[conversationId];
}

 

Step 3: Modify Your Webhook/HTTP Trigger Node

 

Configure your webhook or HTTP node to properly identify and handle conversations:

  1. Open your n8n workflow
  2. Select your HTTP Request or Webhook node
  3. Ensure it captures a conversation ID (either from query parameters, headers, or generate one)

// In a Function node after your webhook
const incomingMessage = $input.item.json.message;
const conversationId = $input.item.json.conversationId || 
                       $input.item.json.headers['x-conversation-id'] || 
                       Date.now().toString();

// Store conversation ID for this workflow run
$workflow.data.currentConversationId = conversationId;

// Get or initialize conversation history
const conversationHistory = storeConversation(conversationId, incomingMessage);

return {
  conversationId,
  message: incomingMessage,
  history: conversationHistory
};

 

Step 4: Configure Your Gemini AI Node

 

Next, properly configure the Gemini AI node to use the conversation history:

  1. Select your Gemini AI node
  2. Set the "Operation" to "Chat"
  3. In the "History" field, map to the conversation history from previous steps

// Example of mapping expressions for Gemini AI node
// In the "History" field, use:
{{ $node["Function"].json.history }}

// In the "Message" field, use:
{{ $node["Function"].json.message }}

 

Step 5: Update Response Handling

 

After getting a response from Gemini, store it in the conversation history:


// In a Function node after Gemini AI
const geminiResponse = $input.item.json.response;
const conversationId = $workflow.data.currentConversationId;

// Store the AI's response in the conversation history
const updatedHistory = storeConversation(conversationId, geminiResponse, false);

return {
  conversationId,
  response: geminiResponse,
  history: updatedHistory
};

 

Step 6: Prevent Auto-Response in HTTP Response Node

 

A critical step to fix the duplicate thread issue is properly configuring your HTTP Response node:

  1. Select your HTTP Response node
  2. Ensure it only responds after the entire workflow has processed
  3. Configure it to return the conversation ID with the response

// Example of proper HTTP Response node configuration
// Response Body:
{
  "conversationId": "{{ $node['Function1'].json.conversationId }}",
  "response": "{{ $node['GeminiAI'].json.response }}",
  "success": true
}

 

Step 7: Implement a Complete Workflow with Error Handling

 

Here's a complete workflow structure to prevent duplicate threads:

  1. Webhook/HTTP Trigger → Captures incoming message
  2. Function Node → Extracts conversation ID and message
  3. IF Node → Checks if this is a new or existing conversation
  4. Function Node → Retrieves or initializes conversation history
  5. Gemini AI Node → Processes message with conversation history
  6. Function Node → Stores AI response in conversation history
  7. HTTP Response Node → Returns response with conversation ID

 

Step 8: Implement Timeout and Session Management

 

To prevent endless conversations and memory issues, implement conversation timeouts:


// In a Function node, add timeout logic
function cleanupOldConversations() {
  const conversations = $workflow.data.conversations || {};
  const now = Date.now();
  const timeout = 30 _ 60 _ 1000; // 30 minutes
  
  Object.keys(conversations).forEach(id => {
    const lastMessage = conversations\[id]\[conversations[id].length - 1];
    if (lastMessage && lastMessage.timestamp && (now - lastMessage.timestamp > timeout)) {
      delete conversations[id];
    }
  });
  
  $workflow.data.conversations = conversations;
}

// Call this function periodically
cleanupOldConversations();

 

Step 9: Testing Your Fixed Workflow

 

To test if your fix works correctly:

  1. Deploy your updated workflow
  2. Send an initial message to start a conversation
  3. Save the returned conversation ID
  4. Send follow-up messages with the same conversation ID
  5. Verify that all messages appear in a single coherent thread

Use a testing tool like Postman to send sequential requests:


// First request
POST /webhook-endpoint
{
  "message": "Hello, I have a question about n8n workflows"
}

// Response includes conversation ID
{
  "conversationId": "1234567890",
  "response": "Hi there! I'd be happy to help with your n8n workflow questions. What would you like to know?",
  "success": true
}

// Follow-up request (include the conversation ID)
POST /webhook-endpoint
{
  "conversationId": "1234567890",
  "message": "How do I fix the duplicate conversation issue?"
}

 

Step 10: Implement Advanced Error Recovery

 

Add error recovery to handle potential issues:


// In a Function node after Gemini AI
try {
  const geminiResponse = $input.item.json.response;
  const conversationId = $workflow.data.currentConversationId;
  
  // Store the AI's response in the conversation history
  const updatedHistory = storeConversation(conversationId, geminiResponse, false);
  
  return {
    conversationId,
    response: geminiResponse,
    history: updatedHistory,
    success: true
  };
} catch (error) {
  // Log the error
  console.error('Error processing Gemini response:', error);
  
  // Return a fallback response
  return {
    conversationId: $workflow.data.currentConversationId || 'error-recovery',
    response: "I'm sorry, there was an error processing your request. Please try again.",
    success: false,
    error: error.message
  };
}

 

Step 11: Optimize for Performance

 

To ensure your workflow remains performant even with many conversations:

  1. Implement conversation pruning to remove old or inactive conversations
  2. Consider using external storage for conversation histories (like n8n's variables feature)
  3. Limit the number of messages kept in history per conversation

// In a Function node, implement history truncation
function limitConversationHistory(conversationId, maxMessages = 10) {
  const conversations = $workflow.data.conversations || {};
  
  if (conversations[conversationId] && conversations[conversationId].length > maxMessages) {
    // Keep the first message (system prompt) and the last (maxMessages-1) messages
    conversations[conversationId] = [
      conversations\[conversationId]\[0],
      ...conversations[conversationId].slice(-(maxMessages-1))
    ];
  }
  
  $workflow.data.conversations = conversations;
  return conversations[conversationId];
}

// Call this after updating conversation history
limitConversationHistory($workflow.data.currentConversationId);

 

Troubleshooting Common Issues

 

If you still encounter problems with duplicate conversations:

  • Check that your conversation ID generation is consistent and unique
  • Verify that your workflow is correctly storing and retrieving conversation history
  • Ensure your HTTP Response node is properly configured and not sending premature responses
  • Look for race conditions in your workflow that might create multiple threads
  • Confirm that your Gemini AI node is correctly receiving the conversation history

 

Final Considerations

 

To maintain a robust solution:

  • Implement proper logging to track conversation flow
  • Consider implementing user authentication to link conversations to specific users
  • Add periodic database or file storage backups for conversation histories
  • Monitor your workflow's memory usage to prevent resource exhaustion

By following these steps, you'll effectively fix the issue of duplicate conversation threads with Gemini agents in n8n, ensuring a smooth and coherent conversation experience for your users.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022