Skip to content

Return http status code for ApiError#552

Merged
64bit merged 3 commits into
64bit:mainfrom
max-zheng:max/return_api_error_http_status
May 21, 2026
Merged

Return http status code for ApiError#552
64bit merged 3 commits into
64bit:mainfrom
max-zheng:max/return_api_error_http_status

Conversation

@max-zheng
Copy link
Copy Markdown
Contributor

Preserves the HTTP StatusCode from non-2xx responses by changing OpenAIError::ApiError to wrap a new ApiErrorResponse { status_code, api_error } struct. This lets callers distinguish 400 / 404 / 5xx without pattern-matching on ApiError.r#type strings — the original motivation in #549, e.g. for gateways that proxy to non-OpenAI-compatible upstreams.

Implements option (2) from the issue discussion (the maintainer's preferred shape — wrapping in a struct rather than a struct-variant, so a single value can be passed/extended later).

This is a breaking change for callers that pattern-match on OpenAIError::ApiError(_): the inner type is now ApiErrorResponse rather than ApiError, accessed via .api_error. Per the issue thread, a dedicated minor release was suggested.

Closes #549.

Changes

  • error.rs: add ApiErrorResponse (status + api_error, with Display/Error impls); change variant to ApiError(ApiErrorResponse). The existing ApiError struct and its JSON-body Display are untouched.
  • client.rs read_response: wrap both 5xx and non-2xx construction sites with the captured status.
  • middleware/retry/openai.rs: capture status_code before consuming the response body in the 429 path; wrap both the transient and insufficient_quota error returns.

Comment thread async-openai/src/error.rs
Comment thread async-openai/src/error.rs
Copy link
Copy Markdown
Owner

@64bit 64bit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!

@64bit 64bit merged commit 9639a11 into 64bit:main May 21, 2026
99 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve upstream HTTP status code in OpenAIError for non-2xx responses

2 participants