Issues with PHP Integration for OpenAI Assistants v2 - "Missing required parameter: 'role'"

Hello OpenAI Community,

I’ve been working on integrating an assistant I created via the OpenAI platform into a PHP application. I’m using the new assistants v2 API but I keep encountering issues with missing parameters.

Specifically, I’m getting the following error:
"HTTP Error #:400 Response: { “error”: { “message”: “Missing required parameter: ‘role’.”

Here’s a simplified version of the PHP code I’m using:

<?php

// cURL function to send POST requests
function curlAPIPost($api_key, $url, $data = '') {
    $headers = [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key,
        'OpenAI-Beta: assistants=v2'  // Header required to access the Assistants v2 API
    ];

    $curl = curl_init($url);

    if ($data != '') {
        $json_data = json_encode($data);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);
    }

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($curl);
    $err = curl_error($curl);
    $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);

    if ($err) {
        echo "cURL Error: " . $err . "<br>";
        return false;
    } else if ($httpcode >= 400) {
        echo "HTTP Error #:" . $httpcode . " Response: " . $response . "<br>";
        return false;
    } else {
        return json_decode($response, true);
    }
}

// Function to create a thread
function createThread($api_key, $message) {
    $url = 'https://api.openai.com/v1/threads';
    $data = [
        'messages' => [
            ['role' => 'user', 'content' => $message]  // Ensuring the 'role' is correct
        ]
    ];
    return curlAPIPost($api_key, $url, $data);
}

// Function to add a message to the thread
function addMessageToThread($api_key, $thread_id, $message) {
    $url = "https://api.openai.com/v1/threads/$thread_id/messages";
    $data = [
        'role' => 'user',  // Ensuring the 'role' is present
        'content' => $message
    ];
    return curlAPIPost($api_key, $url, $data);
}

// Function to run the assistant
function runAssistant($api_key, $thread_id, $assistant_id) {
    $url = "https://api.openai.com/v1/threads/$thread_id/runs";
    $data = [
        'assistant_id' => $assistant_id
    ];
    return curlAPIPost($api_key, $url, $data);
}

// Function to retrieve the assistant's response
function getMessages($api_key, $thread_id) {
    $url = "https://api.openai.com/v1/threads/$thread_id/messages";
    return curlAPIPost($api_key, $url);
}

// Main code
$api_key = 'your-api-key';
$assistant_id = 'your-assistant-id';

// Creating a thread
$thread = createThread($api_key, "What is i9Tax?");
if ($thread) {
    echo "Thread created: " . $thread['id'] . "<br>";

    // Adding a message to the thread
    $message_response = addMessageToThread($api_key, $thread['id'], "What is i9Tax?");
    if ($message_response) {
        echo "Message added to the thread: " . $message_response['id'] . "<br>";

        // Running the assistant
        $run = runAssistant($api_key, $thread['id'], $assistant_id);
        if ($run) {
            echo "Assistant running: " . $run['id'] . "<br>";

            // Wait for the assistant's response
            $max_attempts = 10;
            $attempt = 0;
            $found_response = false;

            while ($attempt < $max_attempts && !$found_response) {
                // Wait 2 seconds before checking messages
                sleep(2);
                $messages = getMessages($api_key, $thread['id']);

                if ($messages && isset($messages['data'])) {
                    foreach ($messages['data'] as $message) {
                        if ($message['role'] === 'assistant') {
                            echo "Assistant's response: " . $message['content'][0]['text']['value'] . "<br>";
                            $found_response = true;
                            break;
                        }
                    }
                }

                $attempt++;
            }

            if (!$found_response) {
                echo "Error: Could not get the assistant's response within the expected time.<br>";
            }
        } else {
            echo "Error running the assistant.<br>";
        }
    } else {
        echo "Error adding the message to the thread.<br>";
    }
} else {
    echo "Error creating the thread.<br>";
}

?>

I’m making sure to include the required role parameter as documented in the OpenAI Assistant API documentation, but I still get the same error.

Has anyone faced a similar issue, or is there something I might be missing? Any help would be appreciated.

Thank you!