Skip to content

Cache works differently with vertexai.preview vs google.genai #5271

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
asleshapokhrel opened this issue Apr 28, 2025 · 0 comments
Open

Cache works differently with vertexai.preview vs google.genai #5271

asleshapokhrel opened this issue Apr 28, 2025 · 0 comments
Labels
api: vertex-ai Issues related to the googleapis/python-aiplatform API.

Comments

@asleshapokhrel
Copy link

I am trying to understand why caching works differently between these two libraries. vertexai.preview seems to be agnostic to the model used for creating cache however, genai runs into an error when cache is loaded with a different model than the one used to create it. Does the caching work differently under the hood and is the behaviour of one library more correct than the other?

In vertexai.preview, the model can be loaded with a cache using the following and it works fine.

from vertexai.preview import caching
from vertexai.preview.generative_models import GenerativeModel

cached_content = caching.CachedContent.create(
                      model_name = "gemini-1.5-pro-002",
                      contents = ["The sky is blue." * 2000])
model = GenerativeModel(model_name="gemini-2.0-flash-001").from_cached_content(cached_content)
response = model.generate_content("What colour is sky?")
print(response.text)
>> Blue

However, a similar code using genai results in an error.

from google.genai import Client, types

client = Client()
cached_content = client.caches.create(
                    model="gemini-1.5-pro-002",
                    config=types.CreateCachedContentConfig(
                    contents=["The sky is blue." * 2000]))
response = client.models.generate_content(
            model="gemini-2.0-flash-001",
            contents="What colour is the sky?",
            config=types.GenerateContentConfig(cached_content=cached_content.name,))
print(response.text)

>> google.genai.errors.ClientError: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'The model in the inference request gemini-2.0-flash-001 does not match the model in the cached content...'}}
@product-auto-label product-auto-label bot added the api: vertex-ai Issues related to the googleapis/python-aiplatform API. label Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: vertex-ai Issues related to the googleapis/python-aiplatform API.
Projects
None yet
Development

No branches or pull requests

1 participant