A Bing SERP API error needs a specific diagnosis: invalid request, access or balance problem, temporary rate pressure, upstream failure or an unusable body. Retrying every error can waste the task deadline and may repeat charged operations. Keep the failure reason beside the request instead of returning an empty successful result.
Keep the interface and its error contract clear
SearchCans Bing search uses POST /api/v1/search with t:"bing" and the query in s. After HTTP validation, check business status and data.organic; destination URLs use link. Do not copy another vendor’s engine names, status meanings or response paths. The old Microsoft Bing Search APIs and a current third-party SERP endpoint are different services; a lifecycle change in one does not diagnose an HTTP error from the other.
Classify before retrying
| Observation | Next step |
|---|---|
| 400 or malformed request | Fix parameters; do not repeat unchanged |
| 401/403 or account/balance problem | Check the documented account/access cause; waiting alone may not resolve it |
| 429 | Inspect the provider cause, applicable headers and remaining budget |
| Temporary upstream/transport failure | Retry only within an explicit attempt and task deadline |
| HTTP success but wrong body/schema | Preserve the response and fail validation; do not synthesize results |
SerpApi’s status documentation lists its own 429 causes, including throughput and exhausted searches. New Relic documents its own overload headers. These illustrate why the same HTTP status can need different action; they do not define SearchCans account limits or promise that it returns those headers.
Make the wait decision without sending another request
This pure example accepts an observed HTTP status, an optional Retry-After value and a caller deadline. It does not sleep, send a request or determine whether the provider’s failure is transient. The attempt cap and fallback delays are illustrative application choices.
from datetime import timezone
from email.utils import parsedate_to_datetime
from math import isfinite
def retry_plan(status, retry_after, attempt, remaining_seconds, now):
if (not isinstance(remaining_seconds, (int, float))
or not isfinite(remaining_seconds) or remaining_seconds <= 0):
return {"action": "defer", "reason": "invalid_or_exhausted_deadline"}
if status in (400, 401, 403, 404):
return {"action": "stop_and_fix", "reason": "request_or_access"}
if status not in (429, 500, 502, 503, 504) or attempt >= 3:
return {"action": "stop", "reason": "not_retryable_or_attempt_limit"}
delay = min(2 ** attempt, 30) # illustrative caller policy, not API defaults
if retry_after:
try:
numeric_delay = float(retry_after)
if not isfinite(numeric_delay) or numeric_delay < 0:
raise ValueError("Invalid Retry-After seconds")
delay = numeric_delay
except (ValueError, TypeError, OverflowError):
try:
target = parsedate_to_datetime(retry_after)
if target.tzinfo is None:
target = target.replace(tzinfo=timezone.utc)
delay = max(0.0, (target - now).total_seconds())
except (TypeError, ValueError, OverflowError):
pass
if delay >= remaining_seconds:
return {"action": "defer", "reason": "deadline", "delay_seconds": delay}
return {"action": "retry_if_failure_is_transient", "delay_seconds": delay}
The local fixture stops an unchanged 400, defers a 429 wait that exceeds the task budget, handles an HTTP-date header and enforces the attempt cap. A returned retry decision is conditional: stop if the actual cause is permanent, such as exhausted balance. A cancelled local wait does not establish cancellation of a remote operation or an uncharged retry.
Use the Bing request walkthrough for integration, the broader rate-limit handling guide for shared client policy and client-stage diagnosis when the failed stage is unclear. Record attempts and actual charge outcomes rather than claim that every failure or retry is free.