micro/go-micro · error
stream API request failed: %w
Error message
stream API request failed: %w
What it means
streamOpenAI (used for Ollama Cloud / OpenAI-compatible endpoints) failed to execute the streaming HTTP POST to /v1/chat/completions. The error wraps the transport-level error from http.DefaultClient.Do, so the message contains the underlying net/http cause (DNS, TCP, TLS, timeout, context cancellation). No HTTP response was received at all.
Source
Thrown at ai/ollama/ollama.go:349
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, "/") + p.streamPath()
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")
if p.opts.APIKey != "" {
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 &sseStream{body: httpResp.Body, scanner: bufio.NewScanner(httpResp.Body)}, nil
}
// buildOpenAIMessages converts an ai.Request into the OpenAI chat message format.
func buildOpenAIMessages(req *ai.Request) []map[string]any {
messages := []map[string]any{}
if req.SystemPrompt != "" {
messages = append(messages, map[string]any{"role": "system", "content": req.SystemPrompt})
}
for _, m := range req.Messages {
messages = append(messages, map[string]any{"role": m.Role, "content": m.Content})View on GitHub (pinned to 24529f1404)
Solutions
- Verify BaseURL is reachable: curl -v <BaseURL>/v1/chat/completions from the same host.
- Check that ctx is not already cancelled and no overly tight timeout is applied.
- Confirm DNS/proxy settings (HTTP_PROXY/HTTPS_PROXY) and that ollama.com is reachable.
- Inspect the wrapped %w error for the concrete cause (connection refused vs timeout vs TLS).
Example fix
// before
p := ai.NewProvider("ollama", ai.WithBaseURL("https://ollama.wrong-host.example/v1"))
// after
p := ai.NewProvider("ollama", ai.WithBaseURL("https://ollama.com/v1"), ai.WithAPIKey(os.Getenv("OLLAMA_API_KEY"))) Defensive patterns
Strategy: retry
Validate before calling
u, err := url.Parse("https://ollama.com/v1")
if err != nil || u.Host == "" {
return fmt.Errorf("invalid base URL: %w", err)
}
conn, err := net.DialTimeout("tcp", u.Hostname()+":443", 3*time.Second)
if err != nil {
return fmt.Errorf("ollama cloud unreachable: %w", err)
}
conn.Close()
if ctx.Err() != nil {
return ctx.Err()
} Try / catch
stream, err := provider.Stream(ctx, req)
if err != nil {
var netErr net.Error
if errors.As(err, &netErr) && (errors.Is(err, context.DeadlineExceeded) || isTransient(err)) {
// retry with backoff
}
return fmt.Errorf("stream setup failed: %w", err)
} Prevention
- Health-check the endpoint at startup with a short plain (non-stream) request.
- Always pass a context with a sane but generous deadline for long streams.
- Log the wrapped error chain with %v/%w so the root cause is visible.
- Avoid aggressive proxies/VPNs between the app and ollama.com in production.
When it happens
Trigger: Calling Stream() when isCloud() is true (BaseURL contains ollama.com or cloudOverride set) and the HTTP request fails at the transport layer: server unreachable, connection refused, DNS failure, TLS error, or ctx cancelled before response headers.
Common situations: Wrong BaseURL or port, Ollama Cloud API key set but network/VPN blocking ollama.com, local proxy down, request context cancelled/expired mid-request, DNS misconfiguration in containers.
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
- stream API request failed: %w
- stream API request failed: %w
- API request failed: %w
- API request failed: %w
- stream API error (%s): %s
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/7690997c94374527.
Report an issue: GitHub.