vxcontrol/pentagi · error · Fatal

failed to build request: %v

Error message

failed to build request: %v

What it means

http.NewRequest(http.MethodPost, tavilyURL, body) failed to construct the request — tavilyURL failed url.Parse or the body reader was invalid; the searcher returns a Fatal error. The context is attached afterwards via req.WithContext(ctx), so this error is purely about URL/body construction.

Source

Thrown at backend/pkg/tools/searchers/tavily.go:126

	reqPayload := tavilyRequest{
		Query:             query,
		ApiKey:            t.apiKey(),
		Topic:             "general",
		SearchDepth:       "advanced",
		IncludeImages:     false,
		IncludeAnswer:     true,
		IncludeRawContent: true,
		MaxResults:        maxResults,
	}
	reqBody, err := json.Marshal(reqPayload)
	if err != nil {
		return "", Fatal(fmt.Errorf("failed to marshal request body: %v", err))
	}

	req, err := http.NewRequest(http.MethodPost, tavilyURL, bytes.NewBuffer(reqBody))
	if err != nil {
		return "", Fatal(fmt.Errorf("failed to build request: %v", err))
	}

	req = req.WithContext(ctx)
	req.Header.Set("Content-Type", "application/json")

	resp, err := client.Do(req)
	if err != nil {
		return "", Retryable(fmt.Errorf("failed to do request: %v", err), 0)
	}
	defer resp.Body.Close()

	return t.parseHTTPResponse(ctx, resp)
}

func (t *tavily) parseHTTPResponse(ctx context.Context, resp *http.Response) (string, error) {
	switch resp.StatusCode {
	case http.StatusOK:
		var respBody tavilySearchResult

View on GitHub (pinned to ea665308ba)

Solutions

  1. Read the parse error in the wrapped message and fix the tavilyURL constant
  2. Ensure scheme is https:// and the URL has no whitespace/newlines
  3. If the URL becomes configurable, validate it at startup rather than failing per-request

Example fix

// before
const tavilyURL = "https://api.tavily.com/search " // trailing space
// after
const tavilyURL = "https://api.tavily.com/search"
Defensive patterns

Strategy: validation

Validate before calling

if u, err := url.Parse(tavilyURL); err != nil || u.Scheme != "https" {
    log.Fatalf("invalid tavily URL: %v", err)
}

Try / catch

req, err := http.NewRequest(http.MethodPost, tavilyURL, bytes.NewBuffer(reqBody))
if err != nil {
    return "", Fatal(fmt.Errorf("failed to build request: %v", err))
}

Prevention

When it happens

Trigger: search() builds the request to the package-constant tavilyURL; fires only when tavilyURL is malformed (e.g. typo in scheme/control chars) or refactoring replaces the constant with a bad dynamic value.

Common situations: Editing the tavilyURL constant with a typo ("https:/api.tavily.com/search" — missing slash, whitespace, or newline picked up from config).

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01). Data as JSON: /api/errors/1e9b7bd07fee5629. Report an issue: GitHub.