Fetch failed sending request

Hi all,

I’m trying to use @google/genai javascript library (^0.7.0) to integrate with “gemini-2.0-flash-exp-image-generation”.

I was running the example script from google ai to generate a flying pig:

const { GoogleGenAI } = require("@google/genai");
const fs = require("fs");

const ai = new GoogleGenAI({ apiKey: GEMINI_API_KEY });

async function generateImage() {
  const contents = "Hi, can you create a 3d rendered image of a pig " +
    "with wings and a top hat flying over a happy " +
    "futuristic scifi city with lots of greenery?";

  try {
    // Set responseModalities to include "Image" so the model can generate  an image
    const response = await ai.models.generateContent({
      model: 'gemini-2.0-flash-exp-image-generation',
      contents: contents,
      config: {
        responseModalities: ['Text', 'Image']
      },
    });
    for (const part of response.candidates[0].content.parts) {
      // Based on the part type, either show the text or save the image
      if (part.text) {
        console.log(part.text);
      } else if (part.inlineData) {
        const imageData = part.inlineData.data;
        const buffer = Buffer.from(imageData, 'base64');
        fs.writeFileSync('gemini-native-image.png', buffer);
        console.log('Image saved as gemini-native-image.png');
      }
    }
  } catch (error) {
    console.error("Error generating content:", error);
  }
}

generateImage();

But I keep getting the error of Error: exception TypeError: fetch failed sending request.

Full error:
Error generating content: Error: exception TypeError: fetch failed sending request

    at /Users/jones/Documents/projects/lnl/magicwand/node_modules/@google/genai/dist/node/index.js:7571:19
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Models.generateContent (/Users/jones/Documents/projects/lnl/magicwand/node_modules/@google/genai/dist/node/index.js:6332:20)
    at async generateImage (file:///Users/jones/Documents/projects/lnl/magicwand/backend/services/ai/google/test-gemini.js:16:22)
    at async file:///Users/jones/Documents/projects/lnl/4o-magic/backend/services/ai/google/test-gemini.js:40:3

I’m using node v20.11.1.

Additional informaiton:

I have tried using the following curl call to gemini 2 api:

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=mykey" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
  "contents": [{
    "parts":[{"text": "Explain how AI works"}]
    }]
   }'

It worked well.

Does anyone know what’s causing the issue?

1 Like

If you are in China, you can refer to this answer: TypeError: fetch failed · Issue #29 · google-gemini/generative-ai-js · GitHub

2 Likes

Hi @sebjones ,

Apologies for the delayed response. The old genai-js library is deprecated; please use the new genai-js SDK instead. I’ve tested a code sample with the new SDK that achieves the functionality you’re looking for. You can find it by following example. Thank You!!

1 Like

I’m not 100% sure but it seems that if, while awaiting Gemini client sending request, the process was doing some intensive calculation and thus held on to the CPU for too long, this may happen. My guess is that the IO task was not given a chance to the CPU and this caused some timeout….

If suspects that’s the case, try run tasks in sequence or try cut big tasks small & use e.g. setTimeout to yield…

1 Like

What’s going on with Antigravity? Every message I send shows [Failed to Send]
Pls, fix this

image