micro/go-micro · error
stream API request failed: %w
Error message
stream API request failed: %w
What it means
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.
Source
Thrown at ai/openai/openai.go:224
if p.opts.Effort != "" {
apiReq["reasoning_effort"] = p.opts.Effort
}
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/chat/completions"
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("Authorization", "Bearer "+p.opts.APIKey)
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 &openAIStream{body: httpResp.Body, scanner: bufio.NewScanner(httpResp.Body)}, nil
}
type openAIStream struct {
body io.ReadCloser
scanner *bufio.Scanner
closed bool
}
func (s *openAIStream) Recv() (*ai.Response, error) {
for s.scanner.Scan() {
line := strings.TrimSpace(s.scanner.Text())View on GitHub (pinned to 24529f1404)
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
Example fix
// before ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) // after ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
Defensive patterns
Strategy: retry
Validate before calling
// cheap connectivity check to the endpoint host before streaming
conn, err := net.DialTimeout("tcp", host+":443", 3*time.Second)
if err != nil {
return fmt.Errorf("endpoint unreachable: %w", err)
}
conn.Close() Try / catch
_, err := provider.Stream(ctx, req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
// retry with a longer timeout
} else if isNetworkError(err) { // errors.As(err, &net.Error)
// exponential backoff retry
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.
Related errors
- stream API request failed: %w
- failed to create stream request: %w
- stream API error (%s): %s
- API request failed: %w
- API error: nil response
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/fba1826e476a965b.
Report an issue: GitHub.