-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(openai_compat): clarify HTML response parse errors #1075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
4946a8b
a305c0a
9216cd1
c1a3876
6eaa49f
53cba73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
|
|
@@ -189,10 +190,89 @@ func (p *Provider) Chat( | |
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("API request failed:\n Status: %d\n Body: %s", resp.StatusCode, string(body)) | ||
| return nil, wrapHTTPResponseError(resp.StatusCode, body, resp.Header.Get("Content-Type"), p.apiBase) | ||
| } | ||
|
|
||
| return parseResponse(body) | ||
| out, err := parseResponse(body) | ||
| if err != nil { | ||
| return nil, wrapResponseParseError(err, body, resp.Header.Get("Content-Type"), p.apiBase) | ||
| } | ||
|
|
||
| return out, nil | ||
| } | ||
|
||
|
|
||
| func wrapResponseParseError(err error, body []byte, contentType, apiBase string) error { | ||
| if message, ok := htmlResponseMessage(body, contentType, apiBase); ok { | ||
| return errors.New(message) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func wrapHTTPResponseError(statusCode int, body []byte, contentType, apiBase string) error { | ||
| if message, ok := htmlResponseMessage(body, contentType, apiBase); ok { | ||
| return fmt.Errorf("API request failed:\n Status: %d\n Detail: %s", statusCode, message) | ||
| } | ||
| return fmt.Errorf("API request failed:\n Status: %d\n Body: %s", statusCode, string(body)) | ||
| } | ||
|
|
||
| func htmlResponseMessage(body []byte, contentType, apiBase string) (string, bool) { | ||
| trimmedContentType := strings.TrimSpace(contentType) | ||
| if !looksLikeHTML(body, trimmedContentType) { | ||
| return "", false | ||
| } | ||
|
|
||
| contentTypeHint := "" | ||
| if trimmedContentType != "" { | ||
| contentTypeHint = fmt.Sprintf(" (content-type: %s)", trimmedContentType) | ||
| } | ||
|
|
||
| return fmt.Sprintf( | ||
| "expected JSON response from %s/chat/completions, but received HTML%s; check api_base or proxy configuration. Response preview: %s", | ||
| apiBase, | ||
| contentTypeHint, | ||
| responsePreview(body, 160), | ||
| ), true | ||
| } | ||
|
|
||
| func looksLikeHTML(body []byte, contentType string) bool { | ||
| contentType = strings.ToLower(strings.TrimSpace(contentType)) | ||
| if strings.Contains(contentType, "text/html") || strings.Contains(contentType, "application/xhtml+xml") { | ||
| return true | ||
| } | ||
|
|
||
| trimmed := strings.ToLower(string(leadingTrimmedPrefix(body, 128))) | ||
| return strings.HasPrefix(trimmed, "<!doctype html") || | ||
|
||
| strings.HasPrefix(trimmed, "<html") || | ||
| strings.HasPrefix(trimmed, "<head") || | ||
| strings.HasPrefix(trimmed, "<body") | ||
| } | ||
|
|
||
| func leadingTrimmedPrefix(body []byte, maxLen int) []byte { | ||
| i := 0 | ||
| for i < len(body) { | ||
| switch body[i] { | ||
| case ' ', '\t', '\n', '\r', '\f', '\v': | ||
| i++ | ||
| default: | ||
| end := i + maxLen | ||
| if end > len(body) { | ||
| end = len(body) | ||
| } | ||
| return body[i:end] | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func responsePreview(body []byte, maxLen int) string { | ||
| trimmed := bytes.TrimSpace(body) | ||
| if len(trimmed) == 0 { | ||
| return "<empty>" | ||
| } | ||
| if len(trimmed) <= maxLen { | ||
| return string(trimmed) | ||
| } | ||
| return string(trimmed[:maxLen]) + "..." | ||
| } | ||
|
|
||
| func parseResponse(body []byte) (*LLMResponse, error) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, if the server returns a 502 Bad Gateway (typical of nginx proxies) with an HTML page, the code stops there.
This means that if a proxy goes wrong by returning a 502 HTML, your new wrapResponseParseError wrapper is not invoked, because it only kicks in if the status code is 200 OK (which is rarely the case if the endpoint is broken and returns HTML).
How do you want to handle this? If the goal is to intercept HTML and show your error message even when there is an HTTP error (such as 404 Not Found, 502 Bad Gateway, 503 Service Unavailable, often accompanied by HTML), you may want to invoke your HTML check even within the resp.StatusCode != http.StatusOK block.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, fixed in 9216cd1. For non-200 responses we now run the same HTML diagnostic path via wrapHTTPResponseError(...), so 404/502/503 HTML pages also return the actionable api_base/proxy hint instead of only raw body output. Added TestProviderChat_HTMLErrorResponseReturnsHelpfulError (502 case).