{"record":{"id":"cd5035c0ddab29ae","repo":"micro/go-micro","slug":"failed-to-create-request-w-cd5035","errorCode":null,"errorMessage":"failed to create request: %w","messagePattern":"failed to create request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/ollama/ollama.go","lineNumber":235,"sourceCode":"\t\tif followUpResp.Reply != \"\" {\n\t\t\tresp.Answer = followUpResp.Reply\n\t\t}\n\t\tbreak\n\t}\n\n\treturn resp, nil\n}\n\nfunc (p *Provider) callOpenAI(ctx context.Context, req map[string]any) (*ai.Response, *rawChatMessage, error) {\n\treqBody, err := json.Marshal(req)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to marshal request: %w\", err)\n\t}\n\n\tapiURL := strings.TrimRight(p.opts.BaseURL, \"/\") + p.chatPath()\n\thttpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to create request: %w\", err)\n\t}\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\tif p.opts.APIKey != \"\" {\n\t\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+p.opts.APIKey)\n\t}\n\n\thttpResp, err := http.DefaultClient.Do(httpReq)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"API request failed: %w\", err)\n\t}\n\tdefer httpResp.Body.Close()\n\n\trespBody, _ := io.ReadAll(httpResp.Body)\n\tif httpResp.StatusCode != http.StatusOK {\n\t\treturn nil, nil, fmt.Errorf(\"API error (%s): %s\", httpResp.Status, string(respBody))\n\t}\n\n\tvar chatResp struct {","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/ollama/ollama.go#L217-L253","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\np, err := New(opts) // BaseURL: \"http://localhost:11434\\n\"\n// after\nbase := strings.TrimSpace(strings.Trim(opts.BaseURL, \"\\\"'\"))\nif u, err := url.Parse(base); err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid base URL %q\", opts.BaseURL)\n}\nopts.BaseURL = base","handlingStrategy":"validation","validationCode":"u, err := url.Parse(opts.BaseURL)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid Ollama base URL: %q\", opts.BaseURL)\n}","typeGuard":"func isValidURL(raw string) bool {\n    u, err := url.Parse(strings.TrimSpace(raw))\n    return err == nil && u.Scheme != \"\" && u.Host != \"\"\n}","tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to create request\") {\n        // BaseURL is malformed — fix configuration\n    }\n    return err\n}","preventionTips":["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"],"tags":["http-request","url","configuration","ollama"],"backgroundTag":"invalid-url","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}