{"record":{"id":"9ff4e89773ca7790","repo":"micro/go-micro","slug":"stream-api-request-failed-w","errorCode":null,"errorMessage":"stream API request failed: %w","messagePattern":"stream API request failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/anthropic/anthropic.go","lineNumber":263,"sourceCode":"\tapplyReasoningOptions(apiReq, p.opts)\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\n\tapiURL := strings.TrimRight(p.opts.BaseURL, \"/\") + \"/v1/messages\"\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(\"x-api-key\", p.opts.APIKey)\n\thttpReq.Header.Set(\"anthropic-version\", \"2023-06-01\")\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 &streamReader{body: httpResp.Body, scanner: bufio.NewScanner(httpResp.Body)}, nil\n}\n\ntype streamReader struct {\n\tbody    io.ReadCloser\n\tscanner *bufio.Scanner\n\tclosed  bool\n}\n\nfunc (s *streamReader) Recv() (*ai.Response, error) {\n\tfor s.scanner.Scan() {\n\t\tline := strings.TrimSpace(s.scanner.Text())","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/anthropic/anthropic.go#L245-L281","documentation":"Returned by Provider.Stream when the HTTP transport itself fails while executing the POST to the Anthropic streaming endpoint — the request never completed, so this wraps the net/http error (DNS failure, TLS error, timeout, connection refused, context cancellation).","triggerScenarios":"http.DefaultClient.Do(httpReq) returns an error during the /v1/messages streaming call: no network, DNS resolution failure, TLS handshake failure, proxy issues, or ctx cancelled/deadline exceeded mid-request.","commonSituations":"Working offline or behind a corporate proxy without HTTP_PROXY set, firewall blocking api.anthropic.com:443, context deadline too short for model latency, or invalid self-signed MITM certificates.","solutions":["Check basic connectivity: curl -v https://api.anthropic.com/v1/messages from the host.","Set proxy env vars (HTTPS_PROXY) if behind a corporate proxy, or use a custom http.Client with proxy configuration instead of http.DefaultClient.","Verify the context deadline is generous enough for streaming requests; increase it or remove a short WithTimeout.","Inspect the wrapped error with errors.Is(err, context.DeadlineExceeded) / context.Canceled to distinguish timeout vs explicit cancellation."],"exampleFix":"// before\nctx, cancel := context.WithTimeout(ctx, 2*time.Second)\nresp, err := provider.Stream(ctx, req)\n// after\nctx, cancel := context.WithTimeout(ctx, 120*time.Second) // streaming needs long deadlines\nresp, err := provider.Stream(ctx, req)\nif err != nil { log.Printf(\"stream transport error: %v\", err) }","handlingStrategy":"retry","validationCode":"// pre-flight connectivity check\nfunc canReach(ctx context.Context) error {\n\tc, err := net.DialTimeout(\"tcp\", \"api.anthropic.com:443\", 5*time.Second)\n\tif err != nil { return err }\n\tc.Close(); return nil\n}","typeGuard":null,"tryCatchPattern":"resp, err := provider.Stream(ctx, req)\nif err != nil {\n\tif errors.Is(err, context.DeadlineExceeded) { /* raise deadline & retry */ }\n\tif errors.Is(err, context.Canceled) { return err } // do not retry\n\t// wrap with transport context: DNS/TLS/proxy\n}","preventionTips":["Use generous timeouts for streaming requests (minutes, not seconds)","Configure HTTPS_PROXY in corporate environments","Do not cancel the context while consuming the stream","Check errors.Is against context.Canceled before retrying"],"tags":["network","http","timeout","anthropic","streaming"],"backgroundTag":"http-request-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}