使用Node.js调用Vertex AI端点报错:无法解析输入实例
Node.js调用Vertex AI自定义双塔检索模型输入解析错误的解决思路
核心问题定位
你的Node.js代码中输入实例的嵌套层级错误,导致Vertex AI无法正确解析请求负载:
- Python代码里的
instances_list是包含单个实例对象的数组:[{ "input_1": [分词数组] }] - Node.js代码错误地将实例对象再包裹一层数组,最终
instances变成[ [{ "input_1": [分词数组] }] ],多了一层不必要的嵌套,这是触发INVALID_ARGUMENT错误的直接原因。
修正步骤
修改Node.js中实例构造的代码,对齐Python的请求结构:
async function predictCustomTrainedModel() { const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; // 直接构造单个实例对象,无需外层额外数组 const instance = {"input_1": "disenador grafico experiencia montaje planchas".split()}; // instances是包含实例对象的数组,和Python结构完全一致 const instances = [instance]; const request = { endpoint, instances }; try { const response = await predictionServiceClient.predict(request); console.log('Predict custom trained model response'); console.log(`\tDeployed model id : ${response.deployedModelId}`); const predictions = response.predictions; console.log('\tPredictions :'); for (const prediction of predictions) { console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`); } } catch (err) { console.error('Prediction error:', err); } }
额外排查方向
如果修正后仍报错,可从以下维度排查:
- 数据类型一致性:确认
input_1的取值为字符串数组,和模型训练时的输入格式完全匹配 - 客户端版本兼容性:确保
@google-cloud/aiplatform包的版本与Python端google-cloud-aiplatform版本尽量接近,避免API差异导致的解析问题 - 凭证权限验证:检查Node.js中
credentials的配置是否正确,确认账号拥有目标端点的访问权限 - 请求负载对比:将Node.js构造的
instances序列化为JSON,和Python端打印的请求负载直接对比,确保结构、数据完全一致
内容的提问来源于stack exchange,提问作者razimbres
相关产品推荐
相关产品推荐

