generateContent() produces empty text output
Description of the bug:
when using generateContent, the value of output.candidates[0].content.parts[0].text is sometimes empty. See
Actual vs expected behavior:
async function expected() {
const genAI = new GoogleGenerativeAI(process.env.GEMINI);
const MODEL = "gemini-2.0-flash-001";
const { response: output } = await genAI.getGenerativeModel({
model: MODEL,
systemInstruction: "you are a helpful assistant.",
generationConfig: {
maxOutputTokens: 1000,
temperature: 0,
}
}).generateContent("what is the capital of france?");
console.log(output.candidates[0].content.parts[0].text); // expected The capital of France is Paris.
}
async function actual() {
const genAI = new GoogleGenerativeAI(process.env.GEMINI);
const MODEL = "gemini-2.0-flash-001";
const { response: output } = await genAI.getGenerativeModel({
model: MODEL,
systemInstruction: "you are a helpful assistant.",
generationConfig: {
maxOutputTokens: 1000,
temperature: 0,
}
}).generateContent("what is the capital of france?");
console.log(output.candidates[0].content.parts[0].text); // actual "".
}
Any other information you'd like to share?
I'm struggling to reproduce this error consistently. Sometimes it works on retries. There is no indication from logs that we hit an api rate limit
I'm also encountering this issue.
This happens on both Vertex and AI studio for me.
When the text is empty, does the response have the .promptFeedback field set? Or any other field?
Hey @IvanLH I’d like to contribute a fix for this issue. Is this still open for external contributions?
Hi @anishadoni I tried this out in my terminal and it dint showed my null output anywhere.
import {GoogleGenerativeAI} from '@google/generative-ai'
async function expected() {
const genAI = new GoogleGenerativeAI('AIzaSyAzgvqgie9VoOuj0nLm8RmqN-LKmwIdruM');
const MODEL = "gemini-2.0-flash-001";
const { response: output } = await genAI.getGenerativeModel({
model: MODEL,
systemInstruction: "you are a helpful assistant.",
generationConfig: {
maxOutputTokens: 1000,
temperature: 0,
}
}).generateContent("what is the capital of france?");
console.log(output.candidates[0].content.parts[0].text==""?"NULL OUTPUT": output.candidates[0].content.parts[0].text); // expected The capital of France is Paris.
}
setInterval(expected, 6000)
I faced the same issue in my project. My prompt size was around 35k, and I usually ran the code only two or three times a day to test a few analysis parts.
Sometimes it returned a value, but other times it returned null. Even when I switched the API key to another account after getting a null response, I still kept receiving null values.
For context, I’m on the free tier.