Confirm this is a Node library issue and not an underlying OpenAI API issue
Describe the bug
Quick summary
If an OpenAI compatible API endpoint returns error information in the response in a field other than "error", the error message won't get reported.
This differs in behavior from the python client for openai, which will fall back to putting the entire response body in the status code
Is this a bug?
Is the python implementation more correct than the node.js one?
I'm not sure which implementation is "more correct". Let me know what you think.
- From my personal experience, my feedback is the python implementation was more helpful for me, because the endpoint I was connecting to did not put error information in "error"
- Feel free to close this if it's considered "out of spec" and unsupported for an openai compatible api to not put errors in the "error" field.
- I couldn't find a reference saying openai compatible apis must return errors in the "error" field.
I'm mostly making this issue to point out that the python client acts differently than the node.js one, and make people aware of that.
Sample working / non-working responses
Sample error response that will not be read by the openai node.js client (this will result in an error like 422 status code (no body)):
{
"detail":"422: The model gpt-5-gibberish does not exist."
}
Sample error response from the OpenAI API that does work:
{
"error": {
"message":"The model `gpt-5-gibberish` does not exist or you do not have access to it.",
"type":"invalid_request_error",
"param":null,
"code":"model_not_found"
}
}
node.js code trace
How the Node.js openai client looks for error messages:
- At
|
const error = (errorResponse as Record<string, any>)?.['error']; |
, const error = (errorResponse as Record<string, any>)?.['error']; -- error will be undefined
- At https://github.com/openai/openai-node/blob/44b7ac27bc989b2fc2bb2e831191821b11133d2e/src/core/error.ts#L50-52 , message and error will be undefined, msg will be undefined
a. Yes, message is undefined, it's not a bug that it's undefined, that's from the code here
|
const errMessage = errJSON ? undefined : errText; |
- You end up with error messages like 422 (no body)
In case this doesn't get fixed, a hacky workaround for this is to use dev tools and put a breakpoint at
|
const error = (errorResponse as Record<string, any>)?.['error']; |
- Then run this command
errorResponse.error = errorResponse.detail
- Let the execution continue, the error message will be better.
Python code trace
See this code: https://github.com/openai/openai-python/blob/v2.15.0/src/openai/_client.py#L408
- In the python implementation, if it can't find a field named "error", it will just put the entire body in the status code
See this pdb output for a demonstration
python -m pdb openai_error_test.py
(Pdb) b <path/to>/python3.13/site-packages/openai/_client.py:409
(Pdb) c
-> if response.status_code == 400:
(Pdb) p body
{'detail': '422: The model gpt-5-gibberish does not exist or you do not have access to it.'}
(Pdb) p data
{'detail': '422: The model gpt-5-gibberish does not exist or you do not have access to it.'}
(Pdb) p body.get("error")
None
To Reproduce
For my testing I used the chat completions API.
Node.js reproduction steps
- Take the basic example here https://github.com/openai/openai-quickstart-node/blob/master/chat_completions/index.js
- Cause an error. The easiest way I know of is to set the model id to a gibberish value like gpt-5-gibberish
- Connect to an API that puts error information in a field other than "error"
You should get an error like 422 status code (no body)
Python reproduction steps
- Take the basic example for chat completions from this page: https://github.com/openai/openai-python?tab=readme-ov-file#usage
- Cause an error. The easiest way I know of is to set the model id to a gibberish value like gpt-5-gibberish
- Connect to an API that puts error information in a field other than "error"
You should get something like this
openai.UnprocessableEntityError: Error code: 422 - {'detail': '422: The model gpt-5-gibberish does not exist or you do not have access to it.'}
OS
macOS
Node version
v24.11.1
Library version
v6.16.0
Confirm this is a Node library issue and not an underlying OpenAI API issue
Describe the bug
Quick summary
If an OpenAI compatible API endpoint returns error information in the response in a field other than "error", the error message won't get reported.
This differs in behavior from the python client for openai, which will fall back to putting the entire response body in the status code
Is this a bug?
Is the python implementation more correct than the node.js one?
I'm not sure which implementation is "more correct". Let me know what you think.
I'm mostly making this issue to point out that the python client acts differently than the node.js one, and make people aware of that.
Sample working / non-working responses
Sample error response that will not be read by the openai node.js client (this will result in an error like
422 status code (no body)):{ "detail":"422: The model gpt-5-gibberish does not exist." }Sample error response from the OpenAI API that does work:
{ "error": { "message":"The model `gpt-5-gibberish` does not exist or you do not have access to it.", "type":"invalid_request_error", "param":null, "code":"model_not_found" } }node.js code trace
How the Node.js openai client looks for error messages:
openai-node/src/core/error.ts
Line 69 in 44b7ac2
a. Yes, message is undefined, it's not a bug that it's undefined, that's from the code here
openai-node/src/client.ts
Line 714 in 44b7ac2
In case this doesn't get fixed, a hacky workaround for this is to use dev tools and put a breakpoint at
openai-node/src/core/error.ts
Line 69 in 44b7ac2
errorResponse.error = errorResponse.detailPython code trace
See this code: https://github.com/openai/openai-python/blob/v2.15.0/src/openai/_client.py#L408
See this pdb output for a demonstration
To Reproduce
For my testing I used the chat completions API.
Node.js reproduction steps
You should get an error like
422 status code (no body)Python reproduction steps
You should get something like this
OS
macOS
Node version
v24.11.1
Library version
v6.16.0