Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response.text() returns empty string on streamed responses #310

Open
abdelkd opened this issue Dec 22, 2024 · 0 comments
Open

response.text() returns empty string on streamed responses #310

abdelkd opened this issue Dec 22, 2024 · 0 comments
Assignees
Labels
component:js sdk Issue/PR related to JavaScript SDK status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working

Comments

@abdelkd
Copy link

abdelkd commented Dec 22, 2024

Description of the bug:

After generating content using generateContentStream or chat.sendMessageStream the response.text() returns empty string

Actual vs expected behavior:

Actual Behavior

import { GenerativeModel, GoogleGenerativeAI } from '@google/generative-ai';

const MODEL = "gemini-1.5-flash"
/** @type {GenerativeModel | undefined} */
let cachedModel = undefined;

/**
  * @returns {GenerativeModel}
  */
function getModel() {
  if (cachedModel !== undefined) return cachedModel;

  const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY)
  cachedModel = genAI.getGenerativeModel({
    model: MODEL,
    systemInstruction: "You are a helpful assistant"
  })

  return cachedModel;
}

async function testSendMessageStream() {
  const model = getModel()
  const chat = model.startChat()

  const res = model.generateContentStream("Hello, How're you?")
  console.log((await (await res).response).text()) // empty string

  let response = await chat.sendMessageStream("Hello, How are you?")
  response = await chat.sendMessageStream("What was my last question?")
  console.log((await response.response).text()) // also empty string
}

async function test() {
  await testSendMessageStream()
}

test()

Expected Behavior

The expected behavior is that text function should return the actual generated response.

Any other information you'd like to share?

No response

abdelkd added a commit to abdelkd/generative-ai-js that referenced this issue Dec 22, 2024
@manojssmk manojssmk added type:bug Something isn't working status:triaged Issue/PR triaged to the corresponding sub-team component:js sdk Issue/PR related to JavaScript SDK labels Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:js sdk Issue/PR related to JavaScript SDK status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants