Am I Understanding The Process For Receiving AI Reply from Assistants API Correctly?

Hey there, I’m setting up the Assistant API to work in my custom app. I just wanted to double-check I’m doing everything in the right order based off of the cURL documentation: https://platform.openai.com/docs/api-reference/assistants

First, I set up the initial code:

curl https://api.openai.com/v1/threads/runs \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "OpenAI-Beta: assistants=v2" \
  -d '{
      "assistant_id": "asst_abc123",
      "thread": {
        "messages": [
          {"role": "user", "content": "Explain deep learning to a 5 year old."}
        ]
      }
    }'

I’m pretty certain this is all working because I do get the initial response with all the id information, and my real-time user input system was working with the regular version of ChatGPT before I decided to switch to Assistants API as shown in the screenshot below:

Next, I start by creating the thread and run (following the section “Create thread and run”)

Then, I create message by extracting the thread and run id and putting them together into new url like here (following the “Create message section”)

Next, I retrieve the run again using the id strings to create the final url (following the “Retrieve run” section)

But, instead of getting the AI’s response to the user’s input, it gives me this error repeatedly:
https://api.openai.com/v1/threads/invalid_value/runs/messageInvalid'thread_id': 'invalid_value. Expected an ID that begins with 'thread'.
But the link I generated does have the thread id just like how the documentation shows it to be.

Am I doing this process in the wrong order, or am I missing some steps?

Here’s a breakdown of the API call flow you’ll need when using CURL directly with OpenAI’s Assistants API, especially when handling file uploads and vector stores, optional, but we’ll start with that first:

  1. Upload Files

    • Endpoint: POST https://api.openai.com/v1/files
    • Purpose: Upload your files with "purpose": "assistants".
    • Polling: Not required here; the file upload returns immediately with a file ID.
  2. Create a Vector Store

    • Endpoint: POST https://api.openai.com/v1/vector_stores
    • Purpose: Create a datastore (vector store) referencing the uploaded file IDs.
    • Polling: Required. After creation, repeatedly call:
      • GET https://api.openai.com/v1/vector_stores/{vector_store_id}
      • Check the "status" field until it becomes "completed".
  3. Create an Assistant

    • Endpoint: POST https://api.openai.com/v1/assistants
    • Purpose: Create an assistant configured to use the vector store (via "tool_resources" and "tools" parameters).
    • Polling: Not required; returns immediately with assistant ID.
  4. Create a Thread and Add Messages

    • Endpoint: POST https://api.openai.com/v1/threads
    • Purpose: Create a conversation thread and optionally add initial user messages.
    • Polling: Not required; returns immediately with thread ID.
  5. Create a Run (Assistant Response)

    • Endpoint: POST https://api.openai.com/v1/threads/{thread_id}/runs
    • Purpose: Trigger the assistant to process the thread messages.
    • Polling: Required. Repeatedly call:
      • GET https://api.openai.com/v1/threads/{thread_id}/runs/{run_id}
      • Check "status" until it becomes "completed".
  6. Retrieve Assistant’s Response

    • Endpoint: GET https://api.openai.com/v1/threads/{thread_id}/messages
    • Purpose: Fetch the assistant’s generated messages after the run completes.
    • Polling: Not required; returns immediately.

In short, you’ll need polling loops at two main points:

  • After creating the vector store (step 2), waiting for it to finish processing files.
  • After creating a run (step 5), waiting for the assistant to generate its response.

Hope this clarifies the flow! Now you can look up all those API parameters and connect things together!

(AI produced this from a complete implementation that is just as instructive in the procedural code)

2 Likes

Hi there,

Thanks for the help! I was able to figure everything out thanks in no small part to your help. I’m connecting Open AI Assistants to Unreal Engine using the Varest plugin and I’ve now got everything working!

1 Like

Sounds fun! Feel free to start a project thread in Community to share with everyone what you’re tinkering on.

Happy coding!