micro/go-micro · error
failed to create request: %w
Error message
failed to create request: %w
What it means
Thrown when http.NewRequestWithContext cannot construct the POST request to the Ollama OpenAI-compatible endpoint in callOpenAI. This is almost always caused by an invalid BaseURL that does not parse as a URL (control characters, unparseable scheme/host).
Source
Thrown at ai/ollama/ollama.go:235
if followUpResp.Reply != "" {
resp.Answer = followUpResp.Reply
}
break
}
return resp, nil
}
func (p *Provider) callOpenAI(ctx context.Context, req map[string]any) (*ai.Response, *rawChatMessage, error) {
reqBody, err := json.Marshal(req)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal request: %w", err)
}
apiURL := strings.TrimRight(p.opts.BaseURL, "/") + p.chatPath()
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))
if err != nil {
return nil, nil, fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
if p.opts.APIKey != "" {
httpReq.Header.Set("Authorization", "Bearer "+p.opts.APIKey)
}
httpResp, err := http.DefaultClient.Do(httpReq)
if err != nil {
return nil, nil, fmt.Errorf("API request failed: %w", err)
}
defer httpResp.Body.Close()
respBody, _ := io.ReadAll(httpResp.Body)
if httpResp.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("API error (%s): %s", httpResp.Status, string(respBody))
}
var chatResp struct {View on GitHub (pinned to 24529f1404)
Solutions
- Validate the Ollama BaseURL with url.Parse before creating the client/provider
- Trim whitespace and quotes from the BaseURL config value
- Confirm BaseURL includes a scheme, e.g. http://localhost:11434
- Log the composed apiURL when this error occurs
Example fix
// before
p, err := New(opts) // BaseURL: "http://localhost:11434\n"
// after
base := strings.TrimSpace(strings.Trim(opts.BaseURL, "\"'"))
if u, err := url.Parse(base); err != nil || u.Scheme == "" || u.Host == "" {
return fmt.Errorf("invalid base URL %q", opts.BaseURL)
}
opts.BaseURL = base Defensive patterns
Strategy: validation
Validate before calling
u, err := url.Parse(opts.BaseURL)
if err != nil || u.Scheme == "" || u.Host == "" {
return fmt.Errorf("invalid Ollama base URL: %q", opts.BaseURL)
} Type guard
func isValidURL(raw string) bool {
u, err := url.Parse(strings.TrimSpace(raw))
return err == nil && u.Scheme != "" && u.Host != ""
} Try / catch
resp, err := provider.Generate(ctx, req)
if err != nil {
if strings.Contains(err.Error(), "failed to create request") {
// BaseURL is malformed — fix configuration
}
return err
} Prevention
- Trim whitespace and quotes from URL config values
- Always include scheme and host in BaseURL
- Validate the URL at client construction time, not per request
- Test BaseURL composition in unit tests
When it happens
Trigger: http.NewRequestWithContext fails in callOpenAI (called from generateOpenAI) — typically when p.opts.BaseURL + p.chatPath() is an invalid URL: empty with bad composition, contains spaces/control chars, or a malformed scheme.
Common situations: Misconfigured OLLAMA base URL env var (spaces, missing scheme, quotes copied into the value); BaseURL set with a trailing newline from a config file; programmatically injected bad URL.
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 stream request: %w
- failed to create request: %w
- failed to create stream request: %w
- failed to create request: %w
- agent: ResumeStreamAsk requires a checkpoint
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/cd5035c0ddab29ae.
Report an issue: GitHub.