fix(utils): coerce Tavily usage counters to int
JSON APIs sometimes return numeric fields as strings; subtracting them raised TypeError and /status dropped the usage block.
This commit is contained in:
@@ -146,8 +146,8 @@ def _parse_tavily_usage(data: dict[str, Any]) -> SearchUsageInfo:
|
||||
}
|
||||
"""
|
||||
account = data.get("account") or {}
|
||||
used = account.get("plan_usage")
|
||||
limit = account.get("plan_limit")
|
||||
used = _optional_int(account.get("plan_usage"))
|
||||
limit = _optional_int(account.get("plan_limit"))
|
||||
|
||||
# Compute remaining
|
||||
remaining = None
|
||||
@@ -160,9 +160,19 @@ def _parse_tavily_usage(data: dict[str, Any]) -> SearchUsageInfo:
|
||||
used=used,
|
||||
limit=limit,
|
||||
remaining=remaining,
|
||||
search_used=account.get("search_usage"),
|
||||
extract_used=account.get("extract_usage"),
|
||||
crawl_used=account.get("crawl_usage"),
|
||||
search_used=_optional_int(account.get("search_usage")),
|
||||
extract_used=_optional_int(account.get("extract_usage")),
|
||||
crawl_used=_optional_int(account.get("crawl_usage")),
|
||||
)
|
||||
|
||||
|
||||
def _optional_int(value: Any) -> int | None:
|
||||
"""Coerce JSON numerics (including string forms) to int; else None."""
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user