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
SystemPromptTemplate.Builder.build() throws this IllegalArgumentException when both an inline template and a Resource are set. Like PromptTemplate, the system-prompt variant requires exactly one template source.
Source
Thrown at spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/SystemPromptTemplate.java:99
}
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;
}
@Override
public SystemPromptTemplate 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 SystemPromptTemplate(this.resource, this.variables, this.renderer);
}
else if (this.template != null) {
return new SystemPromptTemplate(this.template, this.variables, this.renderer);
}
else {
throw new IllegalStateException("Neither template nor resource is set");
}
}
}
}
View on GitHub (pinned to 98a7beda4f)
Solutions
- Remove either the .template(...) or the .resource(...) call so exactly one source remains.
- Select the source conditionally (resource if configured, else inline) before building.
- Use separate builder instances for the two variants.
Example fix
// before
SystemPromptTemplate.builder().template("You are helpful").resource(res).build();
// after
SystemPromptTemplate.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 for SystemPromptTemplate"); Try / catch
try { return builder.build(); } catch (IllegalArgumentException e) { logger.error("both template and resource set on SystemPromptTemplate", e); } Prevention
- Choose exactly one system-prompt source per builder
- Avoid builder reuse that accumulates both setters
- Cover system-prompt construction with unit tests
When it happens
Trigger: SystemPromptTemplate.builder().template("...").resource(resource)...build() — both sources configured on the same builder.
Common situations: Builder chains that set a default inline system prompt and then also attach a resource override; refactored code that kept a stale .template(...) call.
Related errors
- Only one of template or resource can be set
- Neither template nor resource is set
- Only one of prompt or uri can be provided!
- 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/6ea6f87ae196637f.
Report an issue: GitHub.