Image caching leads to wrong behavior of gemini?

I am doing a task that gemini-2.0-flash should analyze a user input image according to some rules. For gemini’s better understanding, I tried to use explicit caching to cache some few-shot images. The user input image is put in genai.Content{}.

The overall prompt structure is as below.

genai.Contents: {
    rules 1,
    user input image,
    rules 2
}

cache: {
    few-shot 1 description,
    few-shot 1 image,
    few-shot 2 description,
    few-shot 2 image
}

However, I found that after caching the few-shot images, gemini cannot correctly recognize the user input image. It always analyzes the last few-shot image, or even the “image“ that doesn’t exist.

Gemini works fine if I also put the few-shot images in genai.Content{} (no caching), so I think there may be something wrong when caching is used.

Has anyone run into the similar problem before?

3 Likes

Yes. I am having the exact same issue. It affects all the models, not just Gemini 2.0. I’ve reported via multiple channels but currently I don’t have any resolution.

There’s more info on this GitHub issue: Critical bug: Vertex API with context cache leaks state between generate_content calls · Issue #1634 · googleapis/python-genai · GitHub

I also reported via Google VRP (since this has security/privacy implications) and this is the internel Google ticket, but the Google reviewer didn’t understand what I was saying. Google Issue Tracker (link only accessible within Google)

-Adrian

2 Likes

Thanks for the information. Hope Google will fix the problem soon.

Hi @hedy @Adrian_Cable This issue is now resolved. Please confirm if you are still facing it.