Skip to content

fix: cap ExpandingQueryTransformer output at n queries - #6225

Open
Lubaoshuai wants to merge 4 commits into
langchain4j:mainfrom
Lubaoshuai:fix/expanding-query-transformer-cap-at-n
Open

Lubaoshuai wants to merge 4 commits into
langchain4j:mainfrom
Lubaoshuai:fix/expanding-query-transformer-cap-at-n

Conversation

@Lubaoshuai

Copy link
Copy Markdown
Contributor

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. ExpandingQueryTransformer returned 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 n parsed queries are returned (the first n; 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

  • There are no breaking changes (API, behaviour)
  • I have added unit and/or integration tests for my change
  • The tests cover both positive and negative cases
  • I have manually run all the unit and integration tests in the module I have added/changed, and they are all green (mvn -pl langchain4j-core test: 1284 tests, 0 failures)
  • I have manually run all the unit and integration tests in the core and main modules, and they are all green (this PR only touches langchain4j-core; full core suite verified above)
  • I have added/updated the documentation (no documentation change required)
  • I have added an example in the examples repo (only for "big" features)
  • I have added/updated Spring Boot starter(s) (if applicable)

LUbaoshuai and others added 2 commits August 30, 2026 21:24
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
Lubaoshuai force-pushed the fix/expanding-query-transformer-cap-at-n branch from 1dcabff to 430019b Compare September 3, 2026 13:21

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] ExpandingQueryTransformer can return more than expected number of queries due to LLM output variability

1 participant