Skip to content

Conversation

@snowykte0426
Copy link

@snowykte0426 snowykte0426 commented Jul 21, 2025

Summary

This PR improves OpenAI API error messages by implementing structured JSON parsing and providing user-friendly error messages with contextual hints, addressing issue #772.

Problem

Previously, OpenAI API errors were displayed in raw JSON format, making them difficult for users to understand:

Before:

401 - {"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"}} 

Solution

The error messages are now parsed and displayed in a user-friendly format with helpful hints:

After:

OpenAI Authentication Error: Incorrect API key provided: sk-proj-*********************************************F2yw. You can find your API key at https://platform.openai.com/account/api-keys. (Please check your API key at https://platform.openai.com/account/api-keys) 

Changes Made

New Classes

  • OpenAiErrorResponse: POJO for parsing OpenAI JSON error response structure
  • OpenAiErrorMessageImprover: Utility class for improving error messages with:
    • JSON parsing of OpenAI error responses
    • Status-based error prefixes (401 → "Authentication Error", 429 → "Rate Limit Error", etc.)
    • Contextual hints based on error codes
    • Graceful fallback to original format when parsing fails

Dependencies

  • Added spring-boot-starter-json dependency to spring-ai-retry module for Jackson JSON processing

Error Message Improvements

  • Authentication errors (401): Direct users to API key management
  • Rate limit errors (429): Provide guidance on quota and timing
  • Model errors: Help with model name verification
  • Context length errors: Suggest input reduction

Testing

  • Unit tests: Comprehensive test coverage for all error scenarios
  • Integration tests: Verified with actual OpenAI API error responses
  • Backward compatibility: Maintains original behavior for non-JSON responses
  • Format validation: All code passes Spring Java Format validation

Related Issues

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

…n dependency - Add spring-boot-starter-json dependency to spring-ai-retry module - Create OpenAiErrorResponse class for parsing OpenAI JSON error responses - Create OpenAiErrorMessageImprover utility for user-friendly error messages - Add status-based prefixes and helpful hints for common error codes - Maintain backward compatibility with graceful fallback Resolves: spring-projects#772 Signed-off-by: snowykte0426 <snowykte0426@naver.com>
@snowykte0426 snowykte0426 marked this pull request as ready for review July 21, 2025 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2 participants