AI assistant not responding with a response

I am using assistant api from the openai, it is working perfectly fine but sometimes it doesn’t respond me with anything and also doesn’t throw any error, so basically a user gets stucks and waits for a response which will never going to come back from the api.

This peculiar behavior occurs approximately 1 in 10 times, and I’m looking for insights or potential solutions from the community.

3 Likes

Hi,

When using a remote API of any kind it is best practice to issue the API call and then begin a timer to check if a reply has been received in an acceptable period of time, if not, the user should be informed. You may wish to show a progress bar or some other visual indicator of “progress” to prevent users thinking the application has crashed.

1 Like

That error rate is higher than I’ve seen, so I wonder if you are expecting to receive a response immediately instead of polling multiple times with a delay.

Can you share how you are calling the following command in your code, and what you are doing with the “status” value to help you debug?

run = client.beta.threads.runs.retrieve(thread_id=my_thread_id, run_id=my_run_id)

status = run.status

1 Like

Hi @shakirsnakescript , I have the same problem, in my last 13 calls it failed 6 times (four of them the same day, consecutively). I also noticed I am being charged for the prompt and instructions on these failed runs without completions. I tried to contact support but didn’t get far. Will reply back to the thread if I manage to get some insights into the problem.

1 Like

I’ve encountered an issue while attempting to connect to my Assistant using the API.

Despite following the documentation for endpoints and ensuring my API key and Assistant ID are correct, I receive an ‘Invalid URL’ error when trying to access chat completions. Could you please provide guidance on the correct endpoint to use or any additional steps I might be missing to establish a successful connection?

I’ve verified the Assistant ID is correct.

From my website, this error popped up: OpenAI Error: Invalid URL (POST /v1/assistants/{assistant_id number}/messages)

I’ve used Postman to validate the connection by keying in this URL: https://api.openai.com/v1/assistants/{assistant_id number}/chat/completions

and this error appeared:
{ “error”: { “message”: “Invalid URL (POST /v1/assistants/asst_3Rbld8O88IqbBM8rl9pCz5YM/chat/completions)”, “type”: “invalid_request_error”, “param”: null, “code”: null } }

Hi and welcome to the Developer Forum!

The URL you are using does not conform to the examples for Assistants API usage, where did you get that syntax from?

The example shows:

Assistant creation

curl "https://api.openai.com/v1/assistants" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
    "instructions": "You are a personal math tutor. Write and run code to answer math questions.",
    "name": "Math Tutor",
    "tools": [{"type": "code_interpreter"}],
    "model": "gpt-4"
  }'

Note that there is no assistants number in that call, I think you may be confusing that with the “thread” which does follow that structure

curl https://api.openai.com/v1/threads/thread_abc123/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: assistants=v1" \
  -d '{
      "role": "user",
      "content": "I need to solve the equation `3x + 11 = 14`. Can you help me?"
    }'

It would be worth you reviewing the examples on this page

That URL was me following ChatGPT’s advice to test my Assistant connection via Postman due to persistent ‘undefined’ response/message/error. This happens whenever I prompt anything for my Assistant to process in my website’s chat interface.

After a thorough review of documentation and insights from ChatGPT and Github Copilot, the challenge persisted until I tried Postman. However, encountering an error in Postman still led to uncertainties about; whether the problem was with the endpoint for Postman or the Assistant’s actual accessibility through Postman and my website.

I’m seeking your help to review my simple plugin’s main code to confirm it’s set up for proper communication and response retrieval from the OpenAI Assistant.

For the second day, I’ve been observing a similar issue where the task returns a status of in_progress for a few seconds, and then responses cease without returning any errors. The result may come back after a delay of 10 minutes, or sometimes not at all. Previously, everything was working stably; the problem seems to be on OpenAI’s side.

Please review the link I gave in the previous post which contains the curl commands required to use the assistants API, asking ChatGPT to create code about a system that it has no knowledge of will result in errors like this, unfortunately there are still aspects of code creation that require human input, you could try pasting the contents of that assistants guide page into ChatGPT as context and then asking for the correct code.

I’ve tried that, can you provide some URL with the correct endpoints for me to check on Postman if my assistant can be connected? I’ve tried both of the following and they failed;

  1. https://api.openai.com/v1/assistants/{assistant id number}/chat/completions
  2. https://api.openai.com/v1/assistants/{assistant id number}/messages

Can you have a look at the following code for the curl function I’ve used but didn’t get connected to Assistant:

function gpt_assistant_chat() {
// Check the nonce for security
check_ajax_referer(‘gpt_assistant_chat’, ‘nonce’);

// Get the user's message from the request
$prompt = sanitize_text_field($_POST['prompt']);

// Get the API key and GPT Assistant ID from the plugin's options
$options = get_option('gpt_assistant_options');
$api_key = $options['api_key'];
$gpt_assistant_id = isset($options['gpt_assistant_id']) ? $options['gpt_assistant_id'] : '';
// Log the GPT Assistant ID
error_log('GPT Assistant ID: ' . $gpt_assistant_id);


// Check if the GPT Assistant ID is valid
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/assistants/$gpt_assistant_id");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = ["Authorization: Bearer $api_key"];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
    wp_send_json_error(['error' => 'Error: ' . curl_error($ch)]);
    return;
}
curl_close($ch);
$response = json_decode($result, true);
if (isset($response['error'])) {
    wp_send_json_error(['error' => $response['error']['message']]);
    return;
}

// Set up the API request
$url = 'https://api.openai.com/v1/chat/completions';
#$url = 'https://api.openai.com/v1/engines/gpt-4-0125-preview/completions';
$headers = [
    'Authorization: Bearer ' . $api_key,
    'Content-Type: application/json'
];
$body = json_encode([
    'model' => $gpt_assistant_id,
    'messages' => [
        ['role' => 'user', 'content' => $prompt]
    ],
    'max_tokens' => 1000
]);

// Send the API request and get the response
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$result = curl_exec($ch);
curl_close($ch);

// Parse the API response
$response = json_decode($result, true);

// Log the entire API response
error_log('API response: ' . print_r($response, true));

// If there's an error, return it as a JSON object
if (isset($response['error'])) {
    wp_send_json_error(['error' => $response['error']['message']]);
}

// Return the model's response as a JSON object
wp_send_json_success(['message' => $response['choices'][0]['message']['content']]);

}