spring-projects/spring-ai · error · CachedContentException
Failed to list cached content
Error message
Failed to list cached content
What it means
GoogleGenAiCachedContentService.list() wraps any failure while paginating cached content listings (contexts.list) in a CachedContentException. The library throws this when the listing API call or response handling throws for any reason.
Source
Thrown at models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/cache/GoogleGenAiCachedContentService.java:236
contents.add(GoogleGenAiCachedContent.from(content));
// Only get the first page worth of results
if (contents.size() >= (pageSize != null ? pageSize : 100)) {
break;
}
}
// Note: Pager doesn't expose page tokens directly, so we can't support
// pagination
// in the same way. This is a limitation of the SDK.
if (logger.isDebugEnabled()) {
logger.debug("Listed " + contents.size() + " cached content items");
}
return new CachedContentPage(contents, null);
}
catch (Exception e) {
logger.error("Failed to list cached content", e);
throw new CachedContentException("Failed to list cached content", e);
}
}
/**
* Lists all cached content without pagination.
* @return list of all cached content
*/
public List<GoogleGenAiCachedContent> listAll() {
List<GoogleGenAiCachedContent> allContent = new ArrayList<>();
String pageToken = null;
do {
CachedContentPage page = list(100, pageToken);
allContent.addAll(page.getContents());
pageToken = page.getNextPageToken();
}
while (pageToken != null);
View on GitHub (pinned to 98a7beda4f)
Solutions
- Inspect the chained cause for the underlying API error
- Do not reuse stale page tokens; restart listing from the first page
- Verify credentials and API quota
- Retry with backoff on transient network errors
Example fix
// before
CachedContentPage page = service.list(null);
// after
try {
CachedContentPage page = service.list(null);
} catch (CachedContentException e) {
logger.warn("listing cached content failed, restarting from first page", e);
CachedContentPage page2 = service.list(null);
} Defensive patterns
Strategy: retry
Try / catch
CachedContentPage page = null;
for (int attempt = 0; attempt < 3; attempt++) {
try { page = service.list(pageToken); break; }
catch (CachedContentException e) {
if (attempt == 2) throw e;
sleep(backoff(attempt));
pageToken = null; // restart listing on repeated failure
}
} Prevention
- Never reuse page tokens across sessions/runs
- Verify credentials and quota before bulk listing
- Treat listing failures as transient and retry with backoff
When it happens
Trigger: Calling list(pageToken/pageSize) when the remote list call fails: bad page token, API/network error, or invalid credentials.
Common situations: Reusing a pageToken from a previous session after items changed or expired, revoked API credentials, or quota/network issues in environments that enumerate cached content periodically.
Related errors
- Failed to create cached content
- Failed to update cached content:
- Cached content not found:
- Vertex AI mode requires both 'project-id' and 'location' to
- Incomplete Google GenAI configuration: Provide 'api-key' for
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/24f951bacea8586d.
Report an issue: GitHub.