Skip to content

Error Handling

This guide includes an overview on error handling for the OpenAI Exceptions in the HAI API.

Error Handling for OpenAI Exceptions

HAI API uses standard HTTP status codes and JSONResponse to indicate the success or failure of a request. The JSON body includes an error_code and a detail field.

  1. The error_code field is a string that represents the type of error that occurred.
  2. The detail field is a string that provides more information about the error.

The following table lists the possible HTTP status codes, their meanings and the corresponding JSONResponse.

HTTP Status Code Meaning JSONResponse
400 Bad Request - Content Filter {"error_code": "content_filter_error","detail": "Could not parse ..."}
400 Bad Request {"error_code": "bad_request","detail": "OpenAI API request was invalid."}
400 Length Filter {"error_code": "length_finish_reason_error","detail": "OpenAI length filter finished with an error."}
401 Unauthorized {"error_code": "unauthorized","detail": "Authentication credentials."}
403 Permission Denied {"error_code": "permission_denied","detail": "OpenAI API request was denied permission."}
409 Conflict {"error_code": "conflict_error","detail": "OpenAI API request resulted in a conflict."}
422 Unprocessable Entity {"error_code": "unprocessable_entity","detail": "OpenAI API request was unprocessable."}
429 Too Many Requests {"error_code": "rate_limit_error","detail": "OpenAI API request rate limit exceeded."}
500 API Timeout {"error_code": "api_timeout_error","detail": "OpenAI API request timed out."}
500 API Connection Error {"error_code": "api_connection_error","detail": "OpenAI API request connection error."}
500 API Error {"error_code": "api_error","detail": "OpenAI API request resulted in an error."}

Example

{
    "error_code": "content_filter_error",
    "detail": "Could not parse response content as the request was rejected by the content filter"
}

Error Handling for streaming Responses

When an OpenAI Exception happens during the streaming of a response, the response will contain all streamed chunks up to the point of the error and chunk containing the details and finish reason of the streaming in the meta_data field.

Example

{
    "message_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "thread_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "created_at": "2024-11-15T17:43:37.443794",
    "delta": {
        "role": "assistant",
        "content": "",
        "sources": [],
        "meta_data": null
    }
}
{
    "message_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "thread_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "created_at": "2024-11-15T17:43:37.443991",
    "delta": {
        "role": "assistant",
        "content": "",
        "sources": [],
        "meta_data": null
    }
}
{
    "message_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "thread_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "created_at": "2024-11-15T17:43:37.444612",
    "delta": {
        "role": "assistant",
        "content": "Die",
        "sources": [],
        "meta_data": null
    }
}
{
    "message_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "thread_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "created_at": "2024-11-15T17:43:37.444993",
    "delta": {
        "role": "assistant",
        "content": " Künd",
        "sources": [],
        "meta_data": null
    }
}
{
    "message_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "thread_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "created_at": "2024-11-15T17:43:37.445959",
    "delta": {
        "role": "assistant",
        "content": "igungs",
        "sources": [],
        "meta_data": null
    }
}
{
    "message_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "thread_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "created_at": "2024-11-15T17:43:37.446459",
    "delta": {
        "role": "assistant",
        "content": "",
        "sources": [],
        "meta_data": {
            "type": "common_metadata",
            "finish_reason": "content_filter_error",
            "detail": "Could not parse response content as the request was rejected by the content filter."
        }
    }
}