openai.beta.chat.completions.stream(chatRequest).finalChatCompletion() throws missing finish_reason error for gpt-4o-audio-preview streaming. #1206
Open
Description
Confirm this is a Node library issue and not an underlying OpenAI API issue
- This is an issue with the Node library
Describe the bug
gpt-4o-audio-preview streamed final return chunk doesn't contain finish_reason
and the finalizeChatCompletion method cannot handle it properly
data: {"id":"chatcmpl-123456","object":"chat.completion.chunk","created":1730764995,"model":"gpt-4o-audio-preview-2024-10-01","system_fingerprint":"fp_6e2d124157","choices":[{"index":0,"delta":{"audio":{"transcript":"!"}},"finish_reason":null}]}
data: {"id":"chatcmpl-123456","object":"chat.completion.chunk","created":1730764995,"model":"gpt-4o-audio-preview-2024-10-01","system_fingerprint":"fp_6e2d124157","choices":[{"index":0,"delta":{"role":"assistant","content":null,"refusal":null,"audio":{"id":"audio_672960c3a3e48190aed70377a7d6c941","data":"CAAIAAkACgAPABEAEQARABMAEAANAAgAAwD9//v/+f/3//L/8v/w//L/8"}},"finish_reason":null}]}
data: {"id":"chatcmpl-123456","object":"chat.completion.chunk","created":1730764995,"model":"gpt-4o-audio-preview-2024-10-01","system_fingerprint":"fp_6e2d124157","choices":[{"index":0,"delta":{"audio":{"data":"lBNbDTYZdhycFrgTxg6NBT8BavhC85LyNu/j9Yv8DvqAAuA"}},"finish_reason":null}]}
data: {"id":"chatcmpl-123456","object":"chat.completion.chunk","created":1730764995,"model":"gpt-4o-audio-preview-2024-10-01","system_fingerprint":"fp_6e2d124157","choices":[{"index":0,"delta":{"audio":{"data":"J/6d/nsAkAE3/xv7U/jp9ur0bPRY+DH//v//v8IAAcAEgALABsAFAANAAsA//8IAP//8//y//T/7//3//v/+v8TABsADgAdACIAPAACAPn//f/j/zAAIQATADIAQgBbAAwA+f/z/wgA/v8YABkAOAAjANn/"}},"finish_reason":null}]}
data: {"id":"chatcmpl-123456","object":"chat.completion.chunk","created":1730764995,"model":"gpt-4o-audio-preview-2024-10-01","system_fingerprint":"fp_6e2d124157","choices":[{"index":0,"delta":{"audio":{"expires_at":1730768595}}}]}
See how the final only has an expires_at and doesn't have a finish_reason
To Reproduce
const chatRequest = {
messages: chatRequestMessageList,
model: "gpt-4o-audio-preview",
modalities: ["text", "audio"],
audio: { voice: "alloy", format: "pcm16" },
max_tokens: COMPLETION_RESPONSE_SETTING,
temperature: 0.7,
top_p: 1,
stream: true,
tools: request.body.tools,
tool_choice: request.body.toolChoice,
}
const stream = openai.beta.chat.completions.stream(chatRequest)
...
const finalChatCompletion = await stream.finalChatCompletion();
throw error
Error in the streaming of response: OpenAIError: missing finish_reason for choice... at finalizeChatCompletion ... at ChatCompletionStream._ChatCompletionStream_endRequest.....at ChatCompletionStream._createChatCompletion.... at process.processTicksAndRejections..... at async ChatCompletionStream._runChatCompletion....node_modules/openai/lib/AbstractChatCompletionRunner.js:135:16
Code snippets
No response
OS
macOS
Node version
node 18
Library version
4.73.1