{"record":{"id":"993de3e98610af5e","repo":"Tencent/WeKnora","slug":"failed-to-unmarshal-zhipu-response-w","errorCode":null,"errorMessage":"failed to unmarshal Zhipu response: %w","messagePattern":"failed to unmarshal Zhipu response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/infrastructure/web_search/zhipu.go","lineNumber":159,"sourceCode":"\n\tlogger.Infof(ctx, \"[WebSearch][Zhipu] query=%q maxResults=%d engine=%s\", preparedQuery, maxResults, p.searchEngine)\n\tresp, err := p.client.Do(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to execute Zhipu request: %w\", err)\n\t}\n\tdefer resp.Body.Close()\n\n\trespBody, err := readZhipuResponseBody(resp.Body)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn nil, zhipuHTTPError(resp.StatusCode, respBody)\n\t}\n\n\tvar response zhipuSearchResponse\n\tif err := json.Unmarshal(respBody, &response); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to unmarshal Zhipu response: %w\", err)\n\t}\n\tif response.Error.Message != \"\" || response.Error.Code != \"\" {\n\t\treturn nil, fmt.Errorf(\"Zhipu API error (%s): %s\", response.Error.Code, response.Error.Message)\n\t}\n\n\tresults := make([]*types.WebSearchResult, 0, len(response.SearchResult))\n\tfor _, item := range response.SearchResult {\n\t\tif strings.TrimSpace(item.Title) == \"\" && strings.TrimSpace(item.Link) == \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tresult := &types.WebSearchResult{\n\t\t\tTitle:   item.Title,\n\t\t\tURL:     item.Link,\n\t\t\tSnippet: item.Content,\n\t\t\tSource:  \"zhipu\",\n\t\t}\n\t\tif includeDate {\n\t\t\tif publishedAt, ok := parseZhipuDate(item.PublishDate); ok {","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/infrastructure/web_search/zhipu.go#L141-L177","documentation":"The Zhipu provider received an HTTP 200 response whose body is not valid JSON matching the zhipuSearchResponse schema. json.Unmarshal failed, so the provider wraps the decode error. This indicates the API returned something other than the expected search-result payload.","triggerScenarios":"Calling Search() when Zhipu returns a 200 with an HTML error page, truncated body, or an unexpected JSON shape (schema change on the API side).","commonSituations":"A gateway/proxy intercepting the response and returning HTML; API version drift changing the response schema; a captive-portal or WAF returning a 200 with non-JSON content.","solutions":["Log the raw response body on this failure to see what was actually returned","Verify you are targeting a current, supported Zhipu API endpoint/version","Check for proxies or gateways rewriting the response body","Add a content-type check before unmarshalling"],"exampleFix":"// before\nrespBody, err := readZhipuResponseBody(resp.Body)\n// after\nrespBody, err := readZhipuResponseBody(resp.Body)\nif err == nil && !json.Valid(respBody) {\n    return nil, fmt.Errorf(\"non-JSON response: %.200s\", respBody)\n}","handlingStrategy":"try-catch","validationCode":"if resp.StatusCode == 200 && !strings.Contains(resp.Header.Get(\"Content-Type\"), \"json\") { return errors.New(\"unexpected content-type from zhipu\") }","typeGuard":"func isUnmarshalError(err error) bool { var ue *json.UnmarshalTypeError; return errors.As(err, &ue) }","tryCatchPattern":"results, err := provider.Search(ctx, q)\nif err != nil && strings.Contains(err.Error(), \"failed to unmarshal Zhipu response\") {\n    log.WithError(err).Warn(\"zhipu returned non-JSON body; check proxy/API version\")\n}","preventionTips":["Pin/verify the Zhipu API endpoint version you target","Log raw bodies on decode failures to diagnose schema drift","Check Content-Type before decoding","Alert on spikes of unmarshal failures (usually infra, not code)"],"tags":["json","unmarshal","zhipu","web-search"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}