ChatGPT API responding with empty object

Hi, I’m getting empty responses from OpenAIs chat completions API.

Heres my code:

import { MongoClient } from "mongodb";
import fetch from "node-fetch";
import { nanoid } from "nanoid";

// Replace the uri string with your MongoDB deployment's connection string.
const uri = process.env.MONGODB_URI;

const client = new MongoClient(uri);

export default async function run(req, res) {
  try {
    // Get the database and collection on which to run the operation
    const database = client.db("users");
    const gptKeys = database.collection("gptKeys");

    // Query for a movie that has the title 'The Room'
    const query = { userid: req.body.userid };

    // Execute query
    const key = await gptKeys.findOne(query);

    const body = {
        model: "gpt-3.5-turbo",
        messages: [
            role: "system",
            content: "create a blog article about" + req.body.topic

    fetch("", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + key.key
        body: JSON.stringify(body)
    .then(response => response.json())
    .then(async data => {

            await client.connect();
            const database = client.db("users");
            const users = database.collection("users");
            const result = await users.insertOne({ _id: nanoid(), response: data.completions.choices[0].message.content, userid: req.body.userid, date: new Date() });
            console.log(`New user created: ${result}`);
            res.status(201).json({ result: result });
    .catch((error) => {
        console.error('Error:', error);
        res.status(400).json({ error: error });
    // Print the document returned
  } finally {
    await client.close();

Heres the response:

  id: 'chatcmpl-98tash2sZiegFqgI0JxhOa6zqr0xV',
  object: 'chat.completion',
  created: 1711907354,
  model: 'gpt-3.5-turbo-0125',
  choices: [
      index: 0,
      message: [Object],
      logprobs: null,
      finish_reason: 'stop'
  usage: { prompt_tokens: 22, completion_tokens: 586, total_tokens: 608 },
  system_fingerprint: 'fp_3bc1b5746c'

I cant seem to figure out why the respneded message is an empty object. I bought the credits I need btw and don’t want to run through all of them just trying to figure this out.

Is the message actually empty? From what you’re showing it seems like it is not. You need to look for message content which is in the message object including role. Best to read the docs
Also it looks like you are having an LLM write code for you, so you might want to learn how to code yourself. Or at least ask AI to explain how to access the message object using node/javascript.

1 Like
1 Like

hi, you seem to be correct. i got working after changing code around.

Btw used github copilot when writing the code, so that’s probably why it looks like a LLM wrote the code as I have 5 years of experience writing javascript and 5 months of experience in NextJS.