Tencent/WeKnora · error

bing API returned status %d: %s

Error message

bing API returned status %d: %s

What it means

The Bing API responded with a non-200 HTTP status; the provider surfaces the status code plus the response body. This is an upstream Bing Web Search API failure (auth, quota, bad params, or service issue) translated into a Go error by doSearch.

Source

Thrown at internal/infrastructure/web_search/bing.go:111

	logger.Infof(ctx, "[WebSearch][Bing] returned %d results", len(results))
	return results, nil
}

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

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	if resp.StatusCode != http.StatusOK {
		logger.Warnf(ctx, "[WebSearch][Bing] API returned status %d: %s", resp.StatusCode, string(body))
		return nil, fmt.Errorf("bing API returned status %d: %s", resp.StatusCode, string(body))
	}

	var respData bingSearchResponse
	if err := json.Unmarshal(body, &respData); err != nil {
		return nil, fmt.Errorf("failed to unmarshal response: %w", err)
	}
	results := make([]*types.WebSearchResult, 0, len(respData.WebPages.Value))
	for _, item := range respData.WebPages.Value {
		results = append(results, &types.WebSearchResult{
			Title:       item.Name,
			URL:         item.URL,
			Snippet:     item.Snippet,
			Source:      "bing",
			PublishedAt: &item.DateLastCrawled,
		})
	}
	return results, nil
}

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Read the status code and body in the error message: 401/403 -> verify the Azure subscription key and Bing Search resource entitlement.
  2. 429 -> back off and retry later, or upgrade the pricing tier.
  3. 400 -> check query length, count parameter, and market/language params against the Bing API docs.
  4. 5xx -> retry with exponential backoff; check Azure status page.
  5. Note Bing Search v7 was retired (Aug 2025); migrate to an alternative provider if the resource no longer exists.
Defensive patterns

Strategy: retry

Validate before calling

// preflight: verify key is configured and non-empty
if strings.TrimSpace(bingAPIKey) == "" {
    return errors.New("missing BING_API_KEY")
}

Type guard

type HTTPStatusError interface{ error; StatusCode() int }
func asStatusErr(err error) (int, bool) {
    var se HTTPStatusError
    if errors.As(err, &se) { return se.StatusCode(), true }
    return 0, false
}

Try / catch

results, err := provider.Search(ctx, query, 10, false)
if err != nil {
    if strings.Contains(err.Error(), "status 429") || strings.Contains(err.Error(), "status 5") {
        // exponential backoff retry
    } else if strings.Contains(err.Error(), "status 401") || strings.Contains(err.Error(), "status 403") {
        // fatal: fix credentials/entitlement, do not retry
    }
}

Prevention

When it happens

Trigger: Bing returns 401 (invalid Ocp-Apim-Subscription-Key), 403 (no entitlement), 429 (rate limit exceeded), 400 (bad parameters), or 5xx during a Search call.

Common situations: Expired or wrong Azure Cognitive Services key; exhausted monthly Bing API quota; deprecated/retired Bing Search v7 resource; regional endpoint mismatch in baseURL.

Related errors


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