From 948ac8a3ddd61919b1f333c544b3b593d6b61343 Mon Sep 17 00:00:00 2001
From: Sean Hatfield <seanhatfield5@gmail.com>
Date: Fri, 10 May 2024 17:33:25 -0700
Subject: [PATCH] [FIX] Validate messages schema for gemini provider (#1351)

validate messages schema for gemini provider
---
 server/utils/AiProviders/gemini/index.js | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/server/utils/AiProviders/gemini/index.js b/server/utils/AiProviders/gemini/index.js
index b9eb26c3c..962b12013 100644
--- a/server/utils/AiProviders/gemini/index.js
+++ b/server/utils/AiProviders/gemini/index.js
@@ -114,6 +114,24 @@ class GeminiLLM {
       allMessages[allMessages.length - 1].role === "user"
     )
       allMessages.pop();
+
+    // Validate that after every user message, there is a model message
+    // sometimes when using gemini we try to compress messages in order to retain as
+    // much context as possible but this may mess up the order of the messages that the gemini model expects
+    // we do this check to work around the edge case where 2 user prompts may be next to each other, in the message array
+    for (let i = 0; i < allMessages.length; i++) {
+      if (
+        allMessages[i].role === "user" &&
+        i < allMessages.length - 1 &&
+        allMessages[i + 1].role !== "model"
+      ) {
+        allMessages.splice(i + 1, 0, {
+          role: "model",
+          parts: [{ text: "Okay." }],
+        });
+      }
+    }
+
     return allMessages;
   }
 
-- 
GitLab