Skip to content

Surface provider error responses in OpenAiChatModel - #6908

Open
harrisleesh wants to merge 1 commit into
spring-projects:mainfrom
harrisleesh:gh-6900-openrouter-error
Open

Surface provider error responses in OpenAiChatModel#6908
harrisleesh wants to merge 1 commit into
spring-projects:mainfrom
harrisleesh:gh-6900-openrouter-error

Conversation

@harrisleesh

@harrisleesh harrisleesh commented Sep 3, 2026

Copy link
Copy Markdown

Motivation

OpenAI API compatible providers such as OpenRouter report upstream failures with an HTTP 200 response whose body carries an error object instead of choices (see the OpenRouter error handling docs). OpenAiChatModel.internalCall accessed the missing choices field directly, so the call failed with the SDK's generic OpenAIInvalidDataException: \choices` is not set`, hiding the provider's error message and status code and leaving applications nothing to build error handling on.

Changes

  • Before reading choices, detect a response with missing choices and an error additional property, and throw the openai-java service exception corresponding to the reported error code: RateLimitException for 429, InternalServerException for 5xx, BadRequestException/UnauthorizedException/etc. for the matching 4xx codes, and UnexpectedStatusCodeException otherwise. The exception carries the provider's error message, so rate-limiting and upstream outages can be handled distinctly, as the issue requested ("map to corresponding exceptions from openai-java-core").
  • A response with missing choices but no error payload keeps the existing behavior.
  • Added tests covering a 502 upstream error, a 429 rate limit, an error without a code, and the two non-error shapes.

Fixes #6900

OpenAI API compatible providers such as OpenRouter report upstream
failures with an HTTP 200 response whose body carries an "error"
object instead of "choices". Accessing the missing "choices" field
failed with the SDK's generic "OpenAIInvalidDataException: `choices`
is not set", hiding the provider error message and status code from
the application.

Detect this shape before reading "choices" and throw the openai-java
service exception matching the reported error code (e.g.
`RateLimitException` for 429, `InternalServerException` for 5xx),
carrying the provider's error message.

Fixes spring-projects#6900

Signed-off-by: seonghun lee <harrisleesh@gmail.com>
@harrisleesh
harrisleesh force-pushed the gh-6900-openrouter-error branch from 3e114cc to a832798 Compare September 3, 2026 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improper handling of error responses from providers like OpenRouter

2 participants