Gemini-2.5-pro-preview-tts fetch issue

I am using gemini 2.5 pro to generate text-to-speech, but apparently there is always this error when the request time is more than 5 minutes.

I’ve tried to change timeout to 10 minutes by implementing AbortController to global.fetch, but it didn’t work.

Anybody know how to fix it?

1 Like

Hi @Aji_W_Yudhanto ,

Welcome to the Forum!!
Could you please share the minimal reproducible code so that I can try from my end.

@Mrinal_Ghosh this is my code:

import { GoogleGenAI } from "@google/genai";

const originalFetch = global.fetch;

global.fetch = async function (url, options = {}) {
  const timeout = 600000; // 10min
  const controller = new AbortController();
  const id = setTimeout(() => controller.abort(), timeout);

  try {
    return await originalFetch(url, { ...options, signal: controller.signal });
  } finally {
    clearTimeout(id);
  }
};

async generateGeminiTextToSpeech(payload: {
    filename: string;
    text: string;
    speechStyle: string;
    voiceName: string;
  }) {
    const startDate = new Date();
    console.log(
      `generateGeminiTextToSpeech started at: ${startDate.toISOString()}`
    );

    try {
      const text = this.markdownHtml(payload.text);

      const response = await new GoogleGenAI({
      apiKey: API_KEY,
    }).models.generateContent({
        model: "gemini-2.5-pro-preview-tts",
        contents: [
          {
            parts: [
              {
                text: `${payload.speechStyle}: ${text}`,
              },
            ],
          },
        ],
        config: {
          responseModalities: ["AUDIO"],
          speechConfig: {
            voiceConfig: {
              prebuiltVoiceConfig: { voiceName: payload.voiceName },
            },
          },
        },
      });

      const data =
        response.candidates?.[0]?.content?.parts?.[0]?.inlineData?.data;
      const audioBuffer = Buffer.from(data, "base64");

      const fileName = `${payload.filename}.wav`;

      // code to save file

      const endDate = new Date();
      console.log(
        `generateGeminiTextToSpeech completed at: ${endDate.toISOString()}`
      );

      return result;
    } catch (error) {
      const endDate = new Date();
      console.log(
        `generateGeminiTextToSpeech failed at: ${endDate.toISOString()}`
      );
      throw error;
    }
  }

i’ve also tried

new GoogleGenAI({
  apiKey: API_KEY,
  httpOptions: { timeout: 600000 },
})

but nothing’s seems working