Gemini 2.0 Flash - Grounding doesnt work in API (but works in AI Studio)

Hey guys,

I’m trying to set up “gemini-2.0-flash-exp” model with using Grounding (GoogleSearch tool) via API iny my Python code.

However it always gives me errors (like: type object ‘Tool’ has no attribute 'GoogleSearch).
The only time when it gives me something else is when I use GoogleSearchRetrieval (as used in Gemini 1.5 Pro). Then it will give me “400 Unable to submit request because Please use google_search field instead of google_search_retrieval field… Learn more: Gemini API を使用してコンテンツを生成する  |  Generative AI on Vertex AI  |  Google Cloud

The only way I can run Gemini 2.0 Flash with Grounding is via AI Studio. However the code you get from “Get code” just simply doesn’t work. Same case is with the code you get with “Open in Colab”.

Any tips?
Are you guys able to run 2.0 with Grounding in your code?

Thanks!

Hi @jedy, welcome to the forum!

Are you using Vertex AI SDK or Google Gen AI SDK?

If you’re using the Google Gen AI SDK, please take a look at this notebook link.

Thanks.

Thank you very much, @GUNAND_MAYANGLAMBAM!

I have tried to follow the notebook and Google Gen AI SDK to the dot and it works now! So thank you so much!

However, there is just so much discrepancy in the documentation and how to import modules and call the API. I would imagine that the Python code in “Get code” in AI Studio would work but it doesn’t and it’s using a completely different set of modules and methods so that’s quite confusing.

Anyway, I got it working now, so thanks again!

1 Like