{"record":{"id":"f0fd9f891de630fd","repo":"spring-projects/spring-ai","slug":"failed-to-render-resource-resource-getdescripti","errorCode":null,"errorMessage":"Failed to render resource: ${resource.getDescription()}","messagePattern":"Failed to render resource: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java","lineNumber":165,"sourceCode":"\t\tif (resource == null) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\ttry {\n\t\t\t// Handle ByteArrayResource specially\n\t\t\tif (resource instanceof ByteArrayResource byteArrayResource) {\n\t\t\t\treturn new String(byteArrayResource.getByteArray(), StandardCharsets.UTF_8);\n\t\t\t}\n\t\t\t// If the resource exists but is empty\n\t\t\tif (!resource.exists() || resource.contentLength() == 0) {\n\t\t\t\treturn \"\";\n\t\t\t}\n\t\t\t// For other Resource types or as fallback\n\t\t\treturn resource.getContentAsString(StandardCharsets.UTF_8);\n\t\t}\n\t\tcatch (IOException e) {\n\t\t\tif (log.isWarnEnabled()) {\n\t\t\t\tlog.warn(\"Failed to render resource: \" + resource.getDescription(), e);\n\t\t\t}\n\t\t\treturn \"[Unable to render resource: \" + resource.getDescription() + \"]\";\n\t\t}\n\t}\n\n\t// From PromptTemplateMessageActions.\n\n\t@Override\n\tpublic Message createMessage() {\n\t\treturn new UserMessage(render());\n\t}\n\n\t@Override\n\tpublic Message createMessage(List<Media> mediaList) {\n\t\treturn UserMessage.builder().text(render()).media(mediaList).build();\n\t}\n\n\t@Override","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java#L147-L183","documentation":"PromptTemplate.renderResource loads template content from a Spring Resource. If reading the resource throws IOException, the template logs a warning including the resource description and falls back to the placeholder string \"[Unable to render resource: ...]\" instead of propagating the exception — so the failure shows up later as odd template text rather than an error at render time.","triggerScenarios":"Creating/using a PromptTemplate backed by a Resource (classpath or file) whose content cannot be read: missing file, unreadable permissions, broken InputStream, or IO error during getContentAsString.","commonSituations":"classpath: resource not on the classpath after packaging; file: path wrong or moved; resource inside a jar not loaded correctly; permissions issues in containers.","solutions":["Verify the resource path and that the file exists/is on the classpath at runtime (e.g. Resource.exists())","Check file permissions and packaging (resource included in the jar, correct resources directory)","Read the returned string and detect the \"[Unable to render resource:\" sentinel to fail fast in your code","Pre-load the template content yourself (Files.readString) and pass the raw string to PromptTemplate"],"exampleFix":"// before\nPromptTemplate t = new PromptTemplate(resource); // silently yields [Unable to render resource: ...]\n// after\nif (!resource.exists()) {\n    throw new IllegalStateException(\"Template resource missing: \" + resource.getDescription());\n}\nPromptTemplate t = new PromptTemplate(resource.getContentAsString(StandardCharsets.UTF_8));","handlingStrategy":"try-catch","validationCode":"if (!resource.exists() || !resource.isReadable()) {\n    throw new IllegalStateException(\"Template resource unreadable: \" + resource.getDescription());\n}","typeGuard":null,"tryCatchPattern":"try {\n    String content = resource.getContentAsString(StandardCharsets.UTF_8);\n} catch (IOException e) {\n    // template content unavailable; fail fast instead of letting PromptTemplate emit the sentinel\n}","preventionTips":["Check resource.exists() before constructing PromptTemplate","Verify resources are packaged in the jar","Prefer loading template strings explicitly over Resource-based templates in containers","Scan rendered prompts for the \"[Unable to render resource:\" sentinel in tests"],"tags":["template","resource","io"],"backgroundTag":"file-read-failed","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}