Repository navigation
fix: cap ExpandingQueryTransformer output at n queries - #6225
Open
Lubaoshuai wants to merge 4 commits into
Open
Lubaoshuai wants to merge 4 commits into
Lubaoshuai wants to merge 4 commits into
Conversation
LLMs do not always follow the requested query count: they may prepend introductory lines, enumerate results, or return more (or fewer) queries than requested. ExpandingQueryTransformer returned every parsed line, so downstream retrieval could receive more queries than configured. Keep at most n parsed queries (discussed in langchain4j#3187). Returning fewer queries than requested is left as-is. Fixes langchain4j#3187
…-transformer-cap-at-n # Conflicts: # langchain4j-core/src/main/java/dev/langchain4j/rag/query/transformer/ExpandingQueryTransformer.java
Lubaoshuai
force-pushed
the
fix/expanding-query-transformer-cap-at-n
branch
from
September 3, 2026 13:21
1dcabff to
430019b
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
Closes #3187
Change
LLMs do not always follow the requested query count: they may prepend introductory lines, enumerate results, or return more (or fewer) queries than requested.
ExpandingQueryTransformerreturned every parsed line, so downstream retrieval could receive more queries than configured, inflating retrieval cost unpredictably.As discussed in the issue, this PR keeps the simpler strategy as the default: at most
nparsed queries are returned (the firstn; returning fewer than requested is left as-is, since the alternative — re-querying the LLM or an embedding-model dedupe step — requires components that may not be available).Note: #4362 proposes a JSON/structured-output based re-implementation of this transformer. This PR intentionally implements only the simple truncation strategy discussed in the issue, which also works with LLMs that do not support JSON response format. The two approaches are complementary; happy to rebase on top of #4362 if it is preferred.
General checklist
mvn -pl langchain4j-core test: 1284 tests, 0 failures)langchain4j-core; full core suite verified above)