spring-projects/spring-ai · error · IllegalArgumentException
Only one of template or resource can be set
Error message
Only one of template or resource can be set
What it means
PromptTemplate.Builder.build() enforces that the template can come from exactly one source: an inline string template or a Resource. Setting both makes the source ambiguous, so build() throws this IllegalArgumentException.
Source
Thrown at spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/PromptTemplate.java:260
return this;
}
public Builder variables(Map<String, Object> variables) {
Assert.notNull(variables, "variables cannot be null");
Assert.noNullElements(variables.keySet(), "variables keys cannot be null");
this.variables = variables;
return this;
}
public Builder renderer(TemplateRenderer renderer) {
Assert.notNull(renderer, "renderer cannot be null");
this.renderer = renderer;
return this;
}
public PromptTemplate build() {
if (this.template != null && this.resource != null) {
throw new IllegalArgumentException("Only one of template or resource can be set");
}
else if (this.resource != null) {
return new PromptTemplate(this.resource, this.variables, this.renderer);
}
else if (this.template != null) {
return new PromptTemplate(this.template, this.variables, this.renderer);
}
else {
throw new IllegalStateException("Neither template nor resource is set");
}
}
}
}
View on GitHub (pinned to 98a7beda4f)
Solutions
- Set only one source: remove the .template(...) call when using .resource(...), or vice versa.
- If you want file-with-fallback, choose the source conditionally before building.
- Create two separate builders instead of reusing one builder object for both variants.
Example fix
// before
PromptTemplate.builder().template("Hello {name}").resource(res).build();
// after
PromptTemplate.builder().resource(res).build(); Defensive patterns
Strategy: validation
Validate before calling
if (b.templateSet && b.resourceSet) throw new IllegalStateException("Set only one of template or resource"); Try / catch
try { return builder.build(); } catch (IllegalArgumentException e) { logger.error("both template and resource set", e); } Prevention
- Pick one source style per template (file OR inline) as a project convention
- Don't reuse a builder instance across conditional branches that set different sources
- Add a unit test per builder configuration
When it happens
Trigger: Calling PromptTemplate.builder().template("...").resource(resource)...build() — i.e. both setters invoked on the same builder.
Common situations: Reusable builder code that unconditionally sets a default template and then optionally a resource file; copy-pasted fluent chains accumulating both sources.
Related errors
- Only one of template or resource can be set
- Only one of prompt or uri can be provided!
- Bean must not be null
- Bean must not be null
- Bean must not be null
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/5cae98387429d13d.
Report an issue: GitHub.