openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

fix: fall back to entire response body when 'error' field is missing

Open Limitless2023 opened this issue 9 months ago • 0 comments

Summary

Fixes #1734

When an OpenAI-compatible API returns error information in fields other than error (e.g. detail or message), the Node client currently reports 422 status code (no body) instead of showing the actual error content. This differs from the Python client, which falls back to the entire response body.

Changes

  • src/core/error.ts: In APIError.generate(), when errorResponse.error is undefined, fall back to errorResponse itself (the entire parsed JSON body) instead of passing undefined to the error constructor. This is a one-line change: ?? errorResponse.
  • tests/error.test.ts: Added tests covering:
    • Standard OpenAI error response (with error field) — still works as before
    • Non-standard response with detail field — now shows the error content
    • Non-standard response with message field — now shows the error content
    • Truly empty response — still shows (no body)
    • Raw text fallback — still works as before

Before / After

Before: An error response like {"detail": "422: The model gpt-5-gibberish does not exist."} would produce:

UnprocessableEntityError: 422 status code (no body)

After: The same response now produces:

UnprocessableEntityError: 422 {"detail": "422: The model gpt-5-gibberish does not exist."}

This matches the Python client's behavior of falling back to the full response body when the error field is absent.

Limitless2023 avatar Feb 11 '26 12:02 Limitless2023