Blog
One stale urn empties a bulk batch, so split the batch instead of retrying it
One urn that no longer resolves empties a bulk call and still bills a credit. Here is how to spot that 502, split the batch, and pick the cheaper fallback.
POST /v1/people/bulk takes 1 to 50 bare entity_urn values and returns each person’s profile
in one call. It bills one credit per profile returned, exactly what fifty single lookups would
cost, and saves the round trips. It has one behavior that a normal retry loop gets wrong: if any
single urn in the batch no longer resolves, nothing comes back for the whole batch.
HTTP/1.1 502 Bad Gateway
X-Credits-Charged: 1
Content-Type: text/plain
the batch could not be resolved. Nothing comes back for the whole batch when any one entity_urn cannot be resolved, so a single stale urn empties the response. Retry with smaller batches to isolate it.
The call is charged one credit, not fifty. Retrying the same fifty urns fails the same way and charges another credit every time. The fix is in the body: split the batch.
Why your retry loop makes it worse
The standing rule on this site is that 429, 502 and 503 are transient and free, so you retry
them with backoff. Which enrichment errors to retry
argues that at length, and for single lookups it holds. A stale-urn 502 on bulk is the
exception: it is not transient, the answer will not change, and it bills.
Five backoff attempts on one bad batch is five credits for zero profiles, and the batch is still stuck.
So the bulk client needs to tell the two 502s apart. The reference documents the ordinary
temporary failure as free and the stale-batch failure as charged one credit, and every response
carries X-Credits-Charged. Read the header:
| Status | X-Credits-Charged | What it means | Do this |
|---|---|---|---|
200 | number of profiles returned | the batch resolved | store the profiles |
502 | 1 | at least one urn is stale | split the batch, do not retry it whole |
502 | 0 | temporary failure | back off and retry the same batch |
400 | 0 | malformed batch | fix the input, never retry |
402 | 0 | the whole batch is not affordable | top up or send a smaller batch |
Parse the header as a number, not a string compare. Credits are decimal across the API, and
"1" and "1.0" should mean the same thing to your code.
Check the input before you send it
Three of the failures are free but avoidable, and catching them locally keeps the split logic simple.
Only bare urns. Bulk takes the bare ACo... value from a profile’s entity_urn. A public
slug, a profile URL, or a urn:li: prefixed string is a 400. Note that the profile also carries
member_urn, which is a different identifier in urn:li:member: form. It is not the one bulk
wants.
No duplicates, no fifty-first entry. A duplicate in the list or more than fifty entries is a
400. Deduplicate and chunk before you call:
const BARE_URN = /^ACo[A-Za-z0-9_-]+$/;
function chunk(urns, size = 50) {
const clean = [...new Set(urns.filter((u) => BARE_URN.test(u)))];
const out = [];
for (let i = 0; i < clean.length; i += size) out.push(clean.slice(i, i + size));
return out;
}
Enough credits for the whole batch. A batch you cannot afford in full is refused with a 402
rather than part-served. GET /v1/credits is free, so check the balance before a run. The pattern
is in check credits before a batch run.
Two ways to find the stale urn
Once a batch comes back as a charged 502, you know at least one urn in it is stale and you do
not know which. There are two reasonable ways to find out, and they trade credits against round
trips.
Split in half and recurse. Send each half as its own bulk call. The half that resolves comes back as profiles; the half that fails gets split again. With one stale urn in fifty, that takes seven failed calls to corner it, one at each level of halving down to a single urn.
Fall back to single lookups. Send every urn in the failed batch to GET /v1/people/profile,
which accepts the bare urn as profile_id. Each good urn costs its one credit as it would have in
bulk, and the stale one comes back as an error on its own, so you see exactly which it is.
Here is the arithmetic for a batch of fifty with one stale urn, ignoring rate limits:
| Strategy | Calls | Credits | Notes |
|---|---|---|---|
| Retry the same batch five times | 6 | 6 | no profiles, still stuck |
| Split in half and recurse | 13 | 49 plus 7 | fewer round trips |
| Single lookups for the failed batch | 51 | 49 plus 1 plus what the stale urn bills | most round trips |
The single-lookup column assumes the stale urn answers as a 404, which bills one credit like any
miss. Either way, the extra cost of finding the urn is a handful of credits, and the cost of not
finding it is the whole batch, every night.
As a rule of thumb: if stale urns are rare in your data, splitting is fine and keeps you well inside the published 10 requests per second. If a batch often has several stale urns, splitting degrades toward single lookups anyway, and the simpler fallback wins.
The split, in code
This version splits on a charged 502, retries a free one, and returns both the profiles and the
urns it could not resolve. It reuses whatever pacing you already have; the one in
enriching a list holds you at 10 rps.
const BASE = 'https://api.triguna.ai/v1';
async function bulkOnce(urns) {
const res = await fetch(`${BASE}/people/bulk`, {
method: 'POST',
headers: { ApiKey: process.env.TRIGUNA_API_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({ urns }),
});
const charged = Number(res.headers.get('X-Credits-Charged') ?? 0);
return { res, charged };
}
async function bulkWithSplit(urns, attempt = 0) {
const { res, charged } = await bulkOnce(urns);
if (res.ok) {
const body = await res.json();
return { profiles: body.profiles, stale: body.unresolved ?? [] };
}
if (res.status === 502 && charged > 0) {
// Stale batch. Retrying it whole would fail again and bill again.
if (urns.length === 1) return { profiles: [], stale: urns };
const mid = Math.ceil(urns.length / 2);
const a = await bulkWithSplit(urns.slice(0, mid));
const b = await bulkWithSplit(urns.slice(mid));
return { profiles: [...a.profiles, ...b.profiles], stale: [...a.stale, ...b.stale] };
}
if ((res.status === 502 || res.status === 503 || res.status === 429) && attempt < 5) {
const wait = Number(res.headers.get('Retry-After') ?? 2 ** attempt);
await new Promise((r) => setTimeout(r, wait * 1000));
return bulkWithSplit(urns, attempt + 1);
}
throw new Error(`bulk ${res.status}: ${await res.text()}`);
}
The same in Python:
import os, time, requests
BASE = "https://api.triguna.ai/v1"
HEADERS = {"ApiKey": os.environ["TRIGUNA_API_KEY"]}
def bulk_with_split(urns, attempt=0):
res = requests.post(f"{BASE}/people/bulk", json={"urns": urns}, headers=HEADERS, timeout=60)
charged = float(res.headers.get("X-Credits-Charged", "0"))
if res.ok:
body = res.json()
return body["profiles"], body.get("unresolved", [])
if res.status_code == 502 and charged > 0:
if len(urns) == 1:
return [], urns
mid = (len(urns) + 1) // 2
p1, s1 = bulk_with_split(urns[:mid])
p2, s2 = bulk_with_split(urns[mid:])
return p1 + p2, s1 + s2
if res.status_code in (429, 502, 503) and attempt < 5:
time.sleep(float(res.headers.get("Retry-After", 2 ** attempt)))
return bulk_with_split(urns, attempt + 1)
raise RuntimeError(f"bulk {res.status_code}: {res.text}")
A successful response also carries requested, returned and unresolved, and
returned + unresolved.length always equals requested. The code above folds unresolved into
the stale list so a successful call that still names an urn it could not resolve is not silently
dropped.
What to do with a stale urn
Finding it is half the job. The other half is making sure tomorrow’s batch does not contain it.
- Mark it, do not delete the person. Record the urn as stale with the date it failed. The person row and its history are still yours.
- Take it out of every future batch. A stale urn left in the nightly set costs a charged
502and a split, every run, forever. - Re-resolve from what else you stored. If you kept the profile URL or public slug, one
GET /v1/people/profilecall by that value tells you whether the person is still findable. If it answers, store theentity_urnit returns and use that from now on. If it is a404, stop asking: a retried404bills every time.
Where this fits in a nightly job
Bulk pays off on the second pass, when you already hold urns from earlier single lookups. In a
nightly backfill, that means: chunk the stored urns into batches of
fifty, check the balance once, run each batch through the split, write profiles as they arrive,
and write the stale list to its own table. The person records guide
covers mapping what comes back, and since each entry in profiles is the same object
/v1/people/profile returns, the mapper you already have reads it unchanged.
Next
- Person API: the profile object bulk returns, field by field.
- Nightly backfill: pacing and budgeting a scheduled re-read.
- Enriching a list: the per-record path and the 10 rps gate.
- Which enrichment errors to retry: the general rule this post makes one exception to.
- About Triguna: why behavior like this gets written down rather than discovered.
- Pricing: what each endpoint costs. Sign up for fifty free credits at app.triguna.ai/signup.