Include API error message in exception when log_errors is enabled#645
Open
vitor-de-castro wants to merge 2 commits into
Open
Include API error message in exception when log_errors is enabled#645vitor-de-castro wants to merge 2 commits into
vitor-de-castro wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Fixes #634