{"record":{"id":"c530b67e772b03bc","repo":"wavetermdev/waveterm","slug":"openai-s-s","errorCode":null,"errorMessage":"openai %s: %s","messagePattern":"openai (.+?): (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/aiusechat/openai/openai-backend.go","lineNumber":592,"sourceCode":"\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}\n\n// handleOpenAIStreamingResp handles the OpenAI SSE streaming response\nfunc handleOpenAIStreamingResp(ctx context.Context, sse *sse.SSEHandlerCh, decoder *eventsource.Decoder, cont *uctypes.WaveContinueResponse, chatOpts uctypes.WaveChatOpts) (*uctypes.WaveStopReason, []*OpenAIChatMessage) {\n\t// Per-response state\n\tstate := &openaiStreamingState{\n\t\tblockMap: map[string]*openaiBlockState{},\n\t\tchatOpts: chatOpts,\n\t}","sourceCodeStart":574,"sourceCodeEnd":610,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/aiusechat/openai/openai-backend.go#L574-L610","documentation":"This error surfaces when the OpenAI API returns an HTTP error whose body successfully parsed as the standard OpenAI error envelope ({\"error\":{\"message\":...}}). The message is 'openai <status>: <api error message>'. It is the library's way of forwarding the API's own error text alongside the HTTP status.","triggerScenarios":"Any non-2xx response from the OpenAI API carrying the standard error JSON: invalid API key (401), rate limits (429), invalid model name (404/400), malformed request body (400), quota exceeded (429), server errors with structured bodies.","commonSituations":"Expired or rotated API keys; exceeding RPM/TPM rate limits; using a model name the account/key cannot access; requests sent to the wrong api_base (e.g. Azure endpoint with OpenAI client); prompt exceeds context window.","solutions":["Read the API error text after 'openai <status>:' and act on it (401 -> fix key, 429 -> backoff, 404 -> fix model name)","Verify the API key is valid and has quota for the requested model","Implement exponential backoff on 429 responses","Confirm the configured base URL matches the provider (api.openai.com vs Azure/compatible proxy)"],"exampleFix":"// before\nclient.SetAPIKey(\"\") // stale/empty key -> 401 -> openai 401: Incorrect API key provided\n// after\nclient.SetAPIKey(os.Getenv(\"OPENAI_API_KEY\")) // load from env and validate before calls","handlingStrategy":"try-catch","validationCode":"null","typeGuard":null,"tryCatchPattern":"if err != nil {\n    var status, msg string\n    if n, _ := fmt.Sscanf(err.Error(), \"openai %s: %s\", &status, &msg); n == 2 {\n        switch {\n        case strings.HasPrefix(status, \"429\"): scheduleRetry()\n        case strings.HasPrefix(status, \"401\"): refreshAPIKey()\n        default: logAPIError(status, msg)\n        }\n    }\n}","preventionTips":["Validate API key and model name before sending","Implement 429 backoff handling","Keep base URL and key scope (project/org) correct"],"tags":["openai","api-error","http-4xx"],"backgroundTag":"openai-api-error","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}