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

Vertex AI端点调用JSON payload无效,求正确编码方案

解决Vertex AI部署TensorFlow推荐模型的Payload编码问题

先明确模型输入签名(对照saved_model_cli输出)

先从saved_model_cli show --dir <你的模型路径> --all的输出里抓关键信息——重点看serving_default签名下的输入定义。比如你本地用["doctor"]就能跑通,说明模型接受的是字符串列表,对应的签名输入大概率是DT_STRING类型,形状为(-1)(可变长度的一维张量)。

举个常见的签名输出示例:

signature_def['serving_default']:
  输入部分:
    inputs['input_text'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: serving_default_input_text:0
  输出部分:
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 10)
        name: StatefulPartitionedCall:0
    outputs['titles'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 10)
        name: StatefulPartitionedCall:1

正确的curl请求Payload格式

Vertex AI的预测端点要求JSON payload必须用{"instances": [输入数据]}的结构,结合你的本地调用场景,正确格式分两种情况:

情况1:输入是一维字符串列表(对应形状(-1))

直接把你的输入值放进instances数组里:

{"instances": ["doctor"]}

对应的curl命令:

curl -X POST -H "Content-Type: application/json" -d '{"instances": ["doctor"]}' <你的Vertex AI预测端点URL>

情况2:输入签名要求二维张量(比如形状(1,))

如果saved_model_cli显示输入形状是(1,),就把输入再套一层数组:

{"instances": [["doctor"]]}

特殊情况:输入有指定键名

如果签名里的输入不是默认命名,而是有明确的键(比如上面示例里的input_text),就要用键值对格式:

{"instances": [{"input_text": "doctor"}]}

解决Python调用结果不符的问题

Python调用成功但结果不对,基本是输入格式和本地测试不一致导致的:

  • 检查你的Python代码,确保输入结构和本地loaded(["doctor"])完全匹配,比如正确的调用代码应该是:
    import requests
    import json
    
    url = "<你的Vertex AI预测端点URL>"
    headers = {"Content-Type": "application/json"}
    payload = {"instances": ["doctor"]}
    response = requests.post(url, headers=headers, data=json.dumps(payload))
    result = response.json()
    # 解析结果时要对应模型输出的键,比如result["predictions"][0]["scores"]和result["predictions"][0]["titles"]
    print(result)
    
  • 用saved_model_cli run --dir <模型路径> --tag_set serve --signature_def serving_default --input_exprs "input_text=['doctor']"模拟API调用的输入逻辑,对比本地输出和这个命令的输出,确认是否一致。

400错误排查补充

如果还是报无效JSON:

  • 检查payload的语法:引号是否配对、有没有多余逗号
  • 确认请求头Content-Type确实是application/json
  • 核对端点URL是否完整,必须包含预测路径(比如/v1/models/你的模型名:predict)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 08:09:18