Skip to content

Added support for callbacks on streamed response #410

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

sevenhealer
Copy link

@sevenhealer sevenhealer commented Mar 10, 2025

Add streamCallbacks as an Optional Argument to generateContentStream for Easier Handling of Responses. Fixes #322

Check:

Create a js file
nano test.js

import { GoogleGenerativeAI } from "../dist/index.js";

const genAI = new GoogleGenerativeAI("YOUR_API");

const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash" });

const callbacks = {
    onData: (chunk) => console.log("Data: ", chunk),
    //onEnd: (fullText) => console.log("Final Text: ", fullText),
    //onError: (error) => console.error("Error: ", error),
  };

  try {
    const stream = await model.generateContentStream("Explain how AI works", {}, callbacks);
  } catch (error) {
    console.error("Stream generation failed:", error);
  }

Output:

onData
Screenshot 2025-03-10 080056

onEnd:
Screenshot 2025-03-10 080123

Also Fixed .gitignore to Prevent Unnecessary Upload of package-lock.json

Screenshot 2025-03-10 082933

Copy link

@avinesh2101 avinesh2101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! This change has already been proposed in [Original PR #371]. Submitting duplicate PRs doesn’t add value and can create unnecessary noise for maintainers. It’s best to check existing PRs before submitting new ones. Maybe consider contributing in other meaningful ways!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sevenhealer, package.json has been modified, but there’s no apparent need for this change. Could you clarify why this was updated? If it's unintentional, please revert it to avoid unnecessary changes in dependencies.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix Cannot read .eslintignore file #399
I mentioned here.
for windows during npm run test <- it failed
so using cross-env solve this issue for all env(windows,linux and mac)

@sevenhealer
Copy link
Author

The test case of the PR you mentioned failed.

@avinesh2101
Copy link

The test case of the PR you mentioned failed.

The failure in Original PR #371 is due to a Contributor License Agreement (CLA) check, not a test case failure. This means the contributor must sign the CLA before the PR can proceed. Submitting duplicate PRs doesn't resolve this issue and can create unnecessary work for maintainers. It's best to resolve the CLA issue in the original PR. If the issue isn't addressed in a reasonable time, then raising a new PR would be appropriate. Thanks, and happy contributing!

mr-robot-404-cpu

This comment was marked as resolved.

@Annhiluc Annhiluc added type:feature request New feature request/enhancement status:triaged Issue/PR triaged to the corresponding sub-team p3 labels Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3 status:triaged Issue/PR triaged to the corresponding sub-team type:feature request New feature request/enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for callbacks on streamed response
4 participants