{"record":{"id":"60dd4c97e8a6962a","repo":"wavetermdev/waveterm","slug":"openai-s-failed-to-read-error-response-v","errorCode":null,"errorMessage":"openai %s: failed to read error response: %v","messagePattern":"openai (.+?): failed to read error response: (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/aiusechat/openai/openai-backend.go","lineNumber":584,"sourceCode":"\n\t// At this point we have a valid SSE stream, so setup SSE handling\n\t// From here on, errors must be returned through the SSE stream\n\tif cont == nil {\n\t\tsse.SetupSSE()\n\t}\n\n\t// Use eventsource decoder for proper SSE parsing\n\tdecoder := eventsource.NewDecoder(resp.Body)\n\n\tstopReason, rtnMessages := handleOpenAIStreamingResp(ctx, sse, decoder, cont, chatOpts)\n\treturn stopReason, rtnMessages, rateLimitInfo, nil\n}\n\n// parseOpenAIHTTPError parses OpenAI API HTTP error responses\nfunc parseOpenAIHTTPError(resp *http.Response) error {\n\tbody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"openai %s: failed to read error response: %v\", resp.Status, err)\n\t}\n\n\tlogutil.DevPrintf(\"openai full error: %s\\n\", body)\n\n\t// Try to parse as OpenAI error format first\n\tvar errorResp openAIErrorResponse\n\tif err := json.Unmarshal(body, &errorResp); err == nil && errorResp.Error.Message != \"\" {\n\t\treturn fmt.Errorf(\"openai %s: %s\", resp.Status, errorResp.Error.Message)\n\t}\n\n\t// Try to parse as proxy error format\n\tvar proxyErr uctypes.ProxyErrorResponse\n\tif err := json.Unmarshal(body, &proxyErr); err == nil && !proxyErr.Success && proxyErr.Error != \"\" {\n\t\treturn fmt.Errorf(\"openai %s: %s\", resp.Status, proxyErr.Error)\n\t}\n\n\treturn fmt.Errorf(\"openai %s: %s\", resp.Status, utilfn.TruncateString(string(body), 120))\n}","sourceCodeStart":566,"sourceCodeEnd":602,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/aiusechat/openai/openai-backend.go#L566-L602","documentation":"This error is returned by parseOpenAIHTTPError when the OpenAI API returns a non-2xx HTTP response but the error body itself cannot be read from the response stream (io.ReadAll fails). The HTTP status (e.g. '500 Internal Server Error') and the underlying read error are wrapped into one message. It indicates a transport-level problem rather than an API-reported error.","triggerScenarios":"The response body stream was already consumed or closed before parseOpenAIHTTPError ran; connection reset mid-read of the error body; a proxy/interceptor closed resp.Body prematurely; context cancellation aborting the body read.","commonSituations":"Flaky corporate proxies or VPNs killing the connection on error responses; timeouts while reading large error bodies; misconfigured HTTP middleware (logging/retry layers) that drains the body; mobile/unstable networks.","solutions":["Check network/proxy stability between client and the OpenAI API endpoint; retry the request","Verify no custom http.RoundTripper or middleware is consuming or closing resp.Body before error parsing","Add retries with backoff for transient network failures","Enable DevPrintf logging to capture the request flow and reproduce the read failure"],"exampleFix":"// before\nerr := client.Do(req)\nif err != nil { return err }\n// a middleware drains resp.Body for logging, so error parsing fails\n// after\n// in your RoundTripper, buffer the body instead of consuming it:\nbodyBytes, _ := io.ReadAll(resp.Body)\nresp.Body = io.NopCloser(bytes.NewReader(bodyBytes)) // body available again downstream","handlingStrategy":"retry","validationCode":"null","typeGuard":"null","tryCatchPattern":"if strings.Contains(err.Error(), \"failed to read error response\") {\n    // transient body-read failure: retry with backoff\n    time.Sleep(backoff)\n    return doChatRequest(ctx, msgs)\n}","preventionTips":["Avoid middleware/RoundTrippers that consume resp.Body without restoring it","Use retry with exponential backoff for chat API calls","Monitor network/proxy health between client and provider"],"tags":["openai","network","http-error-parsing"],"backgroundTag":"response-body-read-failed","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}