Skip to content

Fix: query expander duplicate queries#12035

Merged
sjrl merged 4 commits into
deepset-ai:mainfrom
aquib8112:fix/query-expander-duplicate-queries
Jul 20, 2026
Merged

Fix: query expander duplicate queries#12035
sjrl merged 4 commits into
deepset-ai:mainfrom
aquib8112:fix/query-expander-duplicate-queries

Conversation

@aquib8112

@aquib8112 aquib8112 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

QueryExpander could return duplicate queries when the chat generator repeated an expansion.

_parse_expanded_queries kept every non-empty string from the response, and the post-processing in run/run_async only checked whether the original query was already present. That check is a guard against appending the original twice, not deduplication, despite a comment claiming otherwise. Duplicates among the generated expansions were passed straight through.

Downstream, each duplicate makes MultiQueryTextRetriever and MultiQueryEmbeddingRetriever run another retrieval for the same query, adding latency and backend load while reducing the number of distinct expansions.

Generated queries are now deduplicated in _parse_expanded_queries, preserving first-seen order. The inaccurate comment was removed from both methods.

Two notes on the approach:

  • Placement. The dedup lives in _parse_expanded_queries because both run and run_async already call it, so both paths are fixed by one change and cannot drift apart again. The method already strips whitespace and drops empty or non-string entries, so it normalizes the response rather than merely parsing it.
  • Order. Deduplicating before truncation avoids spending the expansion budget on repeats. With n_expansions=3 and a response of ["a", "a", "b", "c"], truncating first yields ["a", "b"], while deduplicating first yields ["a", "b", "c"].

Deduplication can leave fewer than n_expansions queries. This matches the documented contract, since the class docstring already describes the output as "up to" N additional queries, and no existing test changes behaviour.

How did you test it?

Four unit tests added next to the existing test_run_query_deduplication, covering both the sync and async paths:

  • duplicate expansions are removed
  • deduplication happens before truncation (["a", "a", "b", "c"] with n_expansions=3 returns three queries, not two)

All four fail on main and pass with this change. Full file: 47 passed, 3 skipped.

Also verified manually that the reproduction snippet from the issue now returns {'queries': ['same query', 'other query']} on both run and run_async.

Notes for the reviewer

  • Happy to move the logic into a shared _post_process_queries helper on the component instead, if you would prefer the parser to stay purely a parser. Kept the diff minimal here.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

Disclaimer: This PR was assisted by AI. I have reviewed the changes and run the relevant tests.

@aquib8112
aquib8112 requested a review from a team as a code owner July 16, 2026 16:53
@aquib8112
aquib8112 requested review from sjrl and removed request for a team July 16, 2026 16:53
@vercel

vercel Bot commented Jul 16, 2026

Copy link
Copy Markdown

@aquib8112 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Jul 16, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Jul 16, 2026
Comment thread test/components/query/test_query_expander.py Outdated
Comment thread haystack/components/query/query_expander.py
@github-actions

github-actions Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/query
  query_expander.py 298
  haystack/tools
  from_function.py
Project Total  

This report was generated by python-coverage-comment-action

@aquib8112

Copy link
Copy Markdown
Contributor Author

Hey @sjrl, thank you for the feedback! I have removed the case-insensitivity logic and moved the async tests to TestQueryExpanderAsync with the @pytest.mark.asyncio marker.

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sjrl
sjrl merged commit ad3391d into deepset-ai:main Jul 20, 2026
22 of 23 checks passed
@sjrl sjrl changed the title Fix/query expander duplicate queries Fix: query expander duplicate queries Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QueryExpander returns duplicate generated queries

3 participants