{"record":{"id":"d59c80dee1357f9c","repo":"sipeed/picoclaw","slug":"create-request-w","errorCode":null,"errorMessage":"create request: %w","messagePattern":"create request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/membench/llm_client.go","lineNumber":122,"sourceCode":"\t\t}\n\t\t// Ollama (0.9+): think field\n\t\tthinkFalse := false\n\t\tbody.Think = &thinkFalse\n\t\t// GLM (智谱): thinking field\n\t\tbody.Thinking = map[string]any{\n\t\t\t\"type\": \"disabled\",\n\t\t}\n\t}\n\n\tjsonBody, err := json.Marshal(body)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"marshal request: %w\", err)\n\t}\n\n\tendpoint := strings.TrimRight(c.BaseURL, \"/\") + \"/chat/completions\"\n\treq, err := http.NewRequestWithContext(ctx, \"POST\", endpoint, bytes.NewReader(jsonBody))\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"create request: %w\", err)\n\t}\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\tif c.APIKey != \"\" {\n\t\treq.Header.Set(\"Authorization\", \"Bearer \"+c.APIKey)\n\t}\n\n\tvar respBody []byte\n\tvar lastErr error\n\tfor attempt := 0; attempt <= c.MaxRetries; attempt++ {\n\t\tif attempt > 0 {\n\t\t\tbackoff := time.Duration(1<<(attempt-1)) * time.Second // 1s, 2s, 4s, ...\n\t\t\tlog.Printf(\"LLM retry %d/%d after %v: %v\", attempt, c.MaxRetries, backoff, lastErr)\n\t\t\tselect {\n\t\t\tcase <-ctx.Done():\n\t\t\t\treturn \"\", ctx.Err()\n\t\t\tcase <-time.After(backoff):\n\t\t\t}\n\t\t\t// Rebuild request (body reader is consumed)","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/sipeed/picoclaw/blob/49183d7e8daed0dba89ddbb6fcb60089401d9680/cmd/membench/llm_client.go#L104-L140","documentation":"http.NewRequestWithContext failed while constructing the POST to <BaseURL>/chat/completions (llm_client.go:122). New errors almost always mean the URL string is invalid — missing scheme, spaces, control characters — or ctx is nil. This fails before any network I/O, so it is a configuration error, not a connectivity one.","triggerScenarios":"BaseURL like 'localhost:11434' (no http://), 'http://local host:11434', trailing garbage after env expansion, or an unset BaseURL producing a relative path '/chat/completions'; passing a nil ctx.","commonSituations":"OLLAMA/OpenAI-compatible endpoint set via env var with quotes or whitespace; YAML config value missing the scheme; copy-pasting a URL with a trailing space; BaseURL default changing between versions.","solutions":["Set BaseURL to a full URL with scheme and host, e.g. http://localhost:11434/v1","Trim whitespace from the configured value before constructing the client","url.Parse the BaseURL at startup and fail fast with a clear message","Ensure a non-nil context.Context is passed through"],"exampleFix":"// before\nendpoint := strings.TrimRight(c.BaseURL, \"/\") + \"/chat/completions\"\n\n// after (fail fast at client construction)\nbase := strings.TrimSpace(c.BaseURL)\nu, err := url.Parse(base)\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return nil, fmt.Errorf(\"invalid LLM base URL %q: need scheme and host\", c.BaseURL)\n}","handlingStrategy":"validation","validationCode":"u, err := url.Parse(strings.TrimSpace(c.BaseURL))\nif err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return nil, fmt.Errorf(\"invalid BaseURL %q: must include scheme and host (e.g. http://localhost:11434/v1)\", c.BaseURL)\n}\nif ctx == nil {\n    return nil, errors.New(\"nil context\")\n}","typeGuard":null,"tryCatchPattern":"req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(jsonBody))\nif err != nil {\n    return \"\", fmt.Errorf(\"create request for %s: %w\", endpoint, err) // endpoint in the message = instant diagnosis\n}","preventionTips":["Always include the scheme in BaseURL (http:// or https://)","url.Parse-verify BaseURL once at client construction, not per request","Trim env-var-provided URLs to kill trailing whitespace/newlines","Never pass a nil context to NewRequestWithContext"],"tags":["go","http","url","llm-client","config"],"backgroundTag":null,"analyzedSha":"49183d7e8daed0dba89ddbb6fcb60089401d9680","analyzedAt":"2026-08-15T21:55:41.315Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}