Fix #772: Improve AI provider error messages with user-friendly parsing
Summary
This PR improves AI provider API error messages in RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER by implementing structured JSON parsing and providing user-friendly messages with contextual hints, addressing issue #772.
Note: As pointed out by @stohirov,
DEFAULT_RESPONSE_ERROR_HANDLERis not used by the OpenAI implementation (which uses its own SDK-level handling), but IS used by DeepSeek, Mistral AI (chat/embedding/OCR/moderation), MiniMax, Ollama, Stability AI, and ElevenLabs — all of which share the same{"error":{"message","type","param","code"}}error format. This PR has been generalized accordingly.
Problem
Previously, API errors were displayed in raw JSON format, making them difficult for users to understand:
Before:
401 - {"error":{"message":"Incorrect API key provided","type":"invalid_request_error","param":null,"code":"invalid_api_key"}}
Solution
Errors are now parsed and displayed in a user-friendly format:
After:
Authentication Error: Incorrect API key provided (Please check your API key)
Changes Made
New Classes
ApiErrorResponse: Provider-neutral POJO for parsing the common{"error":{...}}JSON structureApiErrorMessageImprover: Utility class with:- JSON parsing of structured error responses
- Status-based error prefixes (401 →
Authentication Error:, 429 →Rate Limit Error:, etc.) - Contextual hints based on error codes (
invalid_api_key,insufficient_quota,rate_limit_exceeded, etc.) - Graceful fallback to original
"<status> - <raw>"format when parsing fails
Both classes live in org.springframework.ai.retry (not a provider-specific subpackage).
Dependencies
- Added
jackson-databinddependency tospring-ai-retrymodule (replaces the heavierspring-boot-starter-json)
Related Issues
Resolves #772
@markpollack I would suggest changing the message improver to be general instead of using OpenAI prefix as OpenAI implementation does not use RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER. However, I found that it is still serving some other places: DeepSeek, Mistral AI (chat/embedding/OCR/moderation), MiniMax, Ollama, Stability AI, ElevenLabs (TTS/voices). Most of those (DeepSeek, Minstral, MiniMax) return errors in the same {"error":{"message","type","param","code"}} shape as OpenAI. So the parsing logic is still valuable — but the naming and the "OpenAI Authentication Error: ..." / OpenAI-specific URL hints would be misleading when the error actually came from DeepSeek. I can provide a sync and updated PR.
@markpollack One more thing to mention: the structural problem from #772 is already solved for OPenAI but not by Spring AI, by the OpenAI SDK itself. I would suggest two things for the issue #772:
- Update the title and description of the Issue to generalize the error messages
- Close #772 and open a new one for other models that still uses RetryUtils.
@markpollack I am closing the PR I've created as @snowykte0426 has already updated the code by himself.
However, I would suggest adding test cases and package-info (@snowykte0426). You can take a look at the PR I have created and use it as a template
@stohirov Thank you for the feedback — your points were valid. I verified and updated the implementation: renamed to ApiErrorMessageImprover/ApiErrorResponse, moved out of the openai subpackage, and removed OpenAI-specific prefixes and URL hints.
@snowykte0426 please add tests cases and package-info as well. you can check the PR I've created. Don't forget to run code check style.
@stohirov Thank you for the suggestion. I have added test cases and package-info.java, and ran both spring-javaformat:validate and checkstyle (via verify) — both pass with the project's custom ruleset.
@stohirov Referencing your PR, I moved the classes to the org.springframework.ai.retry.error subpackage — separating error-parsing concerns from the core retry mechanism felt like a cleaner design. I also kept the fallback message as "An error occurred with the AI provider API" rather than "An error occurred with the API", as the extra context makes it clearer to users that the error originated from an AI provider.
@snowykte0426 yeah, makes sense. Wish you luck bro)