Skip to content

fix(error): fall back to full response body when error field is absent#1784

Open
s-zx wants to merge 1 commit intoopenai:masterfrom
s-zx:fix/1734-error-field-fallback
Open

fix(error): fall back to full response body when error field is absent#1784
s-zx wants to merge 1 commit intoopenai:masterfrom
s-zx:fix/1734-error-field-fallback

Conversation

@s-zx
Copy link

@s-zx s-zx commented Mar 21, 2026

Summary

Fixes #1734

When an OpenAI-compatible API returns an error response that does not include an error field (e.g. using detail, message, or other custom fields at the top level), the Node.js client produced unhelpful error messages like 422 status code (no body).

This is because APIError.generate() extracted only body.error and passed undefined to the error constructors when that field was missing — the makeMessage logic then had nothing to display.

Change

In src/core/error.ts, update the error extraction in APIError.generate() to fall back to the full response body when body.error is not present:

// Before
const error = (errorResponse as Record<string, any>)?.['error'];

// After
const error = (errorResponse as Record<string, any>)?.['error'] ?? errorResponse;

This mirrors the behavior of the Python client, which falls back to including the entire response body in the error message when no error field is found. The existing makeMessage logic already handles this correctly by calling JSON.stringify() on non-string error values.

Example

With a response body like {"detail": "422: The model gpt-5-gibberish does not exist."}:

  • Before: 422 status code (no body)
  • After: 422 {"detail":"422: The model gpt-5-gibberish does not exist."}

When an OpenAI-compatible API returns an error response that does not include
an `error` field (e.g. using `detail` or other custom fields), the Node.js
client previously produced unhelpful messages like "422 status code (no body)".

This fix mirrors the Python client behavior: if `body.error` is not present,
the entire response body is used as the error detail. The existing `makeMessage`
logic already handles this case by calling JSON.stringify on non-string error
values, so downstream error messages will now include the full response body
when no `error` field is found.
@s-zx s-zx requested a review from a team as a code owner March 21, 2026 22:50
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.

The node.js client only reads errors from the "error" field of a response, with no fallback if it doesn't exist

2 participants