OpenAI API "messages" endpoint: "POST" acting like "GET" and returning a message list instead of creating a message

92 Views Asked by At

I'm struggling with the OpenAI API (esp. for creating messages on threads) and haven't been able to find any solution to this problem anywhere. I'm following the documentation here.

Basically, when I send the call to create a message (using "post"), it behaves like "get" and returns an empty message list rather than creating a message. Since the thread I'm trying to attach the message to is empty (i.e. no messages yet), the response I'm getting from the API is what I would expect to see (and do see) when using "get" instead of "post".

I've had no problem using the API calls to create threads or assistants. It's only when creating messages that I'm experiencing this.

GOAL: Create a new message on an existing, but empty, thread.

Code (written in Google's apps script version of js):

var endpoint = “https://api.openai.com/v1/threads/THREAD_ID/messages/”;

var options = {
    “method”: “post”,
    “headers”: HEADERS,      //contains API key
    “payload” : { “role” : “user”,
        “content” : USER_PROMPT}
};

var response = UrlFetchApp.fetch(endpoint, options);

(Note: some values in the code are replaced with VARIABLE_NAME for security/privacy reasons).

Expected Response: An OpenAI message object such as:

{
  "id": MESSAGE_ID,
  "object": "thread.message",
  "created_at": DATE_TIME
  "thread_id": THREAD_ID,
  "role": "user",
  "content": [
    {
      "type": "text",
      "text": {
        "value": USER_PROMPT,
        "annotations": []
      }
    }
  ],
  "file_ids": [],
  "assistant_id": ASSIST_ID,
  "metadata": {}
}

Actual Response: An empty list of messages:

{
  "object": "list",
  "data": [],
  "first_id": null,
  "last_id": null,
  "has_more": false
}

Can anyone offer any ideas as to why this is happening?! I'm losing my mind trying to figure this out, lol.

0

There are 0 best solutions below