sipeed/picoclaw · error

create request: %w

Error message

create request: %w

What it means

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.

Source

Thrown at cmd/membench/llm_client.go:122

		}
		// Ollama (0.9+): think field
		thinkFalse := false
		body.Think = &thinkFalse
		// GLM (智谱): thinking field
		body.Thinking = map[string]any{
			"type": "disabled",
		}
	}

	jsonBody, err := json.Marshal(body)
	if err != nil {
		return "", fmt.Errorf("marshal request: %w", err)
	}

	endpoint := strings.TrimRight(c.BaseURL, "/") + "/chat/completions"
	req, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewReader(jsonBody))
	if err != nil {
		return "", fmt.Errorf("create request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")
	if c.APIKey != "" {
		req.Header.Set("Authorization", "Bearer "+c.APIKey)
	}

	var respBody []byte
	var lastErr error
	for attempt := 0; attempt <= c.MaxRetries; attempt++ {
		if attempt > 0 {
			backoff := time.Duration(1<<(attempt-1)) * time.Second // 1s, 2s, 4s, ...
			log.Printf("LLM retry %d/%d after %v: %v", attempt, c.MaxRetries, backoff, lastErr)
			select {
			case <-ctx.Done():
				return "", ctx.Err()
			case <-time.After(backoff):
			}
			// Rebuild request (body reader is consumed)

View on GitHub (pinned to 49183d7e8d)

Solutions

  1. Set BaseURL to a full URL with scheme and host, e.g. http://localhost:11434/v1
  2. Trim whitespace from the configured value before constructing the client
  3. url.Parse the BaseURL at startup and fail fast with a clear message
  4. Ensure a non-nil context.Context is passed through

Example fix

// before
endpoint := strings.TrimRight(c.BaseURL, "/") + "/chat/completions"

// after (fail fast at client construction)
base := strings.TrimSpace(c.BaseURL)
u, err := url.Parse(base)
if err != nil || u.Scheme == "" || u.Host == "" {
    return nil, fmt.Errorf("invalid LLM base URL %q: need scheme and host", c.BaseURL)
}
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(strings.TrimSpace(c.BaseURL))
if err != nil || u.Scheme == "" || u.Host == "" {
    return nil, fmt.Errorf("invalid BaseURL %q: must include scheme and host (e.g. http://localhost:11434/v1)", c.BaseURL)
}
if ctx == nil {
    return nil, errors.New("nil context")
}

Try / catch

req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(jsonBody))
if err != nil {
    return "", fmt.Errorf("create request for %s: %w", endpoint, err) // endpoint in the message = instant diagnosis
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of sipeed/picoclaw@49183d7e8d (2026-08-15). Data as JSON: /api/errors/d59c80dee1357f9c. Report an issue: GitHub.