Sorry, something went wrong. -- assistants API

I was using the assistants API and everything was working great. Didn’t change anything and now all the sudden I have a “failure” and “LastError(code=‘server_error’, message=‘Sorry, something went wrong.’)” Again this was working fine for like three days then all the sudden won’t work now. Why?

This is my code:

def wait_on_run(run, thread):
    while run.status == "queued" or run.status == "in_progress":
        run = client.beta.threads.runs.retrieve(
            thread_id=thread,
            run_id=run.id,
        )
        time.sleep(0.5)
    print(run.status)
    return run

def pretty_print(messages):
    output = "# Messages\n"
    for m in messages:
        role = m.role
        text = ''
        if m.content:
            for content_block in m.content:
                if hasattr(content_block, 'text') and hasattr(content_block.text, 'value'):
                    text += content_block.text.value + ' '
        else:
            text = 'No content available'
        output += f"{role}: {text.strip()}\n"
    output += "\n"
    print(output)
    return output

def get_latest_message(thread):
    messages_page = client.beta.threads.messages.list(
        thread_id=thread,
        order="desc",  # Get the most recent messages first
        limit=1        # Get the last two messages (user and assistant)
    )
    return messages_page.data  # Returns a list of Message objects

# Define the paths to your two image files
image_file_path1 = [yadda yadda]
image_file_path2 = [yadda yadda]

# Upload the first image
file1 = client.files.create(
  file=open(image_file_path1, "rb"),
  purpose="vision"
)

# Upload the second image
file2 = client.files.create(
  file=open(image_file_path2, "rb"),
  purpose="vision"
)

# Step 2: add a user message to the thread
# Add a user message to the thread with both images
message = client.beta.threads.messages.create(
    thread_id="thread_4DsgB9g8iF56tjCGTPMEKVFX",
    role="user",
    content=[
        {
          "type": "text",
          "text": "[big complicated prompt about analyzing images]."
        },
        {
          "type": "image_file",
          "image_file": {"file_id": file1.id}
        },
        {
          "type": "image_file",
          "image_file": {"file_id": file2.id}
        }
    ]
)

time.sleep(1)

thread = "thread_4DsgB9g8iF56tjCGTPMEKVFX"

# Create a new run
run = client.beta.threads.runs.create(
    thread_id=thread,
    assistant_id="asst_sFp7klqycWhhPTlILceYzVfd",
    model="gpt-4o"  # Use "gpt-4" or another model you have access to
)

run = wait_on_run(run, thread)

# Get the latest messages
messages = get_latest_message(thread)

# Pretty print the messages
output = pretty_print(messages)

# Error printing
error_details = run.last_error
print(error_details)

# Write the result to the result.txt file
result_path = [yadda yadda]
try:
    with open(result_path, "w") as f:
        f.write(output)
    print(f"Result written to {result_path}")
except Exception as e:
    print(f"Failed to write result: {e}")```
2 Likes

o1 says to do this, what do we think?

  • Check and Update API Key Permissions:
  • Generate a New API Key with Full Permissions:
    • Log in to your OpenAI dashboard.
    • Navigate to the API keys section.
    • Create a new API key and ensure it has all the necessary permissions, including thread write, assistant read, and any other relevant permissions.
  • Update Your Code to Use the New API Key:
    • Replace the old API key in your code with the new one.
  • Verify Assistant and Thread IDs:
  • Assistant ID:
    • Ensure that the assistant_id (asst_sFp7klqycWhhPTlILceYzVfd) is correct and that you have access to it.
  • Thread ID:
    • Check that the thread_id (thread_4DsgB9g8iF56tjCGTPMEKVFX) is valid and hasn’t expired or been deleted.
  • Confirm Model Availability:
  • Model Name:
    • Verify that the model gpt-4o is available and that your account has access to it. If gpt-4o is not available, consider using gpt-4 or another model you have access to.
  • Update Model Parameter:
    • If necessary, update the model parameter in your client.beta.threads.runs.create() call.
  • Inspect Uploaded Files:
  • Ensure Successful Uploads:
    • Confirm that the files are being uploaded successfully and that file1.id and file2.id are valid.
    • You can print the file IDs to verify them:

python

Copy code

print(f"File1 ID: {file1.id}")
print(f"File2 ID: {file2.id}")
  • Check File Content:
    • Make sure the images comply with OpenAI’s content policies to avoid moderation issues.
  • Handle Potential Content Moderation:
  • Review Images for Compliance:
    • Ensure that the images do not contain disallowed content.
  • Modify Prompt if Necessary:
    • Adjust your prompt to comply with OpenAI’s usage policies.
  • Add Error Handling and Logging:
  • Capture Detailed Error Information:
    • Modify your wait_on_run function to print or log detailed error messages:

python

Copy code

if run.status == "failed":
    print(f"Run failed with error: {run.last_error}")
  • Enable Debugging Information:
    • If the API client library supports it, enable verbose logging to get more insights.
  • Test with a Simplified Version:
  • Remove Images Temporarily:
    • Try running your code without uploading images to see if the issue persists.
  • Use a Simple Prompt:
    • Use a basic prompt to determine if the error is related to the prompt content.
  • Check for API Changes or Announcements:
  • Review API Documentation:
    • Look for any recent changes in the API that might affect your code.
  • Monitor OpenAI’s Announcements:
    • Check if there are any service outages or maintenance activities.

I now also have this problem, it’s terribly frustrating.

1 Like

I wasn’t able to do anything on that thread. I’ve created a new thread, and can make conversation on it. Why did my old thread die? I’m back up and running on a different thread, but I am confused…

Hi there! I hope you’re doing well. I’m still encountering the same error and was wondering if there have been any updates on this issue. We’re also facing a deadline to migrate to the v2 API in less than two months. Could you please advise on how we should proceed with the migration given these ongoing errors? Your help would be greatly appreciated. Thank you!

The only thing I found that worked was switching off the thread. I’m guessing maybe I maxed out the memory on the thread or something? So if you get an unexplainable error, you could try making a new thread and hopping to that. I was also working on a crazy deadline, and had to come up with that as a kludge…

I’m encountering the same issue. In my case, it occurs when the thread already contains some messages and I request the assistant to perform function calls.

However, if the thread is new and I make a function call request right away, everything works without any issues.

Is there anything we can do programmatically to address this?

I suspect the root cause of the “Sorry, something went wrong” error is related to using image uploading with the “vision” purpose in the thread. Has anyone experienced this issue or found a workaround for it? It’s blocking essential product functions on my end. Any help would be appreciated!

I had a similar problem. Realized the tokens on the run was set too low.