Fix: Too Many Results When Using Multiple Knowledge Bases
🐛Issue Description
Fixes issue #17706: When using multiple knowledge bases with a model, the system returns Top-K results per knowledge base instead of Top-K results total.
Problem: Top-K = 1 with 2 knowledge bases returns 2 results instead of 1
Root Cause: Each knowledge base queried with K limit individually, then results merged
Impact: Users get more results than requested, breaking Top-K semantics
User Experience: Confusing behavior when configuring multiple knowledge bases
# Issue reproduction test
Setup: Top-K=1, Collections=2, Expected=1
BEFORE: 2 results (WRONG)
AFTER: 1 result (CORRECT) ✅
# Performance analysis
Typical case: +100% overhead (acceptable)
Large case: +400% overhead (acceptable)
Capped case: Limited to 10x maximum ✅
## 📝 **Contributor License Agreement**
contributor license agreement
---
<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
## 📋 Pull Request Information
**Original PR:** https://github.com/open-webui/open-webui/pull/18210
**Author:** [@lokiee0](https://github.com/lokiee0)
**Created:** 10/10/2025
**Status:** ❌ Closed
**Base:** `main` ← **Head:** `fix-top-k-multiple-knowledge-bases-issue-17706`
---
### 📝 Commits (7)
- [`5f584ab`](https://github.com/open-webui/open-webui/commit/5f584ab070c7650c41b6eed4970b4f3779ade401) Add setup documentation and configuration files
- [`66e1360`](https://github.com/open-webui/open-webui/commit/66e1360058f1b5e15db7b34a9f14c6ef6b114cb8) Fix memory leak and NUL character issues in web search
- [`924c849`](https://github.com/open-webui/open-webui/commit/924c8491220f494ccfae576934a06e5d358881c4) Add comprehensive pull request template for web search fix
- [`31fe01c`](https://github.com/open-webui/open-webui/commit/31fe01ca551798b89648535f2fd0131fdf60c7f6) Add CLA text to pull request template
- [`a201d43`](https://github.com/open-webui/open-webui/commit/a201d4363d0ca1bf2a91d39ecd005b9a2db5b921) Fix embedding and reranker models not working after JSON config import
- [`fc39191`](https://github.com/open-webui/open-webui/commit/fc39191ed2439534304fa3bfa9c1ba95f4fa6ce1) Add PR template with CLA text for embedding config fix
- [`1becb0e`](https://github.com/open-webui/open-webui/commit/1becb0e42613beae2eced08a08caffd30189e83c) Fix Top-K results with multiple knowledge bases (Issue #17706)
### 📊 Changes
**22 files changed** (+2348 additions, -564 deletions)
<details>
<summary>View changed files</summary>
➕ `EMBEDDING_CONFIG_PR_TEMPLATE.md` (+95 -0)
➕ `FIX_EMBEDDING_CONFIG_IMPORT.md` (+144 -0)
➕ `FIX_MEMORY_LEAK_AND_NUL_CHARS.md` (+57 -0)
➕ `FIX_TOP_K_MULTIPLE_KNOWLEDGE_BASES.md` (+169 -0)
➕ `PULL_REQUEST_TEMPLATE.md` (+91 -0)
➕ `SETUP_STATUS.md` (+125 -0)
➕ `TOP_K_PR_TEMPLATE.md` (+118 -0)
📝 `backend/open_webui/retrieval/utils.py` (+11 -3)
📝 `backend/open_webui/retrieval/vector/dbs/pgvector.py` (+6 -2)
📝 `backend/open_webui/retrieval/vector/utils.py` (+30 -3)
📝 `backend/open_webui/routers/configs.py` (+99 -1)
📝 `backend/open_webui/routers/retrieval.py` (+49 -22)
➕ `fix_embedding_config_import.py` (+137 -0)
➕ `fix_top_k_multiple_knowledge_bases.py` (+239 -0)
➕ `litellm_config.yaml` (+23 -0)
📝 `package-lock.json` (+113 -533)
📝 `package.json` (+1 -0)
➕ `test_backend.py` (+95 -0)
➕ `test_embedding_config_fix.py` (+204 -0)
➕ `test_top_k_fix.py` (+222 -0)
_...and 2 more files_
</details>
### 📄 Description
# Fix: Too Many Results When Using Multiple Knowledge Bases
## 🐛 **Issue Description**
Fixes issue #17706: When using multiple knowledge bases with a model, the system returns Top-K results **per knowledge base** instead of Top-K results **total**.
- **Problem**: Top-K = 1 with 2 knowledge bases returns 2 results instead of 1
- **Root Cause**: Each knowledge base queried with K limit individually, then results merged
- **Impact**: Users get more results than requested, breaking Top-K semantics
- **User Experience**: Confusing behavior when configuring multiple knowledge bases
**Related Issue**: #17706
## 🔧 **Root Cause Analysis**
1. **Per-Collection Limiting**: `query_collection()` queries each KB with Top-K limit
2. **Late Merging**: Results combined after individual K limits applied
3. **Incorrect Semantics**: Final result = K × Number of Knowledge Bases
4. **Missing Global Limit**: No mechanism to apply Top-K across all collections
## ✅ **Solution Implemented**
### 1. Expanded Query Limits
- **File**: `backend/open_webui/retrieval/utils.py`
- Modified `query_collection()` to use expanded K limit for initial queries
- Modified `query_collection_with_hybrid_search()` for hybrid search scenarios
- Calculate `expanded_k = min(k * num_collections, k * 10)` to get sufficient candidates
### 2. Smart Expansion Logic
- **Single Collection**: No expansion (preserves existing behavior)
- **Multiple Collections**: Expand by collection count with 10x cap
- **Performance Protection**: Cap prevents excessive resource usage
- **Correct Semantics**: Final Top-K applied across all results
### 3. Hybrid Search Support
- Expand both `k` and `k_reranker` parameters
- Maintain proper reranking behavior across collections
- Ensure consistent results between regular and hybrid search
## 📊 **Performance Impact**
| Scenario | Collections | K | Expanded K | Query Overhead | Status |
|----------|-------------|---|------------|----------------|---------|
| Single KB | 1 | 5 | 5 | 0% | No change |
| Typical | 2 | 1 | 2 | +100% | Acceptable |
| Medium | 3 | 5 | 15 | +200% | Acceptable |
| Large | 5 | 10 | 50 | +400% | Acceptable |
| Capped | 10 | 20 | 200 | +900% | Capped at 10x |
**Performance Notes**:
- Overhead acceptable for correct functionality
- Most real-world scenarios have 2-3 knowledge bases
- 10x cap prevents excessive resource usage
- Correctness prioritized over minor performance impact
## 🧪 **Testing**
### Comprehensive Test Suite
- **File**: `test_top_k_fix.py`
- All test scenarios covered:
- ✅ Issue #17706 exact reproduction
- ✅ Single collection behavior unchanged
- ✅ Multiple collections proper Top-K limiting
- ✅ Duplicate document handling with best scores
- ✅ Performance limits and 10x cap validation
- ✅ Hybrid search K and K_reranker expansion
### Validation Results
```bash
# Issue reproduction test
Setup: Top-K=1, Collections=2, Expected=1
BEFORE: 2 results (WRONG)
AFTER: 1 result (CORRECT) ✅
# Performance analysis
Typical case: +100% overhead (acceptable)
Large case: +400% overhead (acceptable)
Capped case: Limited to 10x maximum ✅
## 📝 **Contributor License Agreement**
contributor license agreement
---
<sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/18210
Author: @lokiee0
Created: 10/10/2025
Status: ❌ Closed
Base:
main← Head:fix-top-k-multiple-knowledge-bases-issue-17706📝 Commits (7)
5f584abAdd setup documentation and configuration files66e1360Fix memory leak and NUL character issues in web search924c849Add comprehensive pull request template for web search fix31fe01cAdd CLA text to pull request templatea201d43Fix embedding and reranker models not working after JSON config importfc39191Add PR template with CLA text for embedding config fix1becb0eFix Top-K results with multiple knowledge bases (Issue #17706)📊 Changes
22 files changed (+2348 additions, -564 deletions)
View changed files
➕
EMBEDDING_CONFIG_PR_TEMPLATE.md(+95 -0)➕
FIX_EMBEDDING_CONFIG_IMPORT.md(+144 -0)➕
FIX_MEMORY_LEAK_AND_NUL_CHARS.md(+57 -0)➕
FIX_TOP_K_MULTIPLE_KNOWLEDGE_BASES.md(+169 -0)➕
PULL_REQUEST_TEMPLATE.md(+91 -0)➕
SETUP_STATUS.md(+125 -0)➕
TOP_K_PR_TEMPLATE.md(+118 -0)📝
backend/open_webui/retrieval/utils.py(+11 -3)📝
backend/open_webui/retrieval/vector/dbs/pgvector.py(+6 -2)📝
backend/open_webui/retrieval/vector/utils.py(+30 -3)📝
backend/open_webui/routers/configs.py(+99 -1)📝
backend/open_webui/routers/retrieval.py(+49 -22)➕
fix_embedding_config_import.py(+137 -0)➕
fix_top_k_multiple_knowledge_bases.py(+239 -0)➕
litellm_config.yaml(+23 -0)📝
package-lock.json(+113 -533)📝
package.json(+1 -0)➕
test_backend.py(+95 -0)➕
test_embedding_config_fix.py(+204 -0)➕
test_top_k_fix.py(+222 -0)...and 2 more files
📄 Description
Fix: Too Many Results When Using Multiple Knowledge Bases
🐛 Issue Description
Fixes issue #17706: When using multiple knowledge bases with a model, the system returns Top-K results per knowledge base instead of Top-K results total.
Related Issue: #17706
🔧 Root Cause Analysis
query_collection()queries each KB with Top-K limit✅ Solution Implemented
1. Expanded Query Limits
backend/open_webui/retrieval/utils.pyquery_collection()to use expanded K limit for initial queriesquery_collection_with_hybrid_search()for hybrid search scenariosexpanded_k = min(k * num_collections, k * 10)to get sufficient candidates2. Smart Expansion Logic
3. Hybrid Search Support
kandk_rerankerparameters📊 Performance Impact
Performance Notes:
🧪 Testing
Comprehensive Test Suite
test_top_k_fix.pyValidation Results