spring-projects/spring-ai · error · IllegalArgumentException
textContent and resource cannot be set at the same time
Error message
textContent and resource cannot be set at the same time
What it means
SystemMessage.Builder.build() enforces mutual exclusivity between textContent and resource: you may supply text directly or load it from a resource, never both. Supplying both throws IllegalArgumentException before any message is created.
Solutions
- Remove either the text(...) or the resource(...) call — keep exactly one
- If the resource should win, skip setting text; if the text should win, drop the resource call
- Load the resource yourself with MessageUtils.readResource and pass it via text(...) if you need pre-processing
Example fix
// before
SystemMessage msg = SystemMessage.builder()
.text("You are helpful").resource(new ClassPathResource("sys.txt")).build();
// after
SystemMessage msg = SystemMessage.builder()
.resource(new ClassPathResource("sys.txt")).build(); Defensive patterns
Strategy: validation
Validate before calling
if (StringUtils.hasText(text) && resource != null) {
throw new IllegalArgumentException("Set either text or resource, not both");
} Try / catch
try {
SystemMessage msg = builder.build();
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("textContent and resource")) { /* rebuild with a single source */ }
throw e;
} Prevention
- Audit builder chains for both text(...) and resource(...) calls
- Centralize prompt construction in one helper that picks a single source
- Add unit tests asserting the mutual-exclusion rule
When it happens
Trigger: Calling SystemMessage.builder().text("...").resource(myResource).build(); i.e. chaining both the text and resource setters on the same builder.
Common situations: Copy-pasting builder code that already sets text then adding a resource-based prompt loader; conditional code paths where both branches accidentally execute.
Related errors
- textContent and resource cannot be set at the same time
- Anthropic Citations API requires all documents to have…
- argumentType is required
- Bean must not be null
- Either outputType or outputJsonSchema must be set.
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/be88c4ee7e7591f6.
Report an issue: GitHub.
Appendix: source
Thrown at spring-ai-model/src/main/java/org/springframework/ai/chat/messages/SystemMessage.java:116
public Builder text(String textContent) {
this.textContent = textContent;
return this;
}
public Builder text(Resource resource) {
this.resource = resource;
return this;
}
public Builder metadata(Map<String, Object> metadata) {
this.metadata = metadata;
return this;
}
public SystemMessage build() {
if (StringUtils.hasText(this.textContent) && this.resource != null) {
throw new IllegalArgumentException("textContent and resource cannot be set at the same time");
}
else if (this.resource != null) {
this.textContent = MessageUtils.readResource(this.resource);
}
return new SystemMessage(this.textContent, this.metadata);
}
}
}
View on GitHub (pinned to 98a7beda4f)