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

Vertex AI AutoML端点报错:predict_tabular_classification_sample()遇意外参数'instances'

解决AutoML表格分类预测的TypeError问题

问题原因

你遇到的TypeError: predict_tabular_classification_sample() got an unexpected keyword argument 'instances'错误,是因为新版Google Cloud AutoML Python客户端库(google-cloud-automl)已将预测函数的参数名从instances改为payload,旧示例代码的参数名不再适配当前版本。

解决步骤

  1. 确认客户端库版本
    执行以下命令检查当前安装的版本:

    pip show google-cloud-automl
    

    若版本≥2.0.0,参数名已变更为payload,需按新格式修改代码。

  2. 修改代码参数与结构
    将原代码中的instances参数替换为payload,且payload需按照{"row": {"values": [特征值1, 特征值2, ...]}}的格式传递(特征值顺序需与训练时的列顺序一致)。

    示例代码:

    from google.cloud import automl_v1beta1 as automl
    
    def predict_tabular_classification_sample(
        project: str,
        endpoint: str,
        payload: dict,
        location: str = "us-central1",
        api_endpoint: str = "us-central1-automl.googleapis.com",
    ):
        client_options = {"api_endpoint": api_endpoint}
        client = automl.TablesClient(client_options=client_options)
    
        response = client.predict(
            endpoint=endpoint,
            payload=payload,
        )
    
        # 打印预测结果
        for result in response.payload:
            print(f"预测类别: {result.display_name}")
            print(f"预测置信度: {result.classification.score}")
    
    # 调用示例(替换为你的项目、端点和特征值)
    predict_payload = {"row": {"values": ["示例文本", 45.6, False]}}
    predict_tabular_classification_sample(
        project="你的项目ID",
        endpoint="你的端点ID",
        payload=predict_payload
    )
    

额外提示

如果需要使用旧版参数格式,可降级客户端库到1.x版本,但不推荐——建议优先适配新版API以获得更好的兼容性和功能支持。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 06:24:56