OpenAIError
openai.OpenAIError (Assistant not found: invalid ID)
Stack trace
openai.OpenAIError: Error 404: Assistant not found: invalid ID
at openai.api_requestor.APIRequestor._handle_error (openai/api_requestor.py:123)
at openai.api_requestor.APIRequestor.request (openai/api_requestor.py:85)
at openai.OpenAI.chat.completions.create (openai/api_resources/chat_completion.py:45)
at main.py:15 Why it happens
This error occurs because the assistant ID provided in the API call does not match any existing assistant resource. It can happen if the ID is mistyped, deleted, or never created. The OpenAI API validates the assistant ID before processing the request and returns a 404 if not found.
Detection
Check API responses for HTTP 404 errors with messages indicating 'Assistant not found'. Log the assistant ID used in requests to verify correctness before retrying.
Causes & fixes
The assistant ID string is mistyped or malformed in the API call.
Verify and correct the assistant ID string to exactly match the ID shown in your OpenAI dashboard or creation response.
The assistant resource was deleted or never created in your OpenAI account.
Create a new assistant via the OpenAI platform or API and use the returned valid assistant ID.
Using an outdated or cached assistant ID that is no longer valid.
Refresh your stored assistant IDs from the OpenAI dashboard or API before making calls.
Code: broken vs fixed
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
# Using an invalid assistant ID triggers the error
response = client.chat.completions.create(
model="openai-assistant-invalid-id",
messages=[{"role": "user", "content": "Hello"}]
) # This line raises OpenAIError: Assistant not found
print(response) import os
from openai import OpenAI, OpenAIError
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
try:
response = client.chat.completions.create(
model="openai-assistant-valid-id", # Corrected assistant ID
messages=[{"role": "user", "content": "Hello"}]
)
print(response)
except OpenAIError as e:
print(f"OpenAI API error: {e}") # Graceful error handling Workaround
Catch the OpenAIError exception, check if the error message contains 'Assistant not found', then prompt the user to verify or update the assistant ID before retrying.
Prevention
Implement validation of assistant IDs by fetching current valid IDs from the OpenAI API or dashboard before use, and avoid hardcoding IDs that may become invalid.