Migration from v1 to v2 (Run creation requires a Thread with a user message or an assistant with instructions)

I have developed a PHP program that utilizes the OpenAI API v1 and it works flawlessly. The program employs REST API to interact with an assistant to which I have passed some files.

Currently, I am attempting to migrate from API v1 to API v2. Despite reading the migration guide, I am encountering difficulties. Specifically, I am receiving the following error:

“Run creation requires a Thread with a user message or an assistant with instructions.”

Here is the function I am using. Could someone please assist me in migrating it?

// Create a Thread
$threadResponse = callOpenAI(‘threads’, array(), $apiKey,‘POST’);

// Add a message to the thread
$data = [‘role’ => $role, ‘content’ => $question ,‘file_ids’ => $fileId];
$response=callOpenAI(‘threads/’ . $threadId . ‘/messages’, $data, $apiKey,‘POST’);

// Run th Thread
$data = [“assistant_id”=> $assistantId,“additional_instructions”=>null];
$runResponse = callOpenAI(‘threads/’ . $threadId . ‘/runs’, $data, $apiKey,‘POST’);
$runId=$runResponse[‘id’];

do {
$run = callOpenAI(‘threads/’ . $threadId . ‘/runs/’ . $runId, , $apiKey, ‘GET’);
$status = $run[‘status’]; // Assuming the response has a ‘status’ field
// echo $status;
if ($status !== ‘completed’)
{
sleep(5);
}
}
while ($status !== ‘completed’);

// Retrieve the Message !!!
$response=callOpenAI(‘threads/’ . $threadId . ‘/messages’, , $apiKey, ‘GET’);

Regards
Giuseppe

Thank you very much.

Before reading your code, I’d suggest you to retrieve your Thread before Running the thread. By looking at your error, seems like you are creating a message but not adding that to the Thread.

So, before the DoWhile in your code, debug the Thread and see if it has message attached to it.

Thanks a lot MrFriday, I debugged everything, again and again, and in the end I found that the error was, a bit like you said, in the fact that I couldn’t add the message in the thread, because the message wasn’t created correctly in as of now the fields_id is not allowed
I chaged
$data = [‘role’ => $role, ‘content’ => $question ,‘file_ids’ => $fileId];
in
$data = [‘role’ => $role, ‘content’ => $question ];
and it worked.
thank you verymuch
regards
Giuseppe

1 Like

Glad I could help, you can close this question by selecting the best answer so that it can help other new people.