Why I am getting object in the API response message?

I am using API to request some data from openAI

This is my url code
const response = await axios.post('https://api.openai.com/v1/chat/completions', data, { headers: headers });

This is my data, in header I am sending my apiKey

const data = {
        model: "gpt-3.5-turbo-1106",
        response_format: { type: "json_object" },
        messages: messages,
        tools: functions6,
        tool_choice: "auto"
    };

This is my response from openAPI

{
  id: 'chatcmpl-8wRDZcadfAAV2W3riPote5dvBBK',
  object: 'chat.completion',
  created: 17089383341,
  model: 'gpt-3.5-turbo-1106',
  choices: [
    {
      index: 0,
      message: [Object],
      logprobs: null,
      finish_reason: 'tool_calls'
    }
  ],
  usage: { prompt_tokens: 337, completion_tokens: 297, total_tokens: 634 },
  system_fingerprint: 'fp_406bAFe318f3'
}

Why I am getting the response message: [Object],

It should be a JSON text but it gives me a Object
I am trying to parse in this way
if (response.data && response.data.choices && response.data.choices[0] && response.data.choices[0].message) { }

and it gives an unknown error

The message is going to be an object as can be seen in the following sample:

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-3.5-turbo-0613",
  "system_fingerprint": "fp_44709d6fcb",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "logprobs": null,
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

It’s the content property that will be be a stringified json object.

So you’ll have to access it using:

message = response.data.choices[0].message;
message_json = JSON.parse(message.content);

HTH

3 Likes