Skip to content

Include API error message in exception when log_errors is enabled#645

Open
vitor-de-castro wants to merge 2 commits into
alexrudall:mainfrom
vitor-de-castro:improve-exception-message
Open

Include API error message in exception when log_errors is enabled#645
vitor-de-castro wants to merge 2 commits into
alexrudall:mainfrom
vitor-de-castro:improve-exception-message

Conversation

@vitor-de-castro

Copy link
Copy Markdown

Problem

When the OpenAI API returns an error response, the exception message only contains the HTTP status code and URL:

the server responded with status 400 for POST https://api.openai.com/v1/chat/completions

A 400 error can be caused by many different issues and the actual reason is buried in the response body. This makes debugging very difficult, especially in background job environments like Solid Queue where only the exception message is logged.

Solution

When log_errors is enabled, extract the error message from the response body and append it to the exception message.

Before:
the server responded with status 400 for POST https://api.openai.com/v1/chat/completions

After:
the server responded with status 400 for POST https://api.openai.com/v1/chat/completions - Invalid content type. image_url is only supported by certain models.

Changes

  • lib/openai.rb — extract error.message from response body in MiddlewareErrors
  • spec/openai/client/http_spec.rb — add test verifying the API error message is included in the exception

Fixes #634

When the OpenAI API returns an error response, the exception message
only contained the HTTP status code and URL, making it difficult to
debug the actual cause of the error.

This change extracts the error message from the response body and
appends it to the exception message when MiddlewareErrors is active
(i.e. when log_errors is set to true).

Before: 'the server responded with status 400 for POST ...'
After:  'the server responded with status 400 for POST ... - Invalid content type. image_url is only supported by certain models.'

Fixes alexrudall#634
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.

Improve the exception message

1 participant