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

Vertex AI Predict请求指定自定义serving签名不生效如何解决

解决方案

Vertex AI 标准predict接口的parameters字段默认用于传递自定义预测路由的配置参数,不会将其中的signature_name自动提取到请求体顶层。而TensorFlow Serving的签名识别规则要求signature_name必须与instances平级放在请求体根节点,所以之前的配置不生效,目前有两种成熟方案可以实现需求:

方案1:升级SDK使用官方公开的raw_predict方法

你之前查阅的是旧版本SDK的源码,google-cloud-aiplatform v1.17.0及以上版本已经正式开放了Endpoint的raw_predict接口,示例代码如下:

from google.cloud import aiplatform

# 初始化项目与区域
aiplatform.init(project="你的项目ID", region="us-central1")
endpoint = aiplatform.Endpoint("你的Endpoint资源ID")

# 直接构造完整请求体,signature_name放在顶层
request_body = {
    "signature_name": "name_of_the_signature",
    "instances": [{"instance_0": ["value_0"], "instance_1": ["value_1"]}]
}

response = endpoint.raw_predict(
    request_body=request_body,
    headers={"Content-Type": "application/json"}
)
print(response.json())

方案2:自行封装REST请求(兼容旧版本SDK)

如果暂时无法升级依赖,可以直接调用Vertex AI的原生预测接口,搭配Google Auth库获取身份凭证实现,示例代码如下:

import requests
from google.auth import default
from google.auth.transport.requests import Request

# 自动获取当前环境的身份凭证
credentials, project_id = default()
credentials.refresh(Request())
auth_token = credentials.token

# 拼接Endpoint预测地址,替换为你自己的区域、项目ID、Endpoint ID
endpoint_url = "https://us-central1-aiplatform.googleapis.com/v1/projects/你的项目ID/locations/us-central1/endpoints/你的Endpoint ID:rawPredict"

headers = {
    "Authorization": f"Bearer {auth_token}",
    "Content-Type": "application/json"
}

request_body = {
    "signature_name": "name_of_the_signature",
    "instances": [{"instance_0": ["value_0"], "instance_1": ["value_1"]}]
}

response = requests.post(endpoint_url, headers=headers, json=request_body)
print(response.json())

内容的提问来源于stack exchange,提问作者Artur Lunardi Di Fante

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 03:45:00