spring-projects/spring-ai · warning
keywordCount will be ignored as keywordsTemplate is set.
Error message
keywordCount will be ignored as keywordsTemplate is set.
What it means
KeywordMetadataEnricher.Builder.build() logs this warning when both keywordsTemplate and keywordCount are set. When a custom keywordsTemplate is supplied, keywordCount has no effect because the template fully defines the prompt. The enricher is built using the template and the count is silently ignored.
Solutions
- Remove the keywordCount(...) call when a custom keywordsTemplate is set.
- Keep the count inside the template text itself (e.g. "{\"keywords\": [no more than 10 keywords]}").
- If you want count-based behavior, drop keywordsTemplate and keep keywordCount only.
Example fix
// before
KeywordMetadataEnricher enricher = KeywordMetadataEnricher.builder()
.chatModel(chatModel)
.keywordsTemplate("Extract 8 keywords: {text}")
.keywordCount(5)
.build();
// after — put the count in the template, drop keywordCount
KeywordMetadataEnricher enricher = KeywordMetadataEnricher.builder()
.chatModel(chatModel)
.keywordsTemplate("Extract 8 keywords from: {text}")
.build(); Defensive patterns
Strategy: validation
Validate before calling
KeywordMetadataEnricher.Builder b = KeywordMetadataEnricher.builder().chatModel(chatModel); assert (templateSet && countSet) == false : "Set either keywordsTemplate or keywordCount, not both";
Prevention
- Choose one configuration style: count OR template.
- Put the desired count inside the template text when using a custom template.
- Review builder calls after copy-pasting examples.
- Add a unit test asserting the enricher's prompt contains the expected count.
When it happens
Trigger: Calling builder.build() on KeywordMetadataEnricher.Builder after setting both keywordsTemplate(...) and keywordCount(n).
Common situations: Copy-pasting a custom template example while leaving a default keywordCount; configuring the enricher via properties where both fields get populated; upgrading from count-based to template-based configuration without removing the count.
Understand the failure class
Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.
Related errors
- Context does not have an entry for key
- Context does not have an entry for key
- Failed to parse metadata JSON. Using empty metadata. json=
- Failed to serialize the Document metadata:
- Invalid Neo4j node label: ''. Labels must start with a…
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/f1b162405a03ac51.
Report an issue: GitHub.
Appendix: source
Thrown at spring-ai-model/src/main/java/org/springframework/ai/model/transformer/KeywordMetadataEnricher.java:147
}
public Builder keywordCount(int keywordCount) {
Assert.isTrue(keywordCount >= 1, "The keywordCount must be >= 1");
this.keywordCount = keywordCount;
return this;
}
public Builder keywordsTemplate(PromptTemplate keywordsTemplate) {
Assert.notNull(keywordsTemplate, "The keywordsTemplate must not be null");
this.keywordsTemplate = keywordsTemplate;
return this;
}
public KeywordMetadataEnricher build() {
if (this.keywordsTemplate != null) {
if (this.keywordCount != 0) {
logger.warn("keywordCount will be ignored as keywordsTemplate is set.");
}
return new KeywordMetadataEnricher(this.chatModel, this.keywordsTemplate);
}
return new KeywordMetadataEnricher(this.chatModel, this.keywordCount);
}
}
}
View on GitHub (pinned to 98a7beda4f)