{"record":{"id":"6c17dc22370884b4","repo":"micro/go-micro","slug":"failed-to-create-stream-request-w-6c17dc","errorCode":null,"errorMessage":"failed to create stream request: %w","messagePattern":"failed to create stream request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/ollama/ollama.go","lineNumber":339,"sourceCode":"\tapiReq := map[string]any{\n\t\t\"model\":          p.opts.Model,\n\t\t\"messages\":       messages,\n\t\t\"stream\":         true,\n\t\t\"stream_options\": map[string]any{\"include_usage\": true},\n\t}\n\tif p.opts.MaxTokens > 0 {\n\t\tapiReq[\"max_tokens\"] = p.opts.MaxTokens\n\t}\n\n\treqBody, err := json.Marshal(apiReq)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal stream request: %w\", err)\n\t}\n\n\tapiURL := strings.TrimRight(p.opts.BaseURL, \"/\") + p.streamPath()\n\thttpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(reqBody))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create stream request: %w\", err)\n\t}\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Accept\", \"text/event-stream\")\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, fmt.Errorf(\"stream API request failed: %w\", err)\n\t}\n\tif httpResp.StatusCode != http.StatusOK {\n\t\tdefer httpResp.Body.Close()\n\t\trespBody, _ := io.ReadAll(httpResp.Body)\n\t\treturn nil, fmt.Errorf(\"stream API error (%s): %s\", httpResp.Status, string(respBody))\n\t}\n\n\treturn &sseStream{body: httpResp.Body, scanner: bufio.NewScanner(httpResp.Body)}, nil","sourceCodeStart":321,"sourceCodeEnd":357,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/ollama/ollama.go#L321-L357","documentation":"Thrown when http.NewRequestWithContext cannot build the streaming POST request to the Ollama OpenAI-compatible stream endpoint in streamOpenAI (called from Stream). The cause is virtually always an invalid composed URL (bad BaseURL), since the body is already-marshalled bytes.","triggerScenarios":"http.NewRequestWithContext fails in streamOpenAI (called from Stream) because apiURL = BaseURL + p.streamPath() is unparseable — BaseURL with spaces/control characters, quotes from config, missing scheme, or other malformed URL syntax.","commonSituations":"OLLAMA base URL env var with stray whitespace/newline or copy-pasted quotes; https scheme against a plain-http local server manifesting as URL/TLS issues; programmatic BaseURL composition bugs.","solutions":["Validate and normalize the BaseURL (trim spaces/quotes, ensure scheme+host) with url.Parse before use","Confirm BaseURL matches the running Ollama server, e.g. http://localhost:11434","Log the composed apiURL when this error occurs to spot malformed composition","Fix the env/config source of the BaseURL rather than patching at call time"],"exampleFix":"// before\nprovider, err := ai.NewOllama(ai.Options{BaseURL: os.Getenv(\"OLLAMA_URL\")})\n// after\nbase := strings.TrimSpace(strings.Trim(os.Getenv(\"OLLAMA_URL\"), \"\\\"'\"))\nif u, err := url.Parse(base); err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid OLLAMA_URL %q\", os.Getenv(\"OLLAMA_URL\"))\n}\nprovider, err := ai.NewOllama(ai.Options{BaseURL: base})","handlingStrategy":"validation","validationCode":"u, err := url.Parse(strings.TrimSpace(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":"stream, err := provider.Stream(ctx, req)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to create stream request\") {\n        // BaseURL malformed — fix env/config\n    }\n    return err\n}","preventionTips":["Normalize and validate BaseURL at provider construction","Strip whitespace/quotes from environment-sourced URLs","Include scheme and host explicitly (http://localhost:11434)","Log the composed stream URL on failure"],"tags":["http-request","url","streaming","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"}