Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by MeteoricTraveler187

Why does instantiating PredictionServiceClient in my Next.js API route lead to hanging and time-outs?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm using an external function in my Next.js app to instantiate the Google Cloud AI Platform's PredictionServiceClient, but the call hangs and eventually times out without providing any clear error messages.

Here's the code I'm using:

TYPESCRIPT
import { v1beta1 } from '@google-cloud/aiplatform'; export function GET(request: Request) { const client = new v1beta1.PredictionServiceClient({ apiEndpoint: 'europe-west3-aiplatform.googleapis.com' }); return new Response(JSON.stringify('ok')); }

I do use OIDC for authentication, but even passing an authClient does not solve the issue—it still times out. What configuration or runtime adjustments might be necessary to resolve this problem?

0

Comments 0

Answers 2

Views 38

2 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

When using libraries such as the Google Cloud AI Platform client that rely on Node’s gRPC under the hood, make sure your route runs in a Node.js environment rather than the Edge runtime. By default, if your Next.js external function (route handler) is run on the Edge runtime, networking and binary dependencies like gRPC might hang or not work properly.

To fix the issue, force your route handler to use the Node.js runtime by adding:

TYPESCRIPT
export const runtime = 'nodejs';

So your file would look like this:

TYPESCRIPT
import { v1beta1 } from '@google-cloud/aiplatform'; export const runtime = 'nodejs'; export function GET(request: Request) { const client = new v1beta1.PredictionServiceClient({ apiEndpoint: 'europe-west3-aiplatform.googleapis.com' }); return new Response(JSON.stringify('ok')); }

This tells Next.js to run your handler on Node.js, which fully supports the underlying gRPC libraries and should resolve the time-out issue.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by StarProbe799 1 month ago

0

There are couple of things:

  1. try to use the v1 (GA) API endpoint.
  2. Typically PredictionServiceClient operates in background, your handler will likely be an async call.
  3. Your sample code seems only showing instantiate PredictionServiceClient but not making actual predict calls.

Here is a sample code to try:

JAVASCRIPT
import { PredictionServiceClient } from '@google-cloud/aiplatform'; export default async function handler(req, res) { const client = new v1.PredictionServiceClient({apiEndpoint: 'europe-west3-aiplatform.googleapis.com'}); // Define your prediction request object // including actual model endpoint, instance configuration and other parameters const request = { endpoint, instances, parameters, }; try { const predictionResponse = await client.predict(request); // Your actual Vertex AI prediction call res.status(200).json({ result: 'ok', prediction: predictionResponse}); // Send JSON response } catch (error) { console.error("Error during prediction:", error); res.status(500).json({ error: 'Prediction failed.' }); // Send error response as JSON } }

No comments yet.

Discussion

No comments yet.