spring-projects / spring-projects/spring-ai
When using `MultiQueryExpander`, if the model's response contains empty newlines, the query expansion fails and returns the original query unchanged. The current implementation doesn't properly handle or filter out empty lines from the model's response.
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 6
Description
Bug description
When using MultiQueryExpander, if the model's response contains empty newlines, the query expansion fails and returns the original query unchanged. The current implementation doesn't properly handle or filter out empty lines from the model's response.
Environment
- Spring AI version: 1.0.0
- Java version: 17
- Model/API used: deepseek-reasoner
-
- Spring Boot version: 3.4.6
Steps to reproduce
- Configure a
MultiQueryExpanderwithnumberOfQueries(3) - Call
expand()with any query - Have the AI model return a response that includes empty newlines between valid queries
- Observe that the original query is returned instead of the expanded queries
Expected behavior
The expander should:
- Filter out empty lines from the model's response
- Return the valid expanded queries as long as there are enough non-empty variants
- Only fall back to the original query if there aren't enough valid expanded queries
Actual behavior
The expander fails when encountering empty newlines in the response, even when there are enough valid query variants present.
Minimal Complete Reproducible example
MultiQueryExpander queryExpander = MultiQueryExpander.builder()
.chatClientBuilder(this.chatClient.mutate())
.includeOriginal(false)
.numberOfQueries(3)
.build();
return queryExpander.expand(new Query("How to run a Spring Boot app?"));
Proposed solution
The split("\n") operation should be followed by filtering out empty strings. Here's the suggested fix:
var queryVariants = Arrays.stream(response.split("\n"))
.filter(StringUtils::hasText)
.toList();
if (CollectionUtils.isEmpty(queryVariants) || this.numberOfQueries > queryVariants.size()) {
logger.warn(
"Query expansion result does not contain the requested {} variants. Returning the input query unchanged.",
this.numberOfQueries);
return List.of(query);
}
Additional context
This is particularly important because:
- LLMs often include formatting newlines in their responses
- The current behavior causes valid expansions to be discarded unnecessarily
- The fix would make the expander more robust to normal model output variations
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at MultiQueryExpander.expand() and reproduce the issue with a model response containing empty lines between valid query variants. The change is complete when valid non-empty variants are returned and the original query is used only when too few valid variants remain.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- ai
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100