Tencent/WeKnora · error

failed to marshal Zhipu request: %w

Error message

failed to marshal Zhipu request: %w

What it means

json.Marshal failed while serializing the Zhipu web search request body. In practice this is nearly impossible with the plain request struct (all string/int/bool fields), so it usually indicates a defective custom struct or an unexpected marshalling environment issue.

Source

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

		logger.Infof(ctx, "[WebSearch][Zhipu] truncated query to %d characters", maxZhipuQueryRunes)
	}
	if maxResults <= 0 {
		maxResults = defaultZhipuResults
	}
	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 {

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Inspect the wrapped error to identify which field cannot be marshalled.
  2. Remove or fix struct fields with unmarshalable types (channel, func, circular pointers).
  3. Check any custom MarshalJSON methods on requestBody or its fields for error paths.
  4. Compare against upstream zhipu.go — this should not occur with the stock struct.
Defensive patterns

Strategy: try-catch

Validate before calling

// stock struct cannot fail; only needed if you customize the request type:
if _, err := json.Marshal(myRequestBody); err != nil {
    return fmt.Errorf("request body not marshalable: %w", err)
}

Try / catch

results, err := provider.Search(ctx, q, 5, false)
if err != nil {
    if strings.Contains(err.Error(), "failed to marshal Zhipu request") {
        return fmt.Errorf("unexpected marshal failure; check custom request fields: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling provider.Search when json.Marshal of the requestBody struct returns an error — theoretically only if the struct gains unsupported field types (e.g. a channel, func, or a MarshalJSON method that errors).

Common situations: Custom forks adding fields with unmarshalable types (channels, funcs, cycles), custom MarshalJSON implementations returning errors, or vendored code drift.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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