Use the documented retry matrix
HTTP 400 means the input failed validation. Correct the media or request rather than resending it unchanged. HTTP 401 requires a valid key, while HTTP 403 requires the appropriate endpoint access. None of these conditions improve through immediate automatic retry.
HTTP 429 should honor Retry-After. HTTP 500 and 503 are the server responses designated for bounded backoff. The wire contract describes up to three retries for server failures; the SDK guides define their own attempt counts and caps.
import os
import time
import requests
API_URL = "YOUR_API_ORIGIN/detect/image"
HEADERS = {"x-api-key": os.environ["NEURALDEFEND_API_KEY"]}
MAX_RETRIES = 3
def detect_image(path):
for attempt in range(MAX_RETRIES + 1):
with open(path, "rb") as media:
response = requests.post(
API_URL, headers=HEADERS, files={"file": media}, timeout=60
)
retries_left = attempt < MAX_RETRIES
if response.status_code == 429 and retries_left:
retry_after = response.headers.get("Retry-After", "")
time.sleep(int(retry_after) if retry_after.isdigit() else 2 ** attempt)
continue
if response.status_code in (500, 503) and retries_left:
time.sleep(2 ** attempt) # 1, 2, then 4 seconds
continue
# 400, 401 and 403 need a fix, not a retry.
response.raise_for_status()
return response.json()["unified_face_authenticity_score"]import { readFile } from "node:fs/promises";
import { basename } from "node:path";
const API_URL = "YOUR_API_ORIGIN/detect/image";
const MAX_RETRIES = 3;
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export async function detectImage(path: string) {
const bytes = await readFile(path);
for (let attempt = 0; ; attempt++) {
const form = new FormData();
form.append("file", new Blob([bytes]), basename(path));
const response = await fetch(API_URL, {
method: "POST",
headers: { "x-api-key": process.env.NEURALDEFEND_API_KEY ?? "" },
body: form,
});
const retriesLeft = attempt < MAX_RETRIES;
if (response.status === 429 && retriesLeft) {
const retryAfter = Number(response.headers.get("Retry-After"));
const seconds = retryAfter > 0 ? retryAfter : 2 ** attempt;
await sleep(seconds * 1000);
continue;
}
if ((response.status === 500 || response.status === 503) && retriesLeft) {
await sleep(2 ** attempt * 1000); // 1, 2, then 4 seconds
continue;
}
// 400, 401 and 403 need a fix, not a retry.
if (!response.ok) {
throw new Error(`NeuroVerify request failed with HTTP ${response.status}`);
}
const body = await response.json();
return body.unified_face_authenticity_score;
}
}Treat ambiguous failures carefully
A timeout or network interruption does not prove that the service failed to process the upload. The Python and TypeScript SDKs do not automatically retry those failures. Before a manual retry, consider duplicate billing and whether the business workflow can reconcile a late or unknown outcome.
A malformed or unexpected response is a protocol failure. Record the available request identifier, update the client if a compatible release exists, and do not reinterpret missing fields as a normal result.
Use the client error surface
The official SDKs distinguish local validation, authentication, scope, rate-limit, server, timeout, network, protocol, and unclassified HTTP errors. The TypeScript client also distinguishes caller cancellation. Catch the most specific type before a general HTTP or base error.
A service business rejection with the documented result envelope resolves as a result. Handle it through status and recovery messaging, not through a generic exception path.
Evidence boundary
Limitations
- This page does not define tenant-specific rate windows, Retry-After values, queueing policy, or a universal retry policy for application-level side effects.
Source record
