How to retrieve error details? - AttributeError: 'Threads' object has no attribute 'errors'

I’m getting an error when invoking Open AI API. The error handler used to work but not anymore. I’m assumign the API must have changed. How do I retrieve error details?

client = OpenAI(api_key=OPENAI_API_KEY)

run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
match run.status:
case “failed”:
errors = client.beta.threads.errors.list(thread_id=thread_id)
print(errors)

AttributeError: ‘Threads’ object has no attribute ‘errors’

If you retrieve the run or run step object, it will have a “last error”.

last_error:
    type: object
    description: The last error associated with this run. Will be `null` if there are no errors.
    nullable: true
    properties:
        code:
            type: string
            description: One of `server_error`, `rate_limit_exceeded`, or `invalid_prompt`.
            enum:
                ["server_error", "rate_limit_exceeded", "invalid_prompt"]
        message:
            type: string

vector store also has a status in its return object (and the API spec has a bad description):

last_error:
    type: object
    description: The last error associated with this vector store file. Will be `null` if there are no errors.
    nullable: true
    properties:
        code:
            type: string
            description: One of `server_error` or `rate_limit_exceeded`.
            enum:
                [
                    "internal_error",
                    "file_not_found",
                    "parsing_error",
                    "unhandled_mime_type",
                ]

Here’s suggested use by AI that knows all about assistants besides this guidance I gave it:

To retrieve error details for an assistant’s run or thread using the updated OpenAI API, you should access the last_error attribute of the RunObject directly. Here’s how you can modify your code to check the run status and print error details if the run has failed:

from openai import OpenAI
client = OpenAI()

# Retrieve the run details
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id)

# Check the run status and print error details if it failed
if run.status == "failed":
    last_error = run.last_error
    if last_error:
        print(f"Error Code: {last_error.code}")
        print(f"Error Message: {last_error.message}")
    else:
        print("No error details available.")

Explanation:

  1. Retrieve the run details: Use client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run_id) to get the run details.
  2. Check the run status: Verify if the status attribute of the run object is "failed".
  3. Access the error details: If the run has failed, access the last_error attribute to get the error details (error code and message).
  4. Print the error details: Print the error code and message if they exist; otherwise, indicate that no error details are available.

This approach uses the last_error field defined in the API specifications for both RunObject and VectorStoreFile objects to retrieve the last error associated with a run.

A bit of hallucination, but you’ve got API reference links on the forum sidebar…

1 Like