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
- Inspect the wrapped error to identify which field cannot be marshalled.
- Remove or fix struct fields with unmarshalable types (channel, func, circular pointers).
- Check any custom MarshalJSON methods on requestBody or its fields for error paths.
- 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
- Do not add channels/funcs/cyclic references to request structs
- Avoid error-returning custom MarshalJSON on request types
- Keep the request struct identical to upstream
- Add a unit test marshalling the request body
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
- failed to marshal Metaso request: %w
- failed to unmarshal Zhipu response: %w
- failed to set FAQ metadata: %w
- marshal credentials: %w
- marshal credentials: %w
AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02).
Data as JSON: /api/errors/2a760287ec152707.
Report an issue: GitHub.