Skip to content

Commit d4a4971

Browse files
PeterDaveHellojosStorer
authored andcommitted
Fix handling of empty choices array in Azure OpenAI API integration
This commit refines the error handling for the Azure OpenAI API integration to address the 'n.choices[0] is undefined' error by ensuring 'data.choices' and 'data.choices[0]' are properly checked before access. This update is necessary due to the behavior changes introduced in the API version update (#684), which can result in empty 'choices' arrays. Fix #698.
1 parent f177a33 commit d4a4971

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/services/apis/azure-openai-api.mjs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,18 @@ export async function generateAnswersWithAzureOpenaiApi(port, question, session)
4747
console.debug('json error', error)
4848
return
4949
}
50-
if ('content' in data.choices[0].delta) {
50+
if (
51+
data.choices &&
52+
data.choices.length > 0 &&
53+
data.choices[0] &&
54+
data.choices[0].delta &&
55+
'content' in data.choices[0].delta
56+
) {
5157
answer += data.choices[0].delta.content
5258
port.postMessage({ answer: answer, done: false, session: null })
5359
}
54-
if (data.choices[0]?.finish_reason) {
60+
61+
if (data.choices && data.choices.length > 0 && data.choices[0]?.finish_reason) {
5562
pushRecord(session, question, answer)
5663
console.debug('conversation history', { content: session.conversationRecords })
5764
port.postMessage({ answer: null, done: true, session: session })

0 commit comments

Comments
 (0)