I’ve create a solution that uses Gemini and RAG in Cloud Run. I keep getting the error: ImportError: cannot import name ‘RagRetrieval’ from ‘vertexai.generative_models’
Gemini keeps suggesting different version of Vertex AI, but none of them resolve this. I am also working in a free trial, but Gemini says that shouldn’t matter. Please advise a solution. Thanks.
The error you’re encountering:
ImportError: cannot import name ‘RagRetrieval’ from ‘vertexai.generative_models’
is likely caused by one of the following:
1. Missing or Outdated Vertex AI SDK Version
The RagRetrieval class was introduced in google-cloud-aiplatform >= 1.51.0. If your environment uses an older version, the import will fail.
Solution:
Update the Vertex AI SDK in your Cloud Run container or local environment:
pip install --upgrade google-cloud-aiplatform
Then, verify the installed version:
pip show google-cloud-aiplatform
Ensure the version is 1.51.0 or higher.
2. Incorrect Import Path
Confirm that you are using the correct import syntax:
from vertexai.generative_models import RagRetrieval
This is the valid import path starting with version 1.51.0 of the SDK.
3. Free Trial Access
The Free Trial does not restrict access to RAG features, provided that:
-
The Vertex AI API is enabled
-
Billing is enabled (Free Trial credits are sufficient)
-
You are using a supported region (e.g.,
us-central1,europe-west4) -
You are calling a GA model, such as
gemini-1.5-pro,gemini-2.5-pro, orgemini-2.5-flash
Note: Preview models like gemini-1.5-pro-preview-0514 may no longer be accessible or supported in new projects.
4. Ensure Correct Dependency in Cloud Run
When deploying to Cloud Run, make sure your requirements.txt includes the appropriate SDK version:
google-cloud-aiplatform>=1.51.0
Then rebuild and redeploy your container to ensure the updated library is used.
Summary:
| Requirement | Status |
|---|---|
| SDK version | Must be >= 1.51.0 |
| Import syntax | from vertexai.generative_models import RagRetrieval |
| Model access | Use GA models (gemini-2.5-pro, etc.) |
| Free Trial | Supported with billing and API access |
If the issue persists after these steps, feel free to share the exact code snippet or deployment setup for further assistance.
Shreyash Tapke
Product Expert – Google Cloud
Thank you Shreyash!
You are suggesting I add this to my main.py…
import vertexai from vertexai.generative_models import RagRetrieval
How would I also import GenerativeModel?
You’re welcome! To import both RagRetrieval and GenerativeModel from the vertexai.generative_models module, you can do it like this in your main.py:
from vertexai.generative_models import RagRetrieval, GenerativeModel
Make sure you’ve already imported and initialized Vertex AI properly as well (if you haven’t already), like so:
import vertexai
vertexai.init(project="your-project-id", location="your-region")
Thanks again Shreyash. I get a clean build and deploy, but get this error from my solutions URL. Do you have any other suggestions?
ImportError: cannot import name ‘RagRetrieval’ from ‘vertexai.generative_models’ (/usr/local/lib/python3.11/site-packages/vertexai/generative_models/init.py)
Traceback:
File "/app/main.py", line 3, in <module>
from vertexai.generative_models import RagRetrieval, GenerativeModel
Try updating to google-cloud-aiplatform==1.105.0 or google-cloud-aiplatform==1.106.0in your requirements.txt—that should resolve the import error.
Thanks again Shreyash. I’ve tried both 1.105.0 and 1.106.0 as well as just specifying google-cloud-aiplatform which gemini tells me should get the latest version. None of these resolve the issues which continues to be the error below.
I also tried initializing vertexai before importing RagRetrival like I think you were suggesting…
import vertexai
vertexai.init(project=“pt-mentor-470212”, location=“us-central1”)
from vertexai.generative_models import GenerativeModel, Tool, RagRetrieval
Does anyone else have a suggestion that will resolve this?
ImportError: cannot import name ‘RagRetrieval’ from ‘vertexai.generative_models’ (/usr/local/lib/python3.11/site-packages/vertexai/generative_models/init.py)
Traceback:
File "/app/main.py", line 4, in <module>
from vertexai.generative_models import GenerativeModel, Tool, RagRetrieval
I was able to force an upgrade to google-cloud-aiplatform inside my Dockerfile using…
RUN pip install --no-cache-dir --upgrade google-cloud-aiplatform
I also added code to display the version I am getting and it shows 1.110.0.
So, why am I still unable to import ‘RagRetrieval’ from ‘vertexai.generative_models’ ???
I’ve attached my code and welcome any suggestions to get past this error.
Kind Regards, CB
Vertex AI SDK Version: 1.110.0
ImportError: cannot import name ‘RagRetrieval’ from ‘vertexai.generative_models’ (/usr/local/lib/python3.11/site-packages/vertexai/generative_models/init.py)
Traceback:
File "/app/main.py", line 38, in <module>
from vertexai.generative_models import GenerativeModel, Tool, RagRetrieval
(Attachment requirements.txt is missing)
(Attachment Dockerfile is missing)
(Attachment main.py is missing)