spring-projects/spring-ai · error · CachedContentException
Failed to create cached content
Error message
Failed to create cached content
What it means
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.
Source
Thrown at models/spring-ai-google-genai/src/main/java/org/springframework/ai/google/genai/cache/GoogleGenAiCachedContentService.java:106
if (request.getTtl() != null) {
configBuilder.ttl(request.getTtl());
}
else if (request.getExpireTime() != null) {
configBuilder.expireTime(request.getExpireTime());
}
try {
CreateCachedContentConfig config = configBuilder.build();
CachedContent cachedContent = this.caches.create(request.getModel(), config);
if (logger.isDebugEnabled()) {
logger.debug("Created cached content: " + cachedContent.name().orElse("unknown"));
}
return GoogleGenAiCachedContent.from(cachedContent);
}
catch (Exception e) {
logger.error("Failed to create cached content", e);
throw new CachedContentException("Failed to create cached content", e);
}
}
/**
* Retrieves cached content by name.
* @param name the cached content name
* @return the cached content, or null if not found
*/
@Nullable public GoogleGenAiCachedContent get(String name) {
Assert.hasText(name, "Name must not be empty");
try {
GetCachedContentConfig config = GetCachedContentConfig.builder().build();
CachedContent cachedContent = this.caches.get(name, config);
if (logger.isDebugEnabled()) {
logger.debug("Retrieved cached content: " + name);
}
return GoogleGenAiCachedContent.from(cachedContent);View on GitHub (pinned to 98a7beda4f)
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
Example fix
// before
CachedContent created = cachedContentService.create(request);
// after
try {
CachedContent created = cachedContentService.create(request);
} catch (CachedContentException e) {
logger.error("create failed: {}", e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
} Defensive patterns
Strategy: try-catch
Validate before calling
Assert.hasText(request.name(), "model required"); // and verify key:
if (apiKey == null || apiKey.isBlank()) throw new IllegalStateException("Google API key not configured"); Try / catch
try {
cached = service.create(request);
} catch (CachedContentException e) {
Throwable cause = e.getCause();
logger.error("cached content create failed: {}", cause == null ? e.getMessage() : cause.getMessage());
// fall back to uncached prompt
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Failed to update cached content:
- Failed to list 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/4ba270f35b6cb403.
Report an issue: GitHub.