{"record":{"id":"fba1826e476a965b","repo":"micro/go-micro","slug":"stream-api-request-failed-w-fba182","errorCode":null,"errorMessage":"stream API request failed: %w","messagePattern":"stream API request failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/openai/openai.go","lineNumber":224,"sourceCode":"\tif p.opts.Effort != \"\" {\n\t\tapiReq[\"reasoning_effort\"] = p.opts.Effort\n\t}\n\treqBody, err := json.Marshal(apiReq)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal stream request: %w\", err)\n\t}\n\tapiURL := strings.TrimRight(p.opts.BaseURL, \"/\") + \"/v1/chat/completions\"\n\thttpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create stream request: %w\", err)\n\t}\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Accept\", \"text/event-stream\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+p.opts.APIKey)\n\n\thttpResp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"stream API request failed: %w\", err)\n\t}\n\tif httpResp.StatusCode != http.StatusOK {\n\t\tdefer httpResp.Body.Close()\n\t\trespBody, _ := io.ReadAll(httpResp.Body)\n\t\treturn nil, fmt.Errorf(\"stream API error (%s): %s\", httpResp.Status, string(respBody))\n\t}\n\treturn &openAIStream{body: httpResp.Body, scanner: bufio.NewScanner(httpResp.Body)}, nil\n}\n\ntype openAIStream struct {\n\tbody    io.ReadCloser\n\tscanner *bufio.Scanner\n\tclosed  bool\n}\n\nfunc (s *openAIStream) Recv() (*ai.Response, error) {\n\tfor s.scanner.Scan() {\n\t\tline := strings.TrimSpace(s.scanner.Text())","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/openai/openai.go#L206-L242","documentation":"Wraps the transport error from http.DefaultClient.Do in the OpenAI provider's Stream call. The request failed at the network layer before a status code was obtained: DNS failure, connection refused, TLS error, or context cancellation. The original error is preserved with %w.","triggerScenarios":"Calling Stream() when the OpenAI-compatible endpoint is unreachable: wrong BaseURL host/port, no internet/DNS, TLS interception with untrusted certs, or the passed ctx is cancelled or times out before the response.","commonSituations":"Typo'd BaseURL (e.g. api.openai.com misspelled); running in an air-gapped environment or behind a corporate proxy that strips TLS; context deadline too short for slow network; firewall blocking outbound 443.","solutions":["Verify reachability: curl -i $BASEURL/v1/chat/completions from the same host","Correct opts.BaseURL (scheme, host, port)","Increase the context timeout or fix premature cancellation of ctx","Configure HTTPS_PROXY / trust the corporate CA if behind a TLS-intercepting proxy"],"exampleFix":"// before\nctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)\n// after\nctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)","handlingStrategy":"retry","validationCode":"// cheap connectivity check to the endpoint host before streaming\nconn, err := net.DialTimeout(\"tcp\", host+\":443\", 3*time.Second)\nif err != nil {\n    return fmt.Errorf(\"endpoint unreachable: %w\", err)\n}\nconn.Close()","typeGuard":null,"tryCatchPattern":"_, err := provider.Stream(ctx, req)\nif err != nil {\n    if errors.Is(err, context.DeadlineExceeded) {\n        // retry with a longer timeout\n    } else if isNetworkError(err) { // errors.As(err, &net.Error)\n        // exponential backoff retry\n    }\n    return err\n}","preventionTips":["Verify outbound HTTPS connectivity from the deployment environment","Set realistic context timeouts for streaming requests","Configure proxy/CA settings for corporate networks","Resolve the API hostname at startup to catch DNS issues early"],"tags":["network","http","openai","streaming"],"backgroundTag":"connection-refused","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}