Error with OpenAI API: OpenAI API response did not contain valid choices

I’m trying to retrieve the updated response from an input sent to openai chat completion via firebase.

The data is successfully retrieved from firebase, then sent to openai api successfully, but there is no valid response.

Any help would be appreciated.

Code:

const completionResponse = await openai.chat.completions.create({
      model: "gpt-3.5-turbo",
      messages: [
        {"role": "system", "content": "You are a helpful editing assistant. Only return the summary without additional information or feedback."},
        {"role": "user", "content": data.summary}
      ],
    });
    
    if (completionResponse.data && completionResponse.data.choices.length > 0) {
      return { editedPitch: completionResponse.data.choices[0].message.content.trim() };
    } else {
      throw new functions.https.HttpsError('internal', 'OpenAI API response did not contain valid choices.');
    }
  } catch (error) {
    console.error("Error with OpenAI API:", error.message, error.stack);
    throw new functions.https.HttpsError('internal', 'Unable to process the pitch at this time.');
  }

Welcome to the community!

Did you check the API docs? https://platform.openai.com/docs/api-reference/chat?lang=node.js

where did your .data come from? :thinking:

Thanks for your response!

The API docs is where I modeled my current code from, but am not sure if the error is coming from that part of the code or the .data part you pointed out. Good question.

The data.summary is coming from firebase in this code prior to the completion response. It looks like this:

const functions = require("firebase-functions");
const { OpenAI } = require("openai");

const openai = new OpenAI({
  apiKey: functions.config().openai.api_key,
});

exports.editSummary = functions.https.onCall(async (data, context) => {
  console.log('Received summary:', data.summary);
  if (!context.auth) {
    throw new functions.https.HttpsError('unauthenticated', 'The function must be called while authenticated.');
  }

I added a log that captures what is sent to openai and it seems correct, but still no luck. I’ve also tried asking ChatGPT for answers but it doesn’t know the current formatting from v3 to v4 referenced here:
https://github.com/openai/openai-node/discussions/217