Skip to content

504 Gateway Timeout for image generation when using Hugging Face API with Vercel serverless functions #2

Description

@HOTHEAD01TH

When calling the Hugging Face Inference API (e.g., for Stable Diffusion image generation) from a Vercel serverless function, I'm consistently receiving 504 Gateway Timeout errors.

Root Cause

After investigation, it appears that the Hugging Face model's inference time sometimes exceeds the execution timeout limit imposed by Vercel's serverless infrastructure. This results in the Vercel edge function terminating before Hugging Face can respond.

Why this happens

Vercel’s serverless functions are optimized for fast, low-latency responses. Long-running API calls (like image generation or large model inference) can easily exceed the timeout threshold (typically 10 seconds on the free plan, and ~60 seconds on Pro).

Steps to Reproduce

  1. Deploy a Next.js (or similar) app on Vercel
  2. Add an API route that sends a POST request to Hugging Face’s Stable Diffusion endpoint
  3. Deploy and hit the endpoint from the frontend
  4. Observe the 504 response after ~10–60 seconds

Possible Solutions

  • Move inference calls to a backend with longer execution time (e.g., AWS Lambda with extended timeout, or a dedicated server).
  • Queue the request and poll for the result after the function returns.
  • Cache Hugging Face model in a managed runtime or use a lightweight model for faster responses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingwontfixThis will not be worked on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions