{"record":{"id":"24f951bacea8586d","repo":"spring-projects/spring-ai","slug":"failed-to-list-cached-content","errorCode":null,"errorMessage":"Failed to list cached content","messagePattern":"Failed to list cached content","errorType":"exception","errorClass":"CachedContentException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/cache/GoogleGenAiCachedContentService.java","lineNumber":236,"sourceCode":"\t\t\t\tcontents.add(GoogleGenAiCachedContent.from(content));\n\t\t\t\t// Only get the first page worth of results\n\t\t\t\tif (contents.size() >= (pageSize != null ? pageSize : 100)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Note: Pager doesn't expose page tokens directly, so we can't support\n\t\t\t// pagination\n\t\t\t// in the same way. This is a limitation of the SDK.\n\t\t\tif (logger.isDebugEnabled()) {\n\t\t\t\tlogger.debug(\"Listed \" + contents.size() + \" cached content items\");\n\t\t\t}\n\n\t\t\treturn new CachedContentPage(contents, null);\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tlogger.error(\"Failed to list cached content\", e);\n\t\t\tthrow new CachedContentException(\"Failed to list cached content\", e);\n\t\t}\n\t}\n\n\t/**\n\t * Lists all cached content without pagination.\n\t * @return list of all cached content\n\t */\n\tpublic List<GoogleGenAiCachedContent> listAll() {\n\t\tList<GoogleGenAiCachedContent> allContent = new ArrayList<>();\n\t\tString pageToken = null;\n\n\t\tdo {\n\t\t\tCachedContentPage page = list(100, pageToken);\n\t\t\tallContent.addAll(page.getContents());\n\t\t\tpageToken = page.getNextPageToken();\n\t\t}\n\t\twhile (pageToken != null);\n","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/cache/GoogleGenAiCachedContentService.java#L218-L254","documentation":"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.","triggerScenarios":"Calling list(pageToken/pageSize) when the remote list call fails: bad page token, API/network error, or invalid credentials.","commonSituations":"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.","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"],"exampleFix":"// before\nCachedContentPage page = service.list(null);\n// after\ntry {\n    CachedContentPage page = service.list(null);\n} catch (CachedContentException e) {\n    logger.warn(\"listing cached content failed, restarting from first page\", e);\n    CachedContentPage page2 = service.list(null);\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"CachedContentPage page = null;\nfor (int attempt = 0; attempt < 3; attempt++) {\n    try { page = service.list(pageToken); break; }\n    catch (CachedContentException e) {\n        if (attempt == 2) throw e;\n        sleep(backoff(attempt));\n        pageToken = null; // restart listing on repeated failure\n    }\n}","preventionTips":["Never reuse page tokens across sessions/runs","Verify credentials and quota before bulk listing","Treat listing failures as transient and retry with backoff"],"tags":["google-genai","api","caching","pagination","wrapped-exception"],"backgroundTag":"api-error-response","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}