spring-projects / spring-projects/spring-ai

OpenAI - improve error messages when there is a connection error

Open
#772 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

follow up next priorities openai
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the OpenAI request from OpenAiApi$ChatCompletion through Spring's RestClient and the SpringAiRetryAutoConfiguration warning shown in the report. Reproduce the invalid API-key response and determine where the response is converted into the generic RestClientException. Done means connection and authentication failures expose a clear, actionable message like the API error shown by curl.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.