Assistant api POST acting like GET?

Sorry if this has been asked somewhere, but I’ve been searching for an answer and haven’t found anything. I’m struggling to send assistant messages via the API. I’m following the documentation (for some reason it won’t let me post a link) but it’s not working. A summary of my problem:

  1. First, I have an assistant id and a thread id – no problems creating those.

  2. I try to create a message like this:
    (Note: some values are replaced with [VARIABLE] for security/privacy reasons; spaces in the URL are only there because this site gets mad when I try to post a URL, they are not there in the actual code).

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

var options = {
“method”: “post”,
“headers”: [HEADERS],
“payload” : { “role” : “user”,
“content” : [USER_PROMPT]}

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

  1. Instead of creating and returning the new message, it gives me back an empty list of messages (this is the exact same response I receive when using “get” instead of “post”). As far as I can tell, no message is added to the thread.

Any insight or advice?

Any help? I’m at my wits end here. Always getting an empty message list when using “post” instead of getting the message I’m trying to create.

I would first check the request using CURL and see that the output is correct with POST, use -v to get verbose output. Feed it the same HEADERS. API_KEY, thread_id etc…

Have you logged the HEADERS and USER_PROMPT and options to inspect what it is sending?

1 Like

Thanks for the advice! I tried using CURL with the same headers, key, etc. and received the expected result (a message object!).

I have double and triple checked the variable contents and I am certain they are the exact same as what I just used with CURL.

I guess now the goal is to determine what might make the UrlFetchApp work differently than CURL, but literally only when creating messages?

Any further suggestions are much appreciated!

EDIT: Maybe I should add that I’m writing this in Google Apps Script, which is why I’m using UrlFetchApp.

Maybe how the headers / Bearer authentication is sent?

It can be a bit tricky… Sounds like we’re narrowing down, tho! :wink:

Okay, I figured it out! UrlFetchApp normally selects the “Content-Type” automatically; I’ve been letting it do that for most calls because specifying it explicitly was often breaking things. But it seems that in this case, not specifying was breaking it!

I simply added “Content-Type”:“application/json” to the headers and it seems to be working now. (Not sure what UrlFetchApp was using for the content type but evidently it was the wrong thing).

Thanks guys, your replies got me pointed in the right direction!

1 Like

Good to hear! Sorry we didn’t catch that, but we did narrow it down!

Hope you stick around. We’ve got a great AI Dev community here, and we’d love you to be part of it.

There’s perks if you reach Trust Level 3 (by answering questions, being helpful, etc.)

1 Like

Thanks! First-time asker but long-time lurker. I’ll be around. :slight_smile:

1 Like