From b65c7033a76fc57ef7930258596f27f52ac76f5b Mon Sep 17 00:00:00 2001 From: savecharlie Date: Tue, 30 Jun 2026 13:14:31 -0700 Subject: [PATCH] fix(tools): rustchain_balance crashes on null/non-numeric balance data.get('balance', default) returns None when the key exists with a null value (e.g. unknown/empty wallet), so float(balance) raised TypeError; a non-numeric string raised ValueError. Coerce safely and return a clear message instead of crashing the tool. Co-Authored-By: Iris (Opus 4.8, 1M) --- rustchain_langchain/tools.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/rustchain_langchain/tools.py b/rustchain_langchain/tools.py index 51a68ba..ca94560 100644 --- a/rustchain_langchain/tools.py +++ b/rustchain_langchain/tools.py @@ -71,8 +71,17 @@ def rustchain_balance(wallet_id: str) -> str: Returns balance in RTC tokens. 1 RTC = $0.10 USD reference rate. """ data = _get(f"{RUSTCHAIN_NODE}/balance", params={"miner_id": wallet_id}) - balance = data.get("balance", data.get("amount", 0)) - return f"Wallet {wallet_id}: {balance} RTC (${float(balance) * 0.10:.2f} USD reference)" + raw = data.get("balance") + if raw is None: + raw = data.get("amount", 0) + # The node may return a null/absent/non-numeric balance (e.g. for an unknown + # or empty wallet). Coerce safely so the tool never crashes with TypeError/ + # ValueError on float(None) / float("not-a-number"). + try: + balance = float(raw) + except (TypeError, ValueError): + return f"Wallet {wallet_id}: balance unavailable (node returned {raw!r})" + return f"Wallet {wallet_id}: {balance:g} RTC (${balance * 0.10:.2f} USD reference)" @tool