Skip to content

Commit 9945264

Browse files
fix: Enhance chart response handling to manage empty or undefined answers
1 parent b6d8d60 commit 9945264

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/App/src/components/Chat/Chat.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,16 @@ const Chat: React.FC<ChatProps> = ({
568568
chartResponse = parsedChartResponse?.choices[0]?.messages[0]?.content;
569569
}
570570

571-
if (typeof chartResponse === 'object' && chartResponse?.answer) {
572-
chartResponse = chartResponse.answer;
571+
if (typeof chartResponse === 'object' && 'answer' in chartResponse) {
572+
if (
573+
chartResponse.answer === "" ||
574+
chartResponse.answer === undefined ||
575+
(typeof chartResponse.answer === "object" && Object.keys(chartResponse.answer).length === 0)
576+
) {
577+
chartResponse = "Chart can't be generated, please try again.";
578+
} else {
579+
chartResponse = chartResponse.answer;
580+
}
573581
}
574582

575583
if (
@@ -619,6 +627,16 @@ const Chat: React.FC<ChatProps> = ({
619627
parsedChartResponse?.error ||
620628
parsedChartResponse?.choices[0]?.messages[0]?.content
621629
) {
630+
let content = parsedChartResponse?.choices[0]?.messages[0]?.content;
631+
let displayContent = content;
632+
try {
633+
const parsed = typeof content === "string" ? JSON.parse(content) : content;
634+
if (parsed && typeof parsed === "object" && "answer" in parsed) {
635+
displayContent = parsed.answer;
636+
}
637+
} catch {
638+
displayContent = content;
639+
}
622640
const errorMsg =
623641
parsedChartResponse?.error ||
624642
parsedChartResponse?.choices[0]?.messages[0]?.content

0 commit comments

Comments
 (0)