micro/go-micro · error
failed to create stream request: %w
Error message
failed to create stream request: %w
What it means
Thrown in the OpenAI provider's Stream when http.NewRequestWithContext fails to construct the HTTP request for the streaming chat-completions call. Per net/http this only fails on an invalid method or an unparseable URL, so it almost always indicates a malformed or empty BaseURL option.
Source
Thrown at ai/openai/openai.go:216
"model": p.opts.Model,
"messages": messages,
"stream": true,
"stream_options": map[string]any{"include_usage": true},
}
if p.opts.MaxTokens > 0 {
apiReq["max_tokens"] = p.opts.MaxTokens
}
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 {View on GitHub (pinned to 24529f1404)
Solutions
- Set a valid absolute BaseURL, e.g. WithBaseURL("https://api.openai.com")
- Verify the configured URL parses: add 'https://' if the scheme is missing
- Print/validate opts.BaseURL before constructing the provider
- Check config loading so an empty env var doesn't overwrite the default BaseURL
Example fix
// before
WithBaseURL("localhost:8080")
// after
WithBaseURL("https://localhost:8080") Defensive patterns
Strategy: validation
Validate before calling
u, err := url.Parse(baseURL)
if err != nil || u.Scheme == "" || u.Host == "" {
return fmt.Errorf("invalid BaseURL %q", baseURL)
} Prevention
- Always configure BaseURL with scheme and host
- Validate the URL with url.Parse at provider construction
- Reject empty env-derived BaseURLs with a fallback default
- Add startup config validation that fails fast on bad URLs
When it happens
Trigger: Calling Stream() when opts.BaseURL is empty, contains spaces/invalid characters, or lacks a scheme (e.g. "localhost:8080" or ""), producing a URL http.NewRequestWithContext cannot parse (apiURL = BaseURL + "/v1/chat/completions").
Common situations: BaseURL left at zero value because WithBaseURL was never called and no default applied; trailing typo in a config file (unquoted YAML value); users pointing at Azure/OpenRouter with a URL missing 'https://'.
Understand the failure class
Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.
Related errors
- failed to create request: %w
- failed to create stream request: %w
- failed to create stream request: %w
- failed to create request: %w
- failed to create stream request: %w
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/e5cdf8e02fd56f05.
Report an issue: GitHub.