{"record":{"id":"1e1ca11e652cb365","repo":"spring-projects/spring-ai","slug":"neither-template-nor-resource-is-set","errorCode":null,"errorMessage":"Neither template nor resource is set","messagePattern":"Neither template nor resource is set","errorType":"validation","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java","lineNumber":269,"sourceCode":"\n\t\tpublic Builder renderer(TemplateRenderer renderer) {\n\t\t\tAssert.notNull(renderer, \"renderer cannot be null\");\n\t\t\tthis.renderer = renderer;\n\t\t\treturn this;\n\t\t}\n\n\t\tpublic PromptTemplate build() {\n\t\t\tif (this.template != null && this.resource != null) {\n\t\t\t\tthrow new IllegalArgumentException(\"Only one of template or resource can be set\");\n\t\t\t}\n\t\t\telse if (this.resource != null) {\n\t\t\t\treturn new PromptTemplate(this.resource, this.variables, this.renderer);\n\t\t\t}\n\t\t\telse if (this.template != null) {\n\t\t\t\treturn new PromptTemplate(this.template, this.variables, this.renderer);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new IllegalStateException(\"Neither template nor resource is set\");\n\t\t\t}\n\t\t}\n\n\t}\n\n}\n","sourceCodeStart":251,"sourceCodeEnd":276,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java#L251-L276","documentation":"Validation guard in PromptTemplate.Builder.build(): neither a template string nor a template resource was supplied, so there is no prompt content to render and constructing the PromptTemplate would produce a useless object. It is a generic guard over the builder's own state — the faulty input is an empty builder (both this.template and this.resource left null). Fix by calling template(...) or resource(...) before build().","triggerScenarios":"Calling PromptTemplate.builder().build() (or only setting variables/renderer) without calling template(...) or resource(...).","commonSituations":"Dynamically built templates where a condition skipped setting the source; builder reused after an earlier branch chose neither; configuration-driven code where the template property resolved to null and was silently skipped.","solutions":["Call builder.template(...) with the template string","Or call builder.resource(...) with a classpath/file Resource"],"exampleFix":"// before\nPromptTemplate.builder().variables(Map.of(\"x\", 1)).build();\n// after\nPromptTemplate.builder().template(\"Hello {x}\").variables(Map.of(\"x\", 1)).build();","handlingStrategy":"validation","validationCode":"if (templateText == null && resource == null) throw new IllegalStateException(\"Provide a template or resource before building PromptTemplate\");","typeGuard":null,"tryCatchPattern":"try { return builder.build(); } catch (IllegalStateException e) { logger.error(\"no template source configured\", e); throw e; }","preventionTips":["Always call .template(...) or .resource(...) before build()","Validate configuration-driven template sources for null/empty early at startup","Fail fast with a clear message rather than letting the builder throw"],"tags":["builder","missing-template","illegal-state"],"backgroundTag":"missing-required-option","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}