From f08fedc4a11c301b4e3b651ba2b4c6d0247f8c12 Mon Sep 17 00:00:00 2001 From: liugangjian Date: Wed, 4 Mar 2026 18:35:32 +0800 Subject: [PATCH] fix(provider): improve error message when API returns HTML instead of JSON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When API endpoint is misconfigured (e.g., wrong api_base URL), the API returns an HTML error page. Previously, the error message was cryptic: "failed to unmarshal response: invalid character '<' looking for beginning of value". Now we detect HTML responses and provide a clear error message guiding users to check their api_base URL configuration. Fixes #1068 🤖 AI Code Generation: Fully AI generated - AI implemented the fix and I reviewed and tested it --- pkg/providers/openai_compat/provider.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/providers/openai_compat/provider.go b/pkg/providers/openai_compat/provider.go index ff9109e96..11e93a876 100644 --- a/pkg/providers/openai_compat/provider.go +++ b/pkg/providers/openai_compat/provider.go @@ -223,9 +223,15 @@ func parseResponse(body []byte) (*LLMResponse, error) { } if err := json.Unmarshal(body, &apiResponse); err != nil { + // Check if the response is HTML instead of JSON (e.g., wrong api_base URL) + bodyStr := string(body) + if strings.HasPrefix(strings.TrimSpace(bodyStr), "<") { + return nil, fmt.Errorf("API returned HTML instead of JSON. Please check your api_base URL configuration. The server may be returning an error page or the endpoint URL may be incorrect (e.g., missing '/v1' path)") + } return nil, fmt.Errorf("failed to unmarshal response: %w", err) } + if len(apiResponse.Choices) == 0 { return &LLMResponse{ Content: "",