You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用google-cloud-billing-budgets出现ThresholdRule TypeError如何解决

报错原因

  • ThresholdRule的threshold_percent字段要求传入单个浮点/整数类型的数值,你传入了列表[0.9],不符合类型约束,直接触发TypeError。
  • Budget的threshold_rules字段要求传入由ThresholdRule实例组成的列表,即使仅需配置一条阈值规则,也需要用列表包裹单个实例。

修正后代码

# 构造单条阈值规则,threshold_percent传入单个数值而非列表
new_threshold_rule = budgets.ThresholdRule({
    'threshold_percent' : 0.9
})

new_budget_details = budgets.Budget({
    'display_name': projectId,
    'amount': new_amount,
    # threshold_rules传入ThresholdRule实例组成的列表
    'threshold_rules': [new_threshold_rule],
})

new_budget = client.create_budget(
    request = {
        'parent': BILLING_ACCOUNT,
        'budget': new_budget_details,
    }
)

多阈值配置示例

如果需要同时配置多档阈值(比如消耗达50%、90%、100%时分别触发提醒),可以按如下方式编写:

threshold_rules = [
    budgets.ThresholdRule({'threshold_percent': 0.5}),
    budgets.ThresholdRule({'threshold_percent': 0.9}),
    budgets.ThresholdRule({'threshold_percent': 1.0})
]

new_budget_details = budgets.Budget({
    'display_name': projectId,
    'amount': new_amount,
    'threshold_rules': threshold_rules,
})

内容的提问来源于stack exchange,提问作者Pin90

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 06:36:03