Undefined with status 200

Hey,
I have read all the threads already related to this but nothing has helped me so far… In short, the api works meaby 60% of the time, and then it just doesn’t. The try catch is not triggered meaning the api returns 200 but with undefined as a response.

I know for sure if the values are not stringified, especifically the function responses, it will give me this kind of error. But in this case, I just don’t know what is causing it, full prompt doesn’t get over 800 tokens, sometimes it works, sometimes it doesn’t. I thought maybe there is a big usage at the time, but it happens at different times of the day too.
Last thing I tried is to stringify all the values passed in the AiPrompt, but still nothing.
ANY HELP is much appreciated!

Below is my code, I’m using nodeJS/NextJS and api v4
API:

export async function gptQuery(
  query: string,
  aiPrompt: string,
  functionRes: Array<{
    role: ChatCompletionRequestMessageRoleEnum;
    content: string;
    name: string;
  }> = [],
  model: string = "gpt-3.5-turbo-0613"
) {
  
  const functions = [
    {
      "name": "get_json_categories",
      "description":
        "Get a json structure based on one or multiple categories",
      "parameters": {
        "type": "object",
        "properties": {
          "industry_type":{
            "type": "string",
            "description":"The type of industry to get the categories from, e.g. 'Medicine'",
          },
          "categories": {
            "type": "array",
            "description":"An array of the categories to fetch, e.g. ['personalDemographics', 'medicalHistory']",
            "items": {
                "type": "string"
            }
          },
        },
        "required": ["industry_type","categories"],
      },
    },
    {
      "name": "populate_json_categories",
      "description":"populate a json structure with a given text",
      "parameters": {
        "type": "object",
        "properties": {
          "text":{
            "type": "string",
            "description":"The full unfiltered text to populate the json with",
          },
          "json": {
            "type": "string",
            "description":
              "The json to populate, e.g. '{\"name\":\"\"}'",
          },
        },
        "required": ["text","json"],
      },
    },
    {
      "name": "get_calendar_events",
      "description":
        "Get the calendar events for a given day, user, or time period.",
      "parameters": {
        "type": "object",
        "properties": {
          "event_date": {
            "type": "string",
            "format": "date",
            "description":
              "date to filter the calendar events in yyyy-mm-dd HH:MM:SS format (e.g. 2023-02-18 23:38:44)",
          },
        },
        "required": ["event_date"],
      },
    },
    {
      "name": "get_answer_from_docs",
      "description":
        "Large Collection of documents, answers any general question user asks. It can be filtered based on title, or time period",
      "parameters": {
        "type": "object",
        "properties": {
          "query": {
            "type": "string",
            "description": "The semantic query to search for, e.g. 'joe smith'",
          },
          "title": {
            "type": "string",
            "description": "The title to search for",
          },
          "date": {
            "format": "date",
            "description":
              "date to filter by in yyyy-mm-dd format (e.g. 2023-02-18)",
          },
        },
        "required": ["query"],
      },
    },
    {
      name: "get_company_info",
      description:
        "Get all the company information for a given company, including address, phone number, and detailed information of the staff",
      parameters: {
        type: "object",
        properties: {},
      },
    },
  ];

  const messages = [{
    role: "system",
    name: "system",
    content: aiPrompt,
  },
  {
    role: "user",
    name: "user",
    content: query,
  }]

  if (functionRes.length > 0) {
    messages.push(...functionRes)
  }

  try {
    const res = await openaiClient.chat.completions.create({
      model,
      messages: messages,
      temperature: 0,
      functions,
    });

    return res;
  } catch (error: any) {
    `ChatGPT was unable to find an answer for that! (Error: ${error.message})`;
  }
}

PROMPT:

export const assistantRole = `
You are a specialized AI chatbot called Andreas.
When faced with ambiguous questions, make informed assumptions derived strictly from the data within this interaction.
Discard any content or question not tied directly to the central topic of the inquiry.
Always follow the format of this example to generate your response: "Your next patient is Joana Nunez, she has been experiencing sleep problems and nausea
for the last two weeks. She is under prescribed medication."
Always follow the listed Gender when using pronouns.
Only use the functions you have been provided with.
`

    const aiPrompt = `
      ${assistantRole}\n 
      Current Session: ${JSON.stringify(userSession)}\n
      Chat Summary: ${JSON.stringify(chatSummary)}\n     
      Previous Chat:${JSON.stringify(previousChat)}\n
      Current date: ${JSON.stringify(currentDate)}.
      `;

Hi,

“it does not work” is not very helpful, do you have any error messages? A log file of the full API call and responses would be ideal.

Sorry about that!, the error message I am getting is:
(Error: The server had an error processing your request. Sorry about that! You can retry your request, or contact us through our help center at help.openai.com if you keep seeing this error. (Please include the request ID 4fe30da7ec8f0ac4e08585207310184d in your email.))

(I realized I forgot to add “return” in the catch… )
Logs:

{
  model: 'gpt-3.5-turbo-0613',
  messages: [
    {
      role: 'system',
      name: 'system',
      content: '\n' +
        '      \n' +
        'You are a specialized AI chatbot called Andreas.\n' +
        'When faced with ambiguous questions, make informed assumptions derived strictly from the data within this interaction.\n' +
        'Discard any content or question not tied directly to the central topic of the inquiry.\n' +
        'Always follow the format of this example to generate your response: "Your next patient is Joana Nunez, she has been experiencing sleep problems and nausea\n' +
        'for the last two weeks. She is under prescribed medication."\n' +
        'Always follow the listed Gender when using pronouns.\n' +
        'Only use the functions you have been provided with.\n' +
        '\n' +
        ' \n' +
        '      Chat Summary: "no summary"\n' +
        '     \n' +
        '      Previous Chat:""\n' +
        '\n' +
        '      Current date: "1:19:34 AM".\n' +
        '      '
    },
    { role: 'user', name: 'user', content: 'You know.' }
  ],
  temperature: 0,
  functions: [
    {
      name: 'get_json_categories',
      description: 'Get a json structure based on one or multiple categories',
      parameters: [Object]
    },
    {
      name: 'populate_json_categories',
      description: 'populate a json structure with a given text',
      parameters: [Object]
    },
    {
      name: 'get_calendar_events',
      description: 'Get the calendar events for a given day, user, or time period.',
      parameters: [Object]
    },
    {
      name: 'get_answer_from_docs',
      description: 'Large Collection of documents, answers any general question user asks. It can be filtered based on title, or time period',
      parameters: [Object]
    },
    {
      name: 'get_company_info',
      description: 'Get all the company information for a given company, including address, phone number, and detailed information of the staff',
      parameters: [Object]
    }
  ]
}

What happens if you remove the name: "system" line?

It fixes the issue and the errors stop! Thank you !!!

1 Like

Hey - we faced a similar issue today. Why would this suggestion help?

Except in our case, we didn’t have name: system, but we got undefined in the response after a long wait time. We’re also using nodejs.

Using the name roll like that causes issues, the Model is trained to expect system messages in a specific format, including it in the name is unexpected.

There can be minor outages from time to time and empty return messages. For any remote API interaction you should have a graceful error handling system to retry on issues like this.

One more thing I found that I constantly kept forgetting is to stringify all the contents you want the AI to have as resources other than the messages. Like so:

const systemPrompt: “Do this and that based on the below list:
Available List: ${someList}”

For this to work, you need to stringify the variable, otherwise it will not read it. Basically, if you console.log(systemPrompt) and you see [object][object]. Every time the AI fails to read that information. You need to stringify it for it to use it.

This sounds simple but in my case I’m constantly switching between python and node projects. You don’t have to do this in python

1 Like