Demo, all content is generated
Question

Vercel 504 FUNCTION_INVOCATION_TIMEOUT when generating a long report with GPT

Solved · 583 views · asked by lucy_hart · edited

Short prompts fine, the "full report" button gives 504: GATEWAY_TIMEOUT Code: FUNCTION_INVOCATION_TIMEOUT after exactly 60 seconds. The OpenAI call takes about 70–90 seconds for the long report. Hobby plan.

What I’ve tried

Added export const maxDuration = 300 which v0 suggested, deployed, still dies. Tried a smaller model, it's faster but the report is worse.

Comment
Is the route streaming or waiting for the full completion? rafa_dev · edited
waiting for the full thing I think lucy_hart · edited

3 answers

Marked as helpful by the asker
rafa_dev · edited

Better fix than chasing a longer timeout: stream it. As long as bytes keep flowing, the user sees the report building up, and it doesn't feel like a 90 second wait.

export const maxDuration = 60; // or whatever your plan allows
export async function POST(req: Request) {
  const completion = await openai.chat.completions.create({ model, messages, stream: true });
  // pipe completion into a ReadableStream and return it
}

If the job is truly long (several minutes, multiple calls), move it out of the request entirely: save a "pending" row, run the work in a background job (Inngest, Trigger.dev, a queue), and poll or subscribe for the result.

Comment
streaming works and honestly it looks cooler this way. thanks! lucy_hart · edited
jonas_k · edited

Check which runtime and plan limits apply to you in the Vercel docs for your account, they've changed several times, and a maxDuration above your plan's max is silently capped. Easiest check: the function's details in the deployment view show the effective max duration.

Comment
the details page says 60s. So maxDuration 300 was just ignored. lucy_hart · edited
abby_ops · edited

If you go the background route: Inngest and Trigger.dev both have free tiers that are plenty for a report button. You get retries and a dashboard too, which beats debugging a timed-out function.

Comment