{"record":{"id":"060ebf26cf521305","repo":"micro/go-micro","slug":"failed-to-create-request-w-060ebf","errorCode":null,"errorMessage":"failed to create request: %w","messagePattern":"failed to create request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ai/together/together.go","lineNumber":167,"sourceCode":"// round is a model call plus the tools it asks for, so this is the ceiling on\n// one question's cost as well as its length; it is high enough that no honest\n// piece of multi-step work reaches it.\nconst maxToolRounds = 12\n\nfunc (p *Provider) Stream(ctx context.Context, req *ai.Request, opts ...ai.GenerateOption) (ai.Stream, error) {\n\treturn openaiapi.Stream(ctx, p.opts, req, \"/v1/chat/completions\")\n}\n\nfunc (p *Provider) callAPI(ctx context.Context, req map[string]any) (*ai.Response, map[string]any, 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, \"/\") + \"/v1/chat/completions\"\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\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\thttpReq.Header.Set(\"Authorization\", \"Bearer \"+p.opts.APIKey)\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, ai.NewHTTPError(httpResp, respBody)\n\t}\n\n\tvar chatResp struct {\n\t\tChoices []struct {","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/ai/together/together.go#L149-L185","documentation":"http.NewRequestWithContext failed while building the POST to <BaseURL>/v1/chat/completions in the Together provider. With a valid bytes.Reader body, this points to URL parsing/validation failure of the assembled API URL.","triggerScenarios":"strings.TrimRight(p.opts.BaseURL, \"/\") + \"/v1/chat/completions\" produces a URL http.NewRequestWithContext cannot parse: BaseURL containing spaces, newlines, quotes, or invalid percent-encodings from config/env.","commonSituations":"TOGETHER base-URL env var with trailing newline/whitespace, copy-pasted URL including quotes, templated config injecting characters into the host.","solutions":["Inspect the exact BaseURL string for hidden whitespace/quotes and TrimSpace it","Ensure BaseURL is a clean absolute https URL (e.g. https://api.together.xyz)","Add a startup config check that url.Parse succeeds on the base URL before use","Log the fully assembled apiURL when this error occurs"],"exampleFix":"// before\nbaseURL := os.Getenv(\"TOGETHER_BASE_URL\") // may contain \"\\n\"\n// after\nbaseURL := strings.TrimSpace(os.Getenv(\"TOGETHER_BASE_URL\"))","handlingStrategy":"validation","validationCode":"func validateTogetherURL(raw string) error {\n    raw = strings.TrimSpace(raw)\n    u, err := url.Parse(raw)\n    if err != nil {\n        return fmt.Errorf(\"invalid Together BaseURL: %w\", err)\n    }\n    if u.Scheme != \"https\" || u.Host == \"\" {\n        return fmt.Errorf(\"Together BaseURL must be absolute https URL, got %q\", raw)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"resp, err := provider.Generate(ctx, req)\nif err != nil && strings.Contains(err.Error(), \"failed to create request\") {\n    return fmt.Errorf(\"malformed Together BaseURL: %w\", err)\n}","preventionTips":["TrimSpace URL values read from env/config","Reject unquoted copy-pasted URLs during config load","Run url.Parse validation at provider construction time","Log the assembled apiURL on request-creation errors"],"tags":["http","url-validation","together","chat"],"backgroundTag":"invalid-request-url","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}