Skip to content

Error: TypeError: fetch failed #3238

@Ali1Ammar

Description

@Ali1Ammar

Describe the bug
When running the project, I encounter a TypeError: fetch failed error during a call to the Google Generative AI API. The full error message is:

Error: TypeError: fetch failed
    at node:internal/deps/undici/undici:15482:13
    at async makeRequest (/Users/aliammar/Desktop/project/sajaj-new/ai-generator/node_modules/.pnpm/@[email protected]/node_modules/@google/generative-ai/dist/index.js:397:20)
    at async generateContent (/Users/aliammar/Desktop/project/sajaj-new/ai-generator/node_modules/.pnpm/@[email protected]/node_modules/@google/generative-ai/dist/index.js:867:22)
    at async ChatSession.sendMessage (/Users/aliammar/Desktop/project/sajaj-new/ai-generator/node_modules/.pnpm/@[email protected]/node_modules/@google/generative-ai/dist/index.js:1210:9)
    at async callGemini (/Users/aliammar/Desktop/project/sajaj-new/ai-generator/node_modules/.pnpm/@[email protected][email protected]/node_modules/@genkit-ai/googleai/src/gemini.ts:1348:26)
    at async <anonymous> (/Users/aliammar/Desktop/project/sajaj-new/ai-generator/node_modules/.pnpm/@[email protected][email protected]/node_modules/@genkit-ai/googleai/src/gemini.ts:1407:11)
    at async <anonymous> (/Users/aliammar/Desktop/project/sajaj-new/ai-generator/node_modules/.pnpm/@[email protected]/node_modules/@genkit-ai/core/src/action.ts:533:14)
    at async <anonymous> (/Users/aliammar/Desktop/project/sajaj-new/ai-generator/node_modules/.pnpm/@[email protected]/node_modules/@genkit-ai/core/src/action.ts:406:30)
    at async <anonymous> (/Users/aliammar/Desktop/project/sajaj-new/ai-generator/node_modules/.pnpm/@[email protected]/node_modules/@genkit-ai/core/src/tracing/instrumentation.ts:73:16)
    at async <anonymous> (/Users/aliammar/Desktop/project/sajaj-new/ai-generator/node_modules/.pnpm/@[email protected]/node_modules/@genkit-ai/core/src/tracing/instrumentation.ts:115:24) {
  traceId: '98620aa43cab0d1e794e55bbc720d705',
  ignoreFailedSpan: true
}

To Reproduce
Happen once only but I was using:

  1. Use the chat feature of the genkit.
  2. Send multiple messages (I sent 12 messages).
  3. 11 messages succeeded, but 1 message failed with the error above.

simplify code (the real code had pdf sent in each message and real zod schema

import { googleAI } from "@genkit-ai/googleai";
import { genkit, z } from "genkit/beta";
import "dotenv/config";

const prompt = `
You are a helpful assistant.
`; // your prompt here

const ai = genkit({
  plugins: [googleAI({})],
  model: googleAI.model("gemini-2.5-flash"),
});

(async () => {
  const chat = ai.chat({
    system: [{ text: prompt }, { text: "You are a helpful assistant." }],
  });

  for (let i = 0; i < 15; i++) {
    console.log(`Sending message ${i + 1}...`);
    const quiz = await chat.send({
      prompt: [{ text: "how are you" }, { text: prompt }],
      output: { schema: z.array(z.string()) },
    });
    console.log(quiz.output);
  }
})();

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingjs

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions