API Calls to v1/threads/{thread_id}/messages fails with [file] and [file_ids]

Hello everyone,

When I make a call the the API

https://api.openai.com/v1/threads/{thread_id}/messages

and add either

    $data['file'] = [$file_id];

or
$data[‘file_ids’] = [‘file’ => $file_id];

I get the following error:

[Array
(
[error] => Array
(
[message] => 2 validation errors for Request
body → file_ids
value is not a valid list (type=type_error.list)
body → file
extra fields not permitted (type=value_error.extra)
[type] => invalid_request_error
[param] =>
[code] =>
)

)

When I test the assistant in the playground you can see that it works as expected.

It works.

I can see the file on the platform after uploading it, so I can confirm the file id.

I can see that the name is the same as what is loaded onto the platform in my error logs.

About two weeks ago (mid January 2024) this was working. Now it seems to no longer work.

Any help would be appreciate.

API #file #thread assistant help-needed

Welcome to the forum!

If you share a more complete code snippet it might be easier to help!

Hello jlvahulst,

Below if the function I have. This worked a while back but recently stopped working.

The assistant works on the platform as expected (see prior screen shots). And this code was fine until recently.

Thoughts?

Respectfully,

Stephen

function addAMessage($thread_id, $prompt, $context, $api_key, $file_id = null) {

// Set the URL
$url = get_threads_api_url() . '/' . $thread_id . '/messages';

$headers = [
    'Content-Type: application/json',
    'OpenAI-Beta: assistants=v1',
    'Authorization: Bearer ' . $api_key
];

// Set up the data payload
$data = [
    'role' => 'user',
    'content' => $prompt,
];

// Add the file reference if file_id is provided
if (!empty($file_id)) {
    $data['file'] = [$file_id];
}

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

// Execute cURL session
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    return null;
}

// Close cURL session
curl_close($ch);

// Return the API response
return json_decode($response, true);

}

@stepheninsrq
Are you able to solve this issue, Am also getting the same issue when am trying to send the file_ids in /message API call

Since this topic concluded 1/4 of a year ago, you must now use the attachments method, assigning a file id to code interpreter, or assigning a ID to vector store where you have placed documents for file_search.

https://platform.openai.com/docs/api-reference/messages/createMessage#messages-createmessage-attachments

1 Like

I have a similar issue after tried to migrate to v2, so I use the attachments method but this error persist : {“error”:“‘Assistants’ object has no attribute ‘files’”}
If any one can help.

Since it is so confusing, here’s a practical example with all types of uploaded files you can put in a message, and the no-library method to run it.

import os, json, urllib3

apikey = os.environ.get("OPENAI_API_KEY")
headers = {"OpenAI-Beta": "assistants=v2",
           "Authorization": f"Bearer {apikey}"
           }

thread_id = "thread_zWknKmTcXoxcxcNgNEU0LGrL"
threads_url = f"https://api.openai.com/v1/threads/{thread_id}/messages"
new_user_message = {  # this message demonstrates all files you might add
  "role": "user",
  "content": [
    {  # text the user has input
      "type": "text",
      "text": ("Say OK if you see the picture. "  # string continues...
                  "List available python tool mount point files. "
                  "List file names that you can search with myfiles_browser."
               ),
    },
    {  # vision for image uploaded to storage, purpose "vision"
      "type": "image_file",
      "image_file": {
        "file_id": "file-y0dKBYzE57mXbYGaLc2MdmdP",
        "detail": "low"
      }
    }
  ],
  "attachments": [
    {  # python file attachment from storage, purpose "assistants"
      "file_id": "file-ESl8xolqhAPS7EzoS9eUrKHU",
      "tools": [{"type": "code_interpreter"}]
    },
    {  # add file to thread's vector store for search, purpose "assistants"
      "file_id": "file-hE850BH10qGiVu3ZlXea8omS",
      "tools": [{"type": "file_search"}]
    }
  ],
  #  add information for your own use
  "metadata": {"customer_id": "cust-3353"}
}
try:
    http = urllib3.PoolManager()
    encoded_body = json.dumps(new_user_message).encode('utf-8')
    response = http.request('POST', threads_url,
                            headers=headers,
                            body=encoded_body
                            )
    response = json.loads(response.data.decode('utf-8'))
    print(json.dumps(response, indent=3))
    message_id = response['id']
except Exception as e:
    print(e)
    raise

(the failure of file_search is the AI not knowing what is behind a search)

You must use your own thread ID and file IDs with the correct purpose.

Your actual new user message function must adapt to any number of images, code interpreter files, or file search files that could be added on user input.

I hope that illustration helps.

Thank’s Jay I modify accordingly but unfortunately my error persist.

Okay I moved forward it’s pretty long to explain but I recommend these have a similar error to go back to the documentation : https://platform.openai.com/docs/assistants/how-it-works