google / generative-ai-docs

Documentation for Google's Gen AI site - including the Gemini API and Gemma
https://ai.google.dev
Apache License 2.0
1.46k stars 498 forks source link

Gemini API multi-turn conversations (chat) | Typeerror for text #413

Open ayush7801 opened 1 month ago

ayush7801 commented 1 month ago

Hi, I am getting this typeerror for text key being undefined even when I am requesting with the same request body as in documentation. I am using NodeJS.

Code given in documentation - docs

const { GoogleGenerativeAI } = require("@google/generative-ai");

// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);

async function run() {
  // For text-only input, use the gemini-pro model
  const model = genAI.getGenerativeModel({ model: "gemini-pro"});

  const chat = model.startChat({
    history: [
      {
        role: "user",
        parts: [{ text: "Hello, I have 2 dogs in my house." }],
      },
      {
        role: "model",
        parts: [{ text: "Great to meet you. What would you like to know?" }],
      },
    ],
    generationConfig: {
      maxOutputTokens: 100,
    },
  });

  const msg = "How many paws are in my house?";

  const result = await chat.sendMessage(msg);
  const response = await result.response;
  const text = response.text();
  console.log(text);
}

run();

Response I am getting - Cannot read properties of undefined (reading 'text') , with response code 500.

Your help is appriciated.