cookbook icon indicating copy to clipboard operation
cookbook copied to clipboard

Update everything to use google-genai SDK

Open markmcd opened this issue 10 months ago • 29 comments

Description of the feature request:

Lots of notebooks are using the google-generativeai SDK, but now that google-genai is at 1.0, we should be using that.

e.g. pip install google-generativeai needs to be replaced with pip install google-genai and then relevant code and text needs to be updated.

There's lots to do right now, but we can do every notebook independently so this would be a great one to open to community contributions. If you want to work on one, check the open PR list to see if someone has already done it, then just send a PR directly (but stick to one notebook per PR).

And here's the migration guide: https://ai.google.dev/gemini-api/docs/migrate

markmcd avatar Feb 07 '25 03:02 markmcd

Are there plans to create a google-genai SDK for JS?

mitrick2 avatar Feb 11 '25 17:02 mitrick2

Updating Anomaly_detection_with_embeddings.ipynb. Informed to avoid duplicate efforts by other contributors, thanks

Faakhir30 avatar Mar 01 '25 08:03 Faakhir30

Are there plans to create a google-genai SDK for JS?

I guess we already have it https://github.com/google-gemini/generative-ai-js, currently at google/[email protected] at the time of comment.

Faakhir30 avatar Mar 01 '25 11:03 Faakhir30

@markmcd need help in accepting Google contributor licence agreement there is always an server error. Any suggestions?

charantek1styearbtech avatar Mar 03 '25 07:03 charantek1styearbtech

@markmcd need help in accepting Google contributor licence agreement there is always an server error. Any suggestions?

I also have the same issue.

ROHANDWIVEDI2005 avatar Mar 03 '25 08:03 ROHANDWIVEDI2005

Are there plans to create a google-genai SDK for JS?

@mitrick2 A new typescript unified SDK is on its way. (and BTW, we're also accepting submissions in other languages than Python if you have great examples)

Giom-V avatar Mar 04 '25 09:03 Giom-V

@charantek1styearbtech @ROHANDWIVEDI2005 @Faakhir30 As for the CLA issue, it's being investigated internally. And it seems it was fixed a couple of hours ago, so please try again and tell me if that still doesn't work.

Thanks you all for your contributions!

Giom-V avatar Mar 04 '25 10:03 Giom-V

Hey all,

Thanks a lot for all your submissions! This is greatly helping us.

I've fallen sick and I have to prepare 4 talks for next week (here, if you are in Paris) so I couldn't find the time to review all the Pull requests. But rest assured that I'm not forgetting you and I'll catch up with everything you submitted next week.

Giom-V avatar Mar 06 '25 17:03 Giom-V

Hi @markmcd @Giom-V

While updating the notebooks to use the new google-genai SDK, I noticed that start_chat() and send_message() are still available, but they no longer retain chat history automatically like they did in google-generativeai.

In the previous SDK, chat history was managed within the chat object, allowing past interactions to persist. For example, in Prompting.ipynb

model = genai.GenerativeModel('gemini-2.0-flash')
chat = model.start_chat(history=[])

With google-genai, start_chat() does not seem to keep track of previous exchanges. Instead, it looks like we now need to manually store and pass chat history with each request.

To address this, I plan to store chat history as a list of dictionaries, where each entry contains a role ("user" or "model") and the message content. The model’s response is then appended back to the list to ensure continuity in future exchanges.

Can you confirm if this is the correct approach, or is there a recommended way to handle chat history in google-genai?

Thank you!

KSruthiVel avatar Mar 13 '25 20:03 KSruthiVel

I also got confused at first but there's still a way to get the history. Check the function calling notebook that I updated last week, I use it in there for the example without automatic FC.

Giom-V avatar Mar 13 '25 20:03 Giom-V

Thank you, @Giom-V. I will check the function calling notebook for reference.

KSruthiVel avatar Mar 14 '25 00:03 KSruthiVel

Use chat.get_history() in Python SDK 1.5 or higher.

markmcd avatar Mar 14 '25 03:03 markmcd

Hi @markmcd, @Giom-V,

I've created several PRs that migrate cookbook examples to use the new google-genai SDK as requested in the issue. These migrations maintain the original functionality while updating the API calls to the latest version.

PR links:

  • https://github.com/google-gemini/cookbook/pull/590
  • https://github.com/google-gemini/cookbook/pull/589
  • https://github.com/google-gemini/cookbook/pull/587

I've thoroughly tested each example to ensure they run correctly with the updated SDK.

Thank you, Chamika Jayasinghe

Chamikajaya avatar Mar 22 '25 12:03 Chamikajaya

Hi @markmcd, @Giom-V I have finished migrating the Search_Wikipedia_using_ReAct.ipynb to new google-genai sdk from old google-generative-ai sdk. and ready to make a pull request and informing others to avoid duplicate submissions. The code looks good but I think we need to modify the model_instructions.txt file because the code output seems to be incorrect. We can open a new issue for this one that needs a fix. I have attached a screenshot for reference along with the correct output from the web. Do let me know.

Thank You Raunak Nag

Image Image Image

Raunak2024 avatar Mar 25 '25 13:03 Raunak2024

If anyone is interested in picking up where me and @Giom-V left off at #487, please feel free to!

myanvoos avatar Mar 31 '25 08:03 myanvoos

Thank you all for your submission (especially those of you who migrated dozen of notebooks).

I made a quick check on what had not been migrated yet so you can have a quick way to find something to work on:

Once again thank you all and don't hesitate to continue to improve the cookbook by fixing typos, adding missing examples (like an example using generate_content instead of chat for the grounding, or a function calling example using a OpenAPI schema object like we do on AI Studio), or maybe just proposing more interesting or more fun prompts in the notebook (just keep in mind the is still Google stuff we you can't be too unhinged so I might be difficult).

Giom-V avatar Mar 31 '25 16:03 Giom-V

If anyone is interested in picking up where me and @Giom-V left off at #487, please feel free to!

I‘ve migrated examples/Classify_text_with_embeddings.ipynb in #622. Feel free to check it out!

Jenniebn avatar Mar 31 '25 21:03 Jenniebn

Hi @markmcd, @Giom-V I have finished migrating the Search_Wikipedia_using_ReAct.ipynb to new google-genai sdk from old google-generative-ai sdk. and ready to make a pull request and informing others to avoid duplicate submissions. The code looks good but I think we need to modify the model_instructions.txt file because the code output seems to be incorrect. We can open a new issue for this one that needs a fix. I have attached a screenshot for reference along with the correct output from the web. Do let me know.

Thank You Raunak Nag

Image Image Image

Hi @markmcd, @Giom-V I have finished migrating the Search_Wikipedia_using_ReAct.ipynb to new google-genai sdk from old google-generative-ai sdk. and ready to make a pull request and informing others to avoid duplicate submissions. The code looks good but I think we need to modify the model_instructions.txt file because the code output seems to be incorrect. We can open a new issue for this one that needs a fix. I have attached a screenshot for reference along with the correct output from the web. Do let me know.

Thank You Raunak Nag

Image Image Image

@markmcd @Giom-V Do review this once!

Raunak2024 avatar Apr 02 '25 14:04 Raunak2024

@markmcd @Giom-V Do review this once!

@Raunak2024 I reviewed it 3 days ago...

Giom-V avatar Apr 03 '25 09:04 Giom-V

Hi everyone! If this topic is still open, I'd like to contribute — could you let me know exactly what I need to do?

iambogeumkim avatar Apr 03 '25 13:04 iambogeumkim

Hi everyone! If this topic is still open, I'd like to contribute — could you let me know exactly what I need to do?

Thanks for volunteering your help. The easiest is to take one of the notebooks I listed in this post that isn't been worked on (double-check the open pull request just in case). And then migrate them to use the new google-genai SDK following this migration guide.

Be warned that I'll likely ask you to not only update the code but to also correct the wording if it's a bit old and to improve the readability of the code in various ways. You can check past PR to get an idea.

Giom-V avatar Apr 03 '25 13:04 Giom-V

@All, I will be travelling to Google Cloud Next this week, which mean that I won't have much time to review and merge all of your pull requests (especially since there are more and more every day... and think you for that!).

I know that some of you wanted to use them to apply for the GSoC, feel free to still use them, even if they haven't been merged yet.

Giom-V avatar Apr 07 '25 13:04 Giom-V

Hi @Giom-V Have a safe trip and good luck with your meetings.

I made a new PR #679 to update the weaviate example

Mu-Magdy avatar Apr 07 '25 15:04 Mu-Magdy

Hey @Mu-Magdy , I think you didn't notice, I already submitted a PR for that notebook #659

andycandy avatar Apr 07 '25 16:04 andycandy

Hi @andycandy Yes I didn't notice at first. But I think you need to update you pr to be reviewable. I can help with that if you want

Mu-Magdy avatar Apr 07 '25 19:04 Mu-Magdy

Hi @andycandy Yes I didn't notice at first. But I think you need to update you pr to be reviewable. I can help with that if you want

No, thank you tho. It was an old PR which I didn't get time to check back. It's all updated now :)

andycandy avatar Apr 07 '25 20:04 andycandy

@Giom-V I have submitted PR #768 to update the notebook with the new SDK. Please let me know if there is anything else I should do.

sbeeredd04 avatar May 18 '25 19:05 sbeeredd04

Hi @markmcd, is this topic still open and what exactly needs to be done to solve this? I see this examples/Voice_memos.ipynb has not been updated with the new sdk. Should i update this?

PranavDarshan avatar Jul 03 '25 17:07 PranavDarshan