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);

}