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

Next.js调用Google Cloud Vertex AI code-bison时遇INVALID_ARGUMENT错误

排查Next.js API调用Vertex AI code-bison时的INVALID_ARGUMENT错误

问题背景

按照官方Node.js教程在Next.js的API端点中实现调用Google Cloud Vertex AI的code-bison模型功能,已确认promptText参数为有效字符串,但调用predict方法时持续报错:

unhandledRejection: Error: 3 INVALID_ARGUMENT: Request contains an invalid argument.

完整错误栈:

unhandledRejection: Error: 3 INVALID_ARGUMENT: Request contains an invalid argument.
at callErrorFromStatus (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/call.js:31:19)
at Object.onReceiveStatus (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/client.js:192:76)
at Object.onReceiveStatus (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/client-interceptors.js:344:141)
at Object.onReceiveStatus (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/client-interceptors.js:308:181)
at eval (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/resolving-call.js:94:78)
at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
for call at
at ServiceClientImpl.makeUnaryRequest (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/client.js:162:32)
at ServiceClientImpl.eval (webpack-internal:///(rsc)/./node_modules/@grpc/grpc-js/build/src/make-client.js:103:19)
at eval (webpack-internal:///(rsc)/./node_modules/@google-cloud/aiplatform/build/src/v1/prediction_service_client.js:252:33)
at eval (webpack-internal:///(rsc)/./node_modules/google-gax/build/src/normalCalls/timeout.js:42:16)
at OngoingCallPromise.call (webpack-internal:///(rsc)/./node_modules/google-gax/build/src/call.js:64:27)
at NormalApiCaller.call (webpack-internal:///(rsc)/./node_modules/google-gax/build/src/normalCalls/normalApiCaller.js:34:19)
at eval (webpack-internal:///(rsc)/./node_modules/google-gax/build/src/createApiCall.js:75:30)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 3,
details: 'Request contains an invalid argument.',
metadata: Metadata {
  internalRepr: Map(2) {
    'endpoint-load-metrics-bin' => [Array],
    'grpc-server-stats-bin' => [Array]
  },
  options: {}
}
}

核心代码:

import { NextRequest, NextResponse } from "next/server";
import aiplatform from "@google-cloud/aiplatform";
import { authOptions } from "../../../auth/[...nextauth]/route";
import { getServerSession } from "next-auth";
import {helpers} from "@google-cloud/aiplatform";

const {PredictionServiceClient} = aiplatform.v1;
const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};
const predictionServiceClient = new PredictionServiceClient(clientOptions);

export async function POST(request: NextRequest) {
  // 省略部分逻辑,promptText已确认是字符串
  callPredict(promptText);
}

async function callPredict(promptText:string) {
    const endpoint = "https://us-central1-aiplatform.googleapis.com/v1/projects/CORRECT_PROJECT_ID_IS_HERE/locations/us-central1/publishers/google/models/code-bison:predict";
    const prompt = { prefix: promptText };
    const instanceValue = helpers.toValue(prompt);
    const instances = [instanceValue];
    const parameter = { temperature: 0.5, maxOutputTokens: 256 };
    const parameters = helpers.toValue(parameter);
    const request = { endpoint, instances, parameters };
    const [response] = await predictionServiceClient.predict(request);
    const predictions = response.predictions;
    console.log('\tPredictions :');
    for (const prediction of predictions) {
      console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
    }
  }

排查方向与解决方法

1. 修正Endpoint格式

PredictionServiceClient的predict方法要求传入的endpoint是资源路径,而非完整的HTTP URL。原代码中的endpoint包含https://前缀和:predict后缀,这是错误的格式。

修改为:

const endpoint = "projects/CORRECT_PROJECT_ID_IS_HERE/locations/us-central1/publishers/google/models/code-bison";

客户端会自动结合clientOptions中的apiEndpoint拼接正确的请求地址。

2. 修复异步调用未等待问题

原POST函数中调用callPredict时未添加await,会导致未处理的Promise rejection,这也是错误栈中unhandledRejection的来源。

修改POST函数:

export async function POST(request: NextRequest) {
  // 省略部分逻辑,promptText已确认是字符串
  await callPredict(promptText);
  // 必须返回NextResponse,符合Next.js API要求
  return NextResponse.json({ status: "success" });
}

3. 验证参数合法性

code-bison模型的参数范围:

  • temperature:0.0~1.0(当前设置0.5符合要求)
  • maxOutputTokens:1~1024(当前设置256符合要求)
    若参数超出范围也会触发INVALID_ARGUMENT错误,需确保参数值在允许区间内。

4. 检查权限与认证

确保Next.js服务运行时使用的Google Cloud账号具备aiplatform.predictions.predict权限,可通过IAM控制台为账号分配Vertex AI User角色或包含该权限的自定义角色。

修改后的完整核心代码

import { NextRequest, NextResponse } from "next/server";
import aiplatform from "@google-cloud/aiplatform";
import { authOptions } from "../../../auth/[...nextauth]/route";
import { getServerSession } from "next-auth";
import { helpers } from "@google-cloud/aiplatform";

const { PredictionServiceClient } = aiplatform.v1;
const clientOptions = {
  apiEndpoint: 'us-central1-aiplatform.googleapis.com',
};
const predictionServiceClient = new PredictionServiceClient(clientOptions);

export async function POST(request: NextRequest) {
  // 省略部分逻辑,promptText已确认是字符串
  await callPredict(promptText);
  return NextResponse.json({ message: "Prediction completed" });
}

async function callPredict(promptText:string) {
    const endpoint = "projects/CORRECT_PROJECT_ID_IS_HERE/locations/us-central1/publishers/google/models/code-bison";
    const prompt = { prefix: promptText };
    const instanceValue = helpers.toValue(prompt);
    const instances = [instanceValue];
    const parameter = { temperature: 0.5, maxOutputTokens: 256 };
    const parameters = helpers.toValue(parameter);
    const request = { endpoint, instances, parameters };
    const [response] = await predictionServiceClient.predict(request);
    const predictions = response.predictions;
    console.log('\tPredictions :');
    for (const prediction of predictions) {
      console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
    }
  }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 16:55:56