diff --git a/api/app/clients/OpenAIClient.js b/api/app/clients/OpenAIClient.js index efb09a6ed3..0d9e43e196 100644 --- a/api/app/clients/OpenAIClient.js +++ b/api/app/clients/OpenAIClient.js @@ -1250,14 +1250,23 @@ ${convo} throw new Error('Chat completion failed'); } - const { message, finish_reason } = chatCompletion.choices[0]; - if (chatCompletion) { - this.metadata = { finish_reason }; + const { choices } = chatCompletion; + if (!Array.isArray(choices) || choices.length === 0) { + logger.warn('[OpenAIClient] Chat completion response has no choices'); + return intermediateReply; } + const { message, finish_reason } = choices[0] ?? {}; + this.metadata = { finish_reason }; + logger.debug('[OpenAIClient] chatCompletion response', chatCompletion); - if (!message?.content?.trim() && intermediateReply.length) { + if (!message) { + logger.warn('[OpenAIClient] Message is undefined in chatCompletion response'); + return intermediateReply; + } + + if (typeof message.content !== 'string' || message.content.trim() === '') { logger.debug( '[OpenAIClient] chatCompletion: using intermediateReply due to empty message.content', { intermediateReply }, diff --git a/client/src/components/Chat/Messages/Content/Markdown.tsx b/client/src/components/Chat/Messages/Content/Markdown.tsx index 07c64d6443..be66f61c8e 100644 --- a/client/src/components/Chat/Messages/Content/Markdown.tsx +++ b/client/src/components/Chat/Messages/Content/Markdown.tsx @@ -144,7 +144,7 @@ const Markdown = memo(({ content = '', isEdited, showCursor, isLatestMessage }: return (
- +