{"record":{"id":"4ba270f35b6cb403","repo":"spring-projects/spring-ai","slug":"failed-to-create-cached-content","errorCode":null,"errorMessage":"Failed to create cached content","messagePattern":"Failed to create 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":106,"sourceCode":"\n\t\tif (request.getTtl() != null) {\n\t\t\tconfigBuilder.ttl(request.getTtl());\n\t\t}\n\t\telse if (request.getExpireTime() != null) {\n\t\t\tconfigBuilder.expireTime(request.getExpireTime());\n\t\t}\n\n\t\ttry {\n\t\t\tCreateCachedContentConfig config = configBuilder.build();\n\t\t\tCachedContent cachedContent = this.caches.create(request.getModel(), config);\n\t\t\tif (logger.isDebugEnabled()) {\n\t\t\t\tlogger.debug(\"Created cached content: \" + cachedContent.name().orElse(\"unknown\"));\n\t\t\t}\n\t\t\treturn GoogleGenAiCachedContent.from(cachedContent);\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tlogger.error(\"Failed to create cached content\", e);\n\t\t\tthrow new CachedContentException(\"Failed to create cached content\", e);\n\t\t}\n\t}\n\n\t/**\n\t * Retrieves cached content by name.\n\t * @param name the cached content name\n\t * @return the cached content, or null if not found\n\t */\n\t@Nullable public GoogleGenAiCachedContent get(String name) {\n\t\tAssert.hasText(name, \"Name must not be empty\");\n\n\t\ttry {\n\t\t\tGetCachedContentConfig config = GetCachedContentConfig.builder().build();\n\t\t\tCachedContent cachedContent = this.caches.get(name, config);\n\t\t\tif (logger.isDebugEnabled()) {\n\t\t\t\tlogger.debug(\"Retrieved cached content: \" + name);\n\t\t\t}\n\t\t\treturn GoogleGenAiCachedContent.from(cachedContent);","sourceCodeStart":88,"sourceCodeEnd":124,"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#L88-L124","documentation":"GoogleGenAiCachedContentService.create() wraps any failure from the Google GenAI SDK's cached-content creation call (contexts.create) in a CachedContentException. The library throws this when the remote create call throws any exception (network, auth, invalid model, payload rejected). The original cause is chained and logged at ERROR level.","triggerScenarios":"Calling create(GoogleGenAiCachedContentCreateRequest) when the underlying genai client's contexts.create fails: invalid model name, content/ttl rejected by the API, expired or missing API credentials, or network outage.","commonSituations":"Wrong or missing GOOGLE_API_KEY, referencing a model that does not support context caching, TTL values outside API limits, or transient network/API errors during caching of large prompts.","solutions":["Inspect the chained cause ('Caused by') for the real SDK/API error message","Verify the API key/credentials are valid and the model supports caching","Check TTL and content payload meet Google GenAI caching constraints","Retry on transient network errors with backoff"],"exampleFix":"// before\nCachedContent created = cachedContentService.create(request);\n// after\ntry {\n    CachedContent created = cachedContentService.create(request);\n} catch (CachedContentException e) {\n    logger.error(\"create failed: {}\", e.getCause() != null ? e.getCause().getMessage() : e.getMessage());\n}","handlingStrategy":"try-catch","validationCode":"Assert.hasText(request.name(), \"model required\"); // and verify key: \nif (apiKey == null || apiKey.isBlank()) throw new IllegalStateException(\"Google API key not configured\");","typeGuard":null,"tryCatchPattern":"try {\n    cached = service.create(request);\n} catch (CachedContentException e) {\n    Throwable cause = e.getCause();\n    logger.error(\"cached content create failed: {}\", cause == null ? e.getMessage() : cause.getMessage());\n    // fall back to uncached prompt\n}","preventionTips":["Validate API key configuration before startup","Confirm the target model supports context caching","Keep TTLs within Google's allowed range","Log and inspect the chained cause, not just the wrapper message"],"tags":["google-genai","api","caching","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"}