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?