Skip to content

sendMessage/ sendMessageStream returns empty response when specific systemInstruction is used with googleSearch tool #1321

@renewboy

Description

@renewboy

Bug Report: sendMessage/ sendMessageStream return empty response when a specific systemInstruction is used with googleSearch tool

Description

I am experiencing an issue with the @google/genai SDK where sendMessageStream returns an empty response when both systemInstruction and googleSearch tool are configured.

Key Findings:

  1. googleSearch + systemInstruction: The stream async iterator never yields. The loop for await (const chunk of result) is never entered.
  2. googleSearch ONLY: If I remove systemInstruction, the API works correctly.
  3. urlContext: Adding urlContext also seems to cause 100% empty responses, similar to this issue.
  4. sendMessage: When I switch to the non-streaming sendMessage method, it also returns an empty response with "finishReason": "STOP".

Reproduction Code

I am using @google/genai (version ^1.41.0).

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

const API_KEY = process.env.GEMINI_API_KEY;
const genAI = new GoogleGenAI({ apiKey: API_KEY });

async function run() {
  try {
    const systemInstruction = `...`; // (Long system prompt)

    // Scenario 1: FAILS (Stream loop is not entered)
    const chat = genAI.chats.create({
      model: "gemini-2.5-flash",
      config: {
        systemInstruction: systemInstruction, // <--- Presence of this causes the issue when combined with tools
        tools: [
          { googleSearch: {} },
          // { urlContext: {} }  // <--- Adding this causes 100% empty responses
        ],
      }
    });

    // Scenario 2: WORKS (If systemInstruction is removed)
    /*
    const chat = genAI.chats.create({
      model: "gemini-2.5-flash",
      config: {
        // systemInstruction: systemInstruction, // <--- Commenting this out fixes the issue
        tools: [
          { googleSearch: {} },
        ],
      }
    });
    */

    const message = "...";

    console.log(`User: ${message}\n`);

    const result = await chat.sendMessageStream({ message });

    console.log("Stream started...");
    
    // PROBLEM: This loop is NEVER entered in Scenario 1
    for await (const chunk of result) {
      const chunkText = chunk.text(); 
      process.stdout.write(chunkText);
    }
    
    console.log(`\n\n[Stream ended]`);
    
  } catch (error) {
    console.error("\nError:", error);
  }
}

run();

Environment

  • SDK: @google/genai@1.41.0
  • OS: macOS

Full Reproduction Code

gemini_stream_demo.js

Metadata

Metadata

Assignees

Labels

api:gemini-apipriority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions