Template

Waterfall enrichment function

Last updated

From Section 4.4. The logic of a waterfall, independent of any vendor: try providers in order, stop when a validator accepts a result, and cap spend per record.

The logic of a waterfall, independent of any vendor: try providers in order, stop when a validator accepts a result, and cap spend per record.

providers, validator, and now() are interfaces you supply. This is the control flow, not a client for any particular enrichment API.

def waterfall(record, providers, validator, max_cost):
    spent = 0.0
    attempts = []
    for p in providers:                       # ordered list
        if spent + p.cost_per_call > max_cost:
            break
        if not p.supports(record):            # e.g., region or field not covered
            continue
        result = p.lookup(record)
        spent += p.cost_if_charged(result)    # some providers charge only on hit
        attempts.append((p.name, result))
        if result and validator(result):
            return {
                "value": result.value,
                "source": p.name,
                "confidence": result.confidence,
                "observed_at": now(),
                "cost": spent,
                "attempts": attempts,
            }
    return {"value": None, "cost": spent, "attempts": attempts}