Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🔝fix: Re-order System Message to Top for Mistral API Payloads (#1678)

Danny Avila9fad1b2caee1
diff --git a/api/app/clients/OpenAIClient.js b/api/app/clients/OpenAIClient.js
index d1b908efbb..96f4bb1b04 100644
--- a/api/app/clients/OpenAIClient.js
+++ b/api/app/clients/OpenAIClient.js
@@ -976,9 +976,22 @@ ${convo}
...opts,
});
- /* hacky fix for Mistral AI API not allowing a singular system message in payload */
+ /* hacky fixes for Mistral AI API:
+ - Re-orders system message to the top of the messages payload, as not allowed anywhere else
+ - If there is only one message and it's a system message, change the role to user
+ */
if (opts.baseURL.includes('https://api.mistral.ai/v1') && modelOptions.messages) {
const { messages } = modelOptions;
+
+ const systemMessageIndex = messages.findIndex((msg) => msg.role === 'system');
+
+ if (systemMessageIndex > 0) {
+ const [systemMessage] = messages.splice(systemMessageIndex, 1);
+ messages.unshift(systemMessage);
+ }
+
+ modelOptions.messages = messages;
+
if (messages.length === 1 && messages[0].role === 'system') {
modelOptions.messages[0].role = 'user';
}