{"record":{"id":"ff64c0a519d60d81","repo":"micro/go-micro","slug":"failed-to-create-stream-request-w","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/anthropic/anthropic.go","lineNumber":254,"sourceCode":"// Stream generates a streaming response from Anthropic's Messages SSE API.\nfunc (p *Provider) Stream(ctx context.Context, req *ai.Request, opts ...ai.GenerateOption) (ai.Stream, error) {\n\tapiReq := map[string]any{\n\t\t\"model\":      p.opts.Model,\n\t\t\"max_tokens\": anthropicMaxTokens(p.opts),\n\t\t\"system\":     cacheableSystem(req.SystemPrompt, nil, p.opts.NoCache),\n\t\t\"messages\":   threadAnthropicMessages(req),\n\t\t\"stream\":     true,\n\t}\n\tapplyReasoningOptions(apiReq, p.opts)\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, \"/\") + \"/v1/messages\"\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\thttpReq.Header.Set(\"x-api-key\", p.opts.APIKey)\n\thttpReq.Header.Set(\"anthropic-version\", \"2023-06-01\")\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\treturn &streamReader{body: httpResp.Body, scanner: bufio.NewScanner(httpResp.Body)}, nil\n}\n","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/anthropic/anthropic.go#L236-L272","documentation":"This error is returned by Provider.Stream when building the outgoing POST request to the Anthropic /v1/messages endpoint fails via http.NewRequestWithContext. It wraps the underlying error (e.g. an unparseable URL or invalid context). Because the BaseURL is constructed with string concatenation, a malformed BaseURL is the usual cause.","triggerScenarios":"p.opts.BaseURL plus \"/v1/messages\" is not a valid URL (invalid characters, spaces, bad scheme, control characters), or the passed ctx has been constructed in a way NewRequestWithContext rejects (rare).","commonSituations":"Misconfigured ANTHROPIC_BASE_URL containing a trailing newline from an env file, a URL missing the scheme (e.g. \"api.anthropic.com\" instead of \"https://api.anthropic.com\"), or copying a base URL with surrounding whitespace into config.","solutions":["Check that opts.BaseURL is a fully qualified URL starting with http:// or https:// and trim whitespace/newlines: strings.TrimSpace(os.Getenv(\"ANTHROPIC_BASE_URL\")).","Print the composed URL (strings.TrimRight(p.opts.BaseURL,\"/\")+\"/v1/messages\") and validate it with url.Parse to see the wrapped cause.","Fix the source of the config value — quote handling in .env files, YAML, or shell export that injected bad characters."],"exampleFix":"// before\np, _ := anthropic.New(anthropic.WithBaseURL(os.Getenv(\"ANTHROPIC_BASE_URL\"))) // \"api.anthropic.com\"\n// after\nbase := strings.TrimSpace(os.Getenv(\"ANTHROPIC_BASE_URL\"))\nif !strings.HasPrefix(base, \"https://\") { base = \"https://\" + base }\np, _ := anthropic.New(anthropic.WithBaseURL(base))","handlingStrategy":"validation","validationCode":"func validateBaseURL(raw string) error {\n\tu, err := url.Parse(strings.TrimSpace(raw) + \"/v1/messages\")\n\tif err != nil { return fmt.Errorf(\"invalid base URL: %w\", err) }\n\tif u.Scheme != \"http\" && u.Scheme != \"https\" { return errors.New(\"base URL must use http(s)\") }\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"resp, err := provider.Stream(ctx, req)\nif err != nil {\n\treturn fmt.Errorf(\"stream setup failed (check BaseURL): %w\", err)\n}","preventionTips":["Trim whitespace/newlines from all URL config values before use","Validate BaseURL with url.Parse at provider construction time","Prefer https:// explicitly in configuration"],"tags":["http","url","config","anthropic","streaming"],"backgroundTag":"invalid-base-url","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}