micro/go-micro · error
stream API request failed: %w
Error message
stream API request failed: %w
What it means
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).
Source
Thrown at ai/anthropic/anthropic.go:263
applyReasoningOptions(apiReq, p.opts)
reqBody, err := json.Marshal(apiReq)
if err != nil {
return nil, fmt.Errorf("failed to marshal stream request: %w", err)
}
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + "/v1/messages"
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
if err != nil {
return nil, fmt.Errorf("failed to create stream request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Accept", "text/event-stream")
httpReq.Header.Set("x-api-key", p.opts.APIKey)
httpReq.Header.Set("anthropic-version", "2023-06-01")
httpResp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("stream API request failed: %w", err)
}
if httpResp.StatusCode != http.StatusOK {
defer httpResp.Body.Close()
respBody, _ := io.ReadAll(httpResp.Body)
return nil, fmt.Errorf("stream API error (%s): %s", httpResp.Status, string(respBody))
}
return &streamReader{body: httpResp.Body, scanner: bufio.NewScanner(httpResp.Body)}, nil
}
type streamReader struct {
body io.ReadCloser
scanner *bufio.Scanner
closed bool
}
func (s *streamReader) Recv() (*ai.Response, error) {
for s.scanner.Scan() {
line := strings.TrimSpace(s.scanner.Text())View on GitHub (pinned to 24529f1404)
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.
Example fix
// before
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
resp, err := provider.Stream(ctx, req)
// after
ctx, cancel := context.WithTimeout(ctx, 120*time.Second) // streaming needs long deadlines
resp, err := provider.Stream(ctx, req)
if err != nil { log.Printf("stream transport error: %v", err) } Defensive patterns
Strategy: retry
Validate before calling
// pre-flight connectivity check
func canReach(ctx context.Context) error {
c, err := net.DialTimeout("tcp", "api.anthropic.com:443", 5*time.Second)
if err != nil { return err }
c.Close(); return nil
} Try / catch
resp, err := provider.Stream(ctx, req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) { /* raise deadline & retry */ }
if errors.Is(err, context.Canceled) { return err } // do not retry
// wrap with transport context: DNS/TLS/proxy
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.
Related errors
- API request failed: %w
- failed to create stream request: %w
- stream API error (%s): %s
- failed to read response: %w
- stream API request failed: %w
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/9ff4e89773ca7790.
Report an issue: GitHub.