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.

Open Beginner friendly
#3,345 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
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

  1. Configure a MultiQueryExpander with numberOfQueries(3)
  2. Call expand() with any query
  3. Have the AI model return a response that includes empty newlines between valid queries
  4. Observe that the original query is returned instead of the expanded queries

Expected behavior
The expander should:

  1. Filter out empty lines from the model's response
  2. Return the valid expanded queries as long as there are enough non-empty variants
  3. 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:

  1. LLMs often include formatting newlines in their responses
  2. The current behavior causes valid expansions to be discarded unnecessarily
  3. The fix would make the expander more robust to normal model output variations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.