Tencent/WeKnora · error

failed to marshal request body: %w

Error message

failed to marshal request body: %w

What it means

Returned by buildRequest when json.Marshal of the {query, max_results} map fails. With these plain value types this is effectively a defensive guard; it could only fire via invalid UTF-8 in the query or a future change to the payload type.

Source

Thrown at internal/infrastructure/web_search/ollama.go:96

	}
	results, err := p.doSearch(ctx, req)
	if err != nil {
		logger.Warnf(ctx, "[WebSearch][Ollama] failed: %v", err)
		return nil, err
	}
	logger.Infof(ctx, "[WebSearch][Ollama] returned %d results", len(results))
	return results, nil
}

func (p *OllamaProvider) buildRequest(ctx context.Context, query string, maxResults int) (*http.Request, error) {
	requestBody := map[string]interface{}{
		"query":       query,
		"max_results": 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")
	req.Header.Set("User-Agent", defaultUserAgentHeader)

	return req, nil
}

func (p *OllamaProvider) doSearch(ctx context.Context, req *http.Request) ([]*types.WebSearchResult, error) {
	resp, err := p.client.Do(req)
	if err != nil {
		return nil, err

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Sanitize the query string (valid UTF-8) before searching
  2. Review changes to the request payload if this starts firing
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/infrastructure/web_search/ollama.go:96 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/f7581255a5bb14de. Report an issue: GitHub.