Skip to content

OpenAI - improve error messages when there is a connection error #772

Open
@michaelisvy

Description

@michaelisvy

Expected Behavior

It would be great to have an explicit error message when the connection with OpenAI cannot be established.

Current Behavior
(Using Spring AI 0.8.1)
Messages are quite hard to understand when there is a connection error with the OpenAI API.
For instance, if there is a mistake in my API key, I get the following error:

2024-05-26T21:23:44.729+08:00  WARN 43707 --- [petclinic-prompt] [           main] o.s.a.a.r.SpringAiRetryAutoConfiguration : Retry error. Retry count:1

org.springframework.web.client.RestClientException: Error while extracting response for type [org.springframework.ai.openai.api.OpenAiApi$ChatCompletion] and content type [application/json;charset=utf-8]
	at org.springframework.web.client.DefaultRestClient.readWithMessageConverters(DefaultRestClient.java:240) ~[spring-web-6.1.8.jar:6.1.8]
	at org.springframework.web.client.DefaultRestClient$DefaultResponseSpec.readBody(DefaultRestClient.java:688) ~[spring-web-6.1.8.jar:6.1.8]

Using curl, we are getting an error message that is easier to understand, so I thought it might be possible to improve it in Spring AI:

Calling OpenAI
{
    "error": {
        "message": "Incorrect API key provided: sk-proj-*********************************************F2yw. You can find your API key at https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": "invalid_api_key"
    }
}

Thanks a lot for the great work!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions