Tencent/WeKnora · error

failed to create request: %w

Error message

failed to create request: %w

What it means

HTTP client error in BaiduProvider.buildRequest: http.NewRequestWithContext could not construct the POST to the Baidu search endpoint after the body was marshaled. The %w wraps the net/http cause; it bubbles up through Search as a request-construction failure, prior to any network activity.

Source

Thrown at internal/infrastructure/web_search/baidu.go:112

func (p *BaiduProvider) buildRequest(ctx context.Context, query string, maxResults int) (*http.Request, error) {
	requestBody := baiduSearchRequest{
		Messages: []baiduMessage{
			{Role: "user", Content: query},
		},
		SearchSource: "baidu_search_v2",
		ResourceTypeFilter: []baiduResourceTypeFilter{
			{Type: "web", TopK: maxResults},
		},
	}

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

	req, err := http.NewRequestWithContext(ctx, "POST", p.baseURL, bytes.NewReader(bodyBytes))
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %w", err)
	}

	req.Header.Set("Authorization", "Bearer "+p.apiKey)
	req.Header.Set("Content-Type", "application/json")

	return req, nil
}

func (p *BaiduProvider) doSearch(ctx context.Context, req *http.Request, includeDate bool) ([]*types.WebSearchResult, error) {
	resp, err := p.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(io.LimitReader(resp.Body, 2<<20)) // 2MB limit
	if err != nil {
		return nil, err

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Verify the configured Baidu baseURL is a valid absolute URL
  2. Check whether the request context was already cancelled before Search was invoked
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/infrastructure/web_search/baidu.go:112 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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