cd /news/developer-tools/reject-nonfinite-2captcha-balance-va… · home › topics › developer-tools › article
[ARTICLE · art-174603] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Reject Nonfinite 2Captcha Balance Values Before Storing Them

A developer demonstrated a small validation step for a 2Captcha account adapter that rejects nonfinite balance values before they reach a dashboard database. The Python example uses Decimal parsing plus an is_finite() check to reject NaN, Infinity, -Infinity and malformed strings, while a separate zero check guards against mapping invalid or empty input to a valid amount. The fixture, built on synthetic data, reports four malformed balance values rejected and recommends running it through the same import path as an ordinary row to confirm it does not overwrite the previous balance.

recommendation

Add an is_finite() check after Decimal parsing and test malformed values through the same import path as valid rows to confirm they never overwrite the stored balance.

more in playbook: Verifying model output
by read2 min views2 publishedNov 23, 2026

I’d place a small validation step between a local account adapter for 2Captcha and the dashboard database. Once an amount is parsed, it often gets trusted downstream. A nonfinite value can be accepted by a general-purpose numeric parser and then poison a sum, a chart range, or the comparison that triggers an operational alert.

Written with AI assistance. The Python example uses synthetic data and was checked locally.

Using Decimal preserves useful monetary precision but it still accepts inputs such as NaN and Infinity. Check the parsed value with is_finite() before attempting a range comparison. The rule in this fixture accepts finite decimals only. A separate business rule may apply a bound or forbid negative balances, depending on what the internal field represents.

The example starts with a synthetic adapter value. It makes no claim about values returned by the live account API. In a pipeline I’d keep an invalid-row count and the adapter’s run identifier, then retain the time of the last valid balance shown in the dashboard.

from decimal import Decimal, InvalidOperation

def balance(raw):
    try:
        value = Decimal(raw)
    except InvalidOperation as error:
        raise ValueError("invalid decimal balance") from error
    if not value.is_finite():
        raise ValueError("nonfinite balance")
    return value

assert balance("0") == Decimal("0")
assert balance("0.0001") == Decimal("0.0001")
rejected = 0
for raw in ("NaN", "Infinity", "-Infinity", "bad"):
    try:
        balance(raw)
    except ValueError:
        rejected += 1
assert rejected == 4
print("Four malformed balance values rejected")

The fixture prints Four malformed balance values rejected. The zero check guards against a common mistake where invalid or empty input is mapped to zero, which itself is a valid amount. I’d keep a separate test for a missing field if that is possible in the adapter’s input.

In a real test suite, feed this fixture through the same import call as an ordinary row and assert that it does not replace the previous balance. This connects parser correctness to the behavior the dashboard owner actually sees.

── more in #developer-tools 4 stories · sorted by recency
── more on @2captcha 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/reject-nonfinite-2ca…] indexed:0 read:2min 2026-11-23 · —