502 (Bad Gateway) on API

Hello, I’m experiencing issues when calling my API from production site or Postman.
POST https://arteclaw.io/api/questionGenerator 502 (Bad Gateway)
POST https://arteclaw.io/api/questionGenerator
Body:
{
“text”: “La economía circular se basa en un conjunto de principios que buscan minimizar el desperdicio y maximizar el valor de los recursos. En su núcleo, se enfoca en la reutilización, el reciclaje y la reducción de residuos. Estos principios son esenciales para abordar los desafíos ambientales y económicos de la sociedad contemporánea. En este apartado, exploraremos en profundidad estos conceptos, destacando su importancia para la gestión sostenible de recursos y su relación con la creación de la asociación civil que es objeto de estudio.\nEs un modelo de producción y consumo que implica compartir, arrendar, reutilizar, reparar, renovar y reciclar los materiales y productos existentes durante el mayor tiempo posible.\nTiene como objetivo abordar desafíos globales como el cambio climático, la pérdida de biodiversidad, la gestión de los desechos y la contaminación. Se define en contraposición a la economía lineal tradicional.\nLas "3R" representan las tres estrategias fundamentales para la gestión sostenible de los recursos y la reducción de los residuos:\n-\tReducir (Reduce): Se refiere a la reducción del consumo de recursos y la minimización de la generación de residuos. Es el principio que promueve la compra y el uso consciente, evitando el desperdicio desde el principio.\n-\tReutilizar (Reuse): Se refiere a la práctica de extender la vida útil de un producto o material utilizando el mismo varias veces o restaurándolo para su uso continuo.\n-\tReciclar (Recycle): Se refiere al proceso de recolectar, procesar y transformar materiales usados o desechados en nuevos productos o materias primas.\nEstas tres estrategias son clave en la promoción de prácticas sostenibles y la gestión responsable de los recursos y los residuos. Cada una de las "3R" contribuye a la reducción del impacto ambiental y a la conservación de los recursos naturales.”,
“numberOfQuestions”: 3,
“language”: “es”,
“apiKey”: “”,
“moduleName”: “testchamp”
}
{“errorMessage”:“2023-11-23T07:11:37.978Z c9f74ca9-50fc-4aba-9607-08f2b54f7f0a Task timed out after 10.02 seconds”}

There is default 10 second timeout on Functions at Netlify. Because the API is not responding within the 10 second time window, the connection is being closed and a 502 status response is sent.

Our support team can increase this timeout to a recommended maximum of 26 seconds. Please note, a new deploy is required for the new timeout to take effect after the setting is changed. We can also make this timeout setting change specific to one site or make the setting change at account level so it applies to all sites.

​Please let us know if we should increase the timeout, if so - if we should do so for just the site or the whole account, or if there are any other questions.

1 Like

Hello, thank you very much for the response.

I have a question, is it a lately change? My app was working on June 8 and was managing larger api calls (30 seconds and more). I noticed this is a trouble that’s happening nowadays when I updated my openai key in netlify’s settings.

Yes, please. Increase the time so I can test my app again and see if it works.

Thanks in advance.

Netlify always had the 10 sec time as default. Not sure how you managed to use 30 seconds before. For AI stuff, we’d recommend using Edge Functions instead of Functions as they can run for unlimited time: Long-running Edge Functions | Edge Functions on Netlify (edge-functions-examples.netlify.app)

Let us know if you still want the upgraded timelimit, though 26 seconds is also very likely not going to be enough for some AI operations.

2 Likes

Good morning, thanks for the response. Yes, please, increase the time for my entire account. Thank you!!

That needs a Pro or above account. You’re currently on the Starter.

Hello, I implemented a Netlify function as suggested at this address: https://arteclaw.io/.netlify/functions/questionGenerationSlow, I tested the function in dev with Postman with the netlify dev web server and it was okay but I’m getting the same previously mentioned error in production: {“errorMessage”:“2023-11-28T05:28:54.577Z 01c6431a-80c3-4e0b-82b9-5908e66c086d Task timed out after 10.01 seconds”}
This is the function’s code:
import { QuestionGeneratorJob } from “…/…/gpt/question-generator-job.js”;
import { Context, Config } from “@netlify/edge-functions”;
//questionGenerationSlow.mts
function questionGenerationSlow(
language: string,
numberOfQuestions: string,
text: string,
apiKey: string,
moduleName: string
): Promise {
return new Promise(async (resolve, reject) => {
try {
const questionGenerator = QuestionGeneratorJob.create({
logger: console,
});
const answer = await questionGenerator.execute(
language,
numberOfQuestions,
text,
apiKey,
moduleName
);
resolve(JSON.stringify({ answer: answer }));
} catch (error) {
reject(error);
}
});
}

export default async (request: Request, context: Context) => {
try {
const requestBody = await request.text();
const { language, numberOfQuestions, text, apiKey, moduleName } =
JSON.parse(requestBody);

const body = new ReadableStream({
  async start(controller) {
    const response = await questionGenerationSlow(
      language,
      numberOfQuestions,
      text,
      apiKey,
      moduleName
    );
    controller.enqueue(new TextEncoder().encode(response));
    controller.close();
  },
});
return new Response(body, {
  headers: {
    "Content-Type": "application/json",
  },
});

} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: (error as Error).message }),
headers: {
“Content-Type”: “application/json”,
},
};
}
};
What am I missing or why is not working?
Thanks in advance.

Hi it’s probably not working because we didn’t upgrade your function timeout to the max. This is because you are on starter.

So, this is not true: " For AI stuff, we’d recommend using Edge Functions instead of Functions as they can run for unlimited time: Long-running Edge Functions | Edge Functions on Netlify (edge-functions-examples.netlify.app)". Do you offer just 10 seconds for a call from an API, function or edge function in the starter plan?

Hello, please I need a solution on this, I implemented a netlify function expecting to resolve the 10 seconds limitation but it didn’t work, I’m getting the same restriction even using edge functions as suggested. My app is not working, it’s not being able to manage an api call that last more than 10 seconds even with your advice.

Hello, I’m on starter plan so increasing the timeout is not possible. Netlify suggest to create an edge function to avoid this restriction, I implemented it but the restriction remains. Please, read my responses below. Thanks

I don’t see you’ce used Edge Functions at all. You are still using Functions which have the 10-sec limit.

So how this should be implemented? I put my code, a little help would be appreciated. I follow this guide: Long-running Edge Functions | Edge Functions on Netlify

Hi, @arteclaw-io. We cannot write the code itself (we don’t have the bandwidth on the support team to write custom code for customers). However, we can help debug the attempts to deploy this as an edge function.

To this end, would you send us a deploy id for a deploy where you deployed this as an edge function? If you attempted to deploy this as an edge function but the attempt failed, would you tell us what steps you took to deploy this as an edge function?