Tencent/WeKnora · error

failed to create Zhipu request: %w

Error message

failed to create Zhipu request: %w

What it means

http.NewRequestWithContext failed while building the POST request to the Zhipu API. This occurs when the HTTP method or URL is invalid — with a constant valid method, this almost always means p.baseURL is malformed (unparseable or unsupported scheme).

Source

Thrown at internal/infrastructure/web_search/zhipu.go:137

	if maxResults > maxZhipuResults {
		maxResults = maxZhipuResults
	}

	requestBody := zhipuSearchRequest{
		SearchQuery:  preparedQuery,
		SearchEngine: p.searchEngine,
		SearchIntent: false,
		Count:        maxResults,
		ContentSize:  p.contentSize,
	}
	body, err := json.Marshal(requestBody)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal Zhipu request: %w", err)
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.baseURL, bytes.NewReader(body))
	if err != nil {
		return nil, fmt.Errorf("failed to create Zhipu request: %w", err)
	}
	req.Header.Set("Authorization", "Bearer "+p.apiKey)
	req.Header.Set("Content-Type", "application/json")

	logger.Infof(ctx, "[WebSearch][Zhipu] query=%q maxResults=%d engine=%s", preparedQuery, maxResults, p.searchEngine)
	resp, err := p.client.Do(req)
	if err != nil {
		return nil, fmt.Errorf("failed to execute Zhipu request: %w", err)
	}
	defer resp.Body.Close()

	respBody, err := readZhipuResponseBody(resp.Body)
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != http.StatusOK {
		return nil, zhipuHTTPError(resp.StatusCode, respBody)
	}

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Print/check p.baseURL and confirm it is a valid absolute URL like https://open.bigmodel.cn/api/paas/v4/web_search.
  2. Ensure the URL includes the scheme (https://) and has no whitespace or control characters.
  3. Validate the base URL at provider construction time (url.Parse) so bad config fails fast.
  4. Fix the config source supplying the base URL (env var, config file, or constructor argument).

Example fix

// before
provider.baseURL = os.Getenv("ZHIPU_BASE_URL") // may be empty/malformed
// after
base := os.Getenv("ZHIPU_BASE_URL")
if base == "" { base = defaultZhipuBaseURL }
if _, err := url.Parse(base); err != nil {
    return nil, fmt.Errorf("invalid zhipu base URL: %w", err)
}
provider.baseURL = base
Defensive patterns

Strategy: validation

Validate before calling

func validateBaseURL(raw string) error {
    if raw == "" { return nil } // default applies
    u, err := url.Parse(raw)
    if err != nil || u.Scheme == "" || u.Host == "" {
        return fmt.Errorf("invalid zhipu base URL: %q", raw)
    }
    return nil
}

Type guard

func isValidURL(raw string) bool {
    u, err := url.Parse(raw)
    return err == nil && u.Scheme != "" && u.Host != ""
}

Try / catch

results, err := provider.Search(ctx, q, 5, false)
if err != nil {
    if strings.Contains(err.Error(), "failed to create Zhipu request") {
        return fmt.Errorf("bad baseURL config for Zhipu provider: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling provider.Search when p.baseURL is an invalid URL (missing scheme, control characters, unparseable host), so url.Parse fails inside http.NewRequestWithContext.

Common situations: Misconfigured base URL from config/env (e.g. empty string, missing https://, trailing garbage), user-supplied override URLs not validated at construction, or secrets interpolated incorrectly into the URL.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/a1e3fa6c439c3c47. Report an issue: GitHub.