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: Prevent Node Server Crash Due to Unhandled ChatCompletionMessage Error (#1278)

Danny Avilaf1bc711cd7bb
diff --git a/api/app/clients/OpenAIClient.js b/api/app/clients/OpenAIClient.js
index c47ce8a5f6..cb0abdf99a 100644
--- a/api/app/clients/OpenAIClient.js
+++ b/api/app/clients/OpenAIClient.js
@@ -771,6 +771,7 @@ ${convo}
...opts,
});
+ let UnexpectedRoleError = false;
if (modelOptions.stream) {
const stream = await openai.beta.chat.completions
.stream({
@@ -782,6 +783,12 @@ ${convo}
})
.on('error', (err) => {
handleOpenAIErrors(err, errorCallback, 'stream');
+ })
+ .on('finalMessage', (message) => {
+ if (message?.role !== 'assistant') {
+ stream.messages.push({ role: 'assistant', content: intermediateReply });
+ UnexpectedRoleError = true;
+ }
});
for await (const chunk of stream) {
@@ -794,9 +801,11 @@ ${convo}
}
}
- chatCompletion = await stream.finalChatCompletion().catch((err) => {
- handleOpenAIErrors(err, errorCallback, 'finalChatCompletion');
- });
+ if (!UnexpectedRoleError) {
+ chatCompletion = await stream.finalChatCompletion().catch((err) => {
+ handleOpenAIErrors(err, errorCallback, 'finalChatCompletion');
+ });
+ }
}
// regular completion
else {
@@ -809,7 +818,11 @@ ${convo}
});
}
- if (!chatCompletion && error) {
+ if (!chatCompletion && UnexpectedRoleError) {
+ throw new Error(
+ 'OpenAIError: Invalid final message: OpenAI expects final message to include role=assistant',
+ );
+ } else if (!chatCompletion && error) {
throw new Error(error);
} else if (!chatCompletion) {
throw new Error('Chat completion failed');
@@ -829,7 +842,9 @@ ${convo}
return '';
}
if (
- err?.message?.includes('stream ended') ||
+ err?.message?.includes(
+ 'OpenAIError: Invalid final message: OpenAI expects final message to include role=assistant',
+ ) ||
err?.message?.includes('The server had an error processing your request') ||
err?.message?.includes('missing finish_reason') ||
(err instanceof OpenAI.OpenAIError && err?.message?.includes('missing finish_reason'))
diff --git a/api/app/clients/tools/AzureAiSearch.js b/api/app/clients/tools/AzureAiSearch.js
index 0a552884df..4f4b8a1ffe 100644
--- a/api/app/clients/tools/AzureAiSearch.js
+++ b/api/app/clients/tools/AzureAiSearch.js
@@ -17,17 +17,40 @@ class AzureAISearch extends StructuredTool {
super();
// Initialize properties using helper function
- this.serviceEndpoint = this._initializeField(fields.AZURE_AI_SEARCH_SERVICE_ENDPOINT, 'AZURE_AI_SEARCH_SERVICE_ENDPOINT');
- this.indexName = this._initializeField(fields.AZURE_AI_SEARCH_INDEX_NAME, 'AZURE_AI_SEARCH_INDEX_NAME');
+ this.serviceEndpoint = this._initializeField(
+ fields.AZURE_AI_SEARCH_SERVICE_ENDPOINT,
+ 'AZURE_AI_SEARCH_SERVICE_ENDPOINT',
+ );
+ this.indexName = this._initializeField(
+ fields.AZURE_AI_SEARCH_INDEX_NAME,
+ 'AZURE_AI_SEARCH_INDEX_NAME',
+ );
this.apiKey = this._initializeField(fields.AZURE_AI_SEARCH_API_KEY, 'AZURE_AI_SEARCH_API_KEY');
- this.apiVersion = this._initializeField(fields.AZURE_AI_SEARCH_API_VERSION, 'AZURE_AI_SEARCH_API_VERSION', AzureAISearch.DEFAULT_API_VERSION);
- this.queryType = this._initializeField(fields.AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE, 'AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE', AzureAISearch.DEFAULT_QUERY_TYPE);
- this.top = this._initializeField(fields.AZURE_AI_SEARCH_SEARCH_OPTION_TOP, 'AZURE_AI_SEARCH_SEARCH_OPTION_TOP', AzureAISearch.DEFAULT_TOP);
- this.select = this._initializeField(fields.AZURE_AI_SEARCH_SEARCH_OPTION_SELECT, 'AZURE_AI_SEARCH_SEARCH_OPTION_SELECT');
+ this.apiVersion = this._initializeField(
+ fields.AZURE_AI_SEARCH_API_VERSION,
+ 'AZURE_AI_SEARCH_API_VERSION',
+ AzureAISearch.DEFAULT_API_VERSION,
+ );
+ this.queryType = this._initializeField(
+ fields.AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE,
+ 'AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE',
+ AzureAISearch.DEFAULT_QUERY_TYPE,
+ );
+ this.top = this._initializeField(
+ fields.AZURE_AI_SEARCH_SEARCH_OPTION_TOP,
+ 'AZURE_AI_SEARCH_SEARCH_OPTION_TOP',
+ AzureAISearch.DEFAULT_TOP,
+ );
+ this.select = this._initializeField(
+ fields.AZURE_AI_SEARCH_SEARCH_OPTION_SELECT,
+ 'AZURE_AI_SEARCH_SEARCH_OPTION_SELECT',
+ );
// Check for required fields
if (!this.serviceEndpoint || !this.indexName || !this.apiKey) {
- throw new Error('Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_INDEX_NAME, or AZURE_AI_SEARCH_API_KEY environment variable.');
+ throw new Error(
+ 'Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_INDEX_NAME, or AZURE_AI_SEARCH_API_KEY environment variable.',
+ );
}
// Create SearchClient
@@ -35,7 +58,7 @@ class AzureAISearch extends StructuredTool {
this.serviceEndpoint,
this.indexName,
new AzureKeyCredential(this.apiKey),
- { apiVersion: this.apiVersion }
+ { apiVersion: this.apiVersion },
);
// Define schema
diff --git a/api/app/clients/tools/structured/AzureAISearch.js b/api/app/clients/tools/structured/AzureAISearch.js
index 0a552884df..4f4b8a1ffe 100644
--- a/api/app/clients/tools/structured/AzureAISearch.js
+++ b/api/app/clients/tools/structured/AzureAISearch.js
@@ -17,17 +17,40 @@ class AzureAISearch extends StructuredTool {
super();
// Initialize properties using helper function
- this.serviceEndpoint = this._initializeField(fields.AZURE_AI_SEARCH_SERVICE_ENDPOINT, 'AZURE_AI_SEARCH_SERVICE_ENDPOINT');
- this.indexName = this._initializeField(fields.AZURE_AI_SEARCH_INDEX_NAME, 'AZURE_AI_SEARCH_INDEX_NAME');
+ this.serviceEndpoint = this._initializeField(
+ fields.AZURE_AI_SEARCH_SERVICE_ENDPOINT,
+ 'AZURE_AI_SEARCH_SERVICE_ENDPOINT',
+ );
+ this.indexName = this._initializeField(
+ fields.AZURE_AI_SEARCH_INDEX_NAME,
+ 'AZURE_AI_SEARCH_INDEX_NAME',
+ );
this.apiKey = this._initializeField(fields.AZURE_AI_SEARCH_API_KEY, 'AZURE_AI_SEARCH_API_KEY');
- this.apiVersion = this._initializeField(fields.AZURE_AI_SEARCH_API_VERSION, 'AZURE_AI_SEARCH_API_VERSION', AzureAISearch.DEFAULT_API_VERSION);
- this.queryType = this._initializeField(fields.AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE, 'AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE', AzureAISearch.DEFAULT_QUERY_TYPE);
- this.top = this._initializeField(fields.AZURE_AI_SEARCH_SEARCH_OPTION_TOP, 'AZURE_AI_SEARCH_SEARCH_OPTION_TOP', AzureAISearch.DEFAULT_TOP);
- this.select = this._initializeField(fields.AZURE_AI_SEARCH_SEARCH_OPTION_SELECT, 'AZURE_AI_SEARCH_SEARCH_OPTION_SELECT');
+ this.apiVersion = this._initializeField(
+ fields.AZURE_AI_SEARCH_API_VERSION,
+ 'AZURE_AI_SEARCH_API_VERSION',
+ AzureAISearch.DEFAULT_API_VERSION,
+ );
+ this.queryType = this._initializeField(
+ fields.AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE,
+ 'AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE',
+ AzureAISearch.DEFAULT_QUERY_TYPE,
+ );
+ this.top = this._initializeField(
+ fields.AZURE_AI_SEARCH_SEARCH_OPTION_TOP,
+ 'AZURE_AI_SEARCH_SEARCH_OPTION_TOP',
+ AzureAISearch.DEFAULT_TOP,
+ );
+ this.select = this._initializeField(
+ fields.AZURE_AI_SEARCH_SEARCH_OPTION_SELECT,
+ 'AZURE_AI_SEARCH_SEARCH_OPTION_SELECT',
+ );
// Check for required fields
if (!this.serviceEndpoint || !this.indexName || !this.apiKey) {
- throw new Error('Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_INDEX_NAME, or AZURE_AI_SEARCH_API_KEY environment variable.');
+ throw new Error(
+ 'Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_INDEX_NAME, or AZURE_AI_SEARCH_API_KEY environment variable.',
+ );
}
// Create SearchClient
@@ -35,7 +58,7 @@ class AzureAISearch extends StructuredTool {
this.serviceEndpoint,
this.indexName,
new AzureKeyCredential(this.apiKey),
- { apiVersion: this.apiVersion }
+ { apiVersion: this.apiVersion },
);
// Define schema
diff --git a/api/package.json b/api/package.json
index a4b8e1a318..521700da28 100644
--- a/api/package.json
+++ b/api/package.json
@@ -59,7 +59,7 @@
"multer": "^1.4.5-lts.1",
"nodejs-gpt": "^1.37.4",
"nodemailer": "^6.9.4",
- "openai": "^4.16.1",
+ "openai": "^4.20.1",
"openai-chat-tokens": "^0.2.8",
"openid-client": "^5.4.2",
"passport": "^0.6.0",
diff --git a/api/server/index.js b/api/server/index.js
index d8d1c18053..52500cd967 100644
--- a/api/server/index.js
+++ b/api/server/index.js
@@ -105,7 +105,7 @@ process.on('uncaughtException', (err) => {
return;
}
- if (err.message.includes('OpenAIError')) {
+ if (err.message.includes('OpenAIError') || err.message.includes('ChatCompletionMessage')) {
console.error(
'\n\nAn Uncaught `OpenAIError` error may be due to your reverse-proxy setup or stream configuration, or a bug in the `openai` node package.',
);
diff --git a/api/server/routes/endpoints/openAI/addTitle.js b/api/server/routes/endpoints/openAI/addTitle.js
index eb43e51735..3197374a68 100644
--- a/api/server/routes/endpoints/openAI/addTitle.js
+++ b/api/server/routes/endpoints/openAI/addTitle.js
@@ -1,5 +1,5 @@
-const { isEnabled } = require('../../../utils');
-const { saveConvo } = require('../../../../models');
+const { saveConvo } = require('~/models');
+const { isEnabled } = require('~/server/utils');
const addTitle = async (req, { text, response, client }) => {
const { TITLE_CONVO = 'true' } = process.env ?? {};
@@ -7,6 +7,11 @@ const addTitle = async (req, { text, response, client }) => {
return;
}
+ // If the request was aborted, don't generate the title.
+ if (client.abortController.signal.aborted) {
+ return;
+ }
+
const title = await client.titleConvo({ text, responseText: response?.text });
await saveConvo(req.user.id, {
conversationId: response.conversationId,
diff --git a/package-lock.json b/package-lock.json
index a4dc94bf65..3616f36b20 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -7,7 +7,6 @@
"": {
"name": "LibreChat",
"version": "0.6.1",
- "hasInstallScript": true,
"license": "ISC",
"workspaces": [
"api",
@@ -74,7 +73,7 @@
"multer": "^1.4.5-lts.1",
"nodejs-gpt": "^1.37.4",
"nodemailer": "^6.9.4",
- "openai": "^4.16.1",
+ "openai": "^4.20.1",
"openai-chat-tokens": "^0.2.8",
"openid-client": "^5.4.2",
"passport": "^0.6.0",
@@ -18170,9 +18169,9 @@
}
},
"node_modules/openai": {
- "version": "4.17.4",
- "resolved": "https://registry.npmjs.org/openai/-/openai-4.17.4.tgz",
- "integrity": "sha512-ThRFkl6snLbcAKS58St7N3CaKuI5WdYUvIjPvf4s+8SdymgNtOfzmZcZXVcCefx04oKFnvZJvIcTh3eAFUUhAQ==",
+ "version": "4.20.1",
+ "resolved": "https://registry.npmjs.org/openai/-/openai-4.20.1.tgz",
+ "integrity": "sha512-Dd3q8EvINfganZFtg6V36HjrMaihqRgIcKiHua4Nq9aw/PxOP48dhbsk8x5klrxajt5Lpnc1KTOG5i1S6BKAJA==",
"dependencies": {
"@types/node": "^18.11.18",
"@types/node-fetch": "^2.6.4",