- Notifications
You must be signed in to change notification settings - Fork 20.9k
fix: #30511 [Bug] knowledge_retrieval_node fails when using Rerank Model: "Working outside of application context" and add regression test #30549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
113 changes: 113 additions & 0 deletions 113 api/tests/unit_tests/core/rag/retrieval/test_knowledge_retrieval.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import threading | ||
| from unittest.mock import Mock, patch | ||
| from uuid import uuid4 | ||
| | ||
| import pytest | ||
| from flask import Flask, current_app | ||
| | ||
| from core.rag.models.document import Document | ||
| from core.rag.retrieval.dataset_retrieval import DatasetRetrieval | ||
| from models.dataset import Dataset | ||
| | ||
| | ||
| class TestRetrievalService: | ||
| @pytest.fixture | ||
| def mock_dataset(self) -> Dataset: | ||
| dataset = Mock(spec=Dataset) | ||
| dataset.id = str(uuid4()) | ||
| dataset.tenant_id = str(uuid4()) | ||
| dataset.name = "test_dataset" | ||
| dataset.indexing_technique = "high_quality" | ||
| dataset.provider = "dify" | ||
| return dataset | ||
| | ||
| def test_multiple_retrieve_reranking_with_app_context(self, mock_dataset): | ||
| """ | ||
| Repro test for current bug: | ||
| reranking runs after `with flask_app.app_context():` exits. | ||
| `_multiple_retrieve_thread` catches exceptions and stores them into `thread_exceptions`, | ||
| so we must assert from that list (not from an outer try/except). | ||
| """ | ||
| dataset_retrieval = DatasetRetrieval() | ||
| flask_app = Flask(__name__) | ||
| tenant_id = str(uuid4()) | ||
| | ||
| # second dataset to ensure dataset_count > 1 reranking branch | ||
| secondary_dataset = Mock(spec=Dataset) | ||
| secondary_dataset.id = str(uuid4()) | ||
| secondary_dataset.provider = "dify" | ||
| secondary_dataset.indexing_technique = "high_quality" | ||
| | ||
| # retriever returns 1 doc into internal list (all_documents_item) | ||
| document = Document( | ||
| page_content="Context aware doc", | ||
| metadata={ | ||
| "doc_id": "doc1", | ||
| "score": 0.95, | ||
| "document_id": str(uuid4()), | ||
| "dataset_id": mock_dataset.id, | ||
| }, | ||
| provider="dify", | ||
| ) | ||
| | ||
| def fake_retriever( | ||
| flask_app, dataset_id, query, top_k, all_documents, document_ids_filter, metadata_condition, attachment_ids | ||
| ): | ||
| all_documents.append(document) | ||
| | ||
| called = {"init": 0, "invoke": 0} | ||
| | ||
| class ContextRequiredPostProcessor: | ||
| def __init__(self, *args, **kwargs): | ||
| called["init"] += 1 | ||
| # will raise RuntimeError if no Flask app context exists | ||
| _ = current_app.name | ||
| | ||
| def invoke(self, *args, **kwargs): | ||
| called["invoke"] += 1 | ||
| _ = current_app.name | ||
| return kwargs.get("documents") or args[1] | ||
| | ||
| # output list from _multiple_retrieve_thread | ||
| all_documents: list[Document] = [] | ||
| | ||
| # IMPORTANT: _multiple_retrieve_thread swallows exceptions and appends them here | ||
| thread_exceptions: list[Exception] = [] | ||
| | ||
| def target(): | ||
| with patch.object(dataset_retrieval, "_retriever", side_effect=fake_retriever): | ||
| with patch( | ||
| "core.rag.retrieval.dataset_retrieval.DataPostProcessor", | ||
| ContextRequiredPostProcessor, | ||
| ): | ||
| dataset_retrieval._multiple_retrieve_thread( | ||
| flask_app=flask_app, | ||
| available_datasets=[mock_dataset, secondary_dataset], | ||
| metadata_condition=None, | ||
| metadata_filter_document_ids=None, | ||
| all_documents=all_documents, | ||
| tenant_id=tenant_id, | ||
| reranking_enable=True, | ||
| reranking_mode="reranking_model", | ||
| reranking_model={ | ||
| "reranking_provider_name": "cohere", | ||
| "reranking_model_name": "rerank-v2", | ||
| }, | ||
| weights=None, | ||
| top_k=3, | ||
| score_threshold=0.0, | ||
| query="test query", | ||
| attachment_id=None, | ||
| dataset_count=2, # force reranking branch | ||
| thread_exceptions=thread_exceptions, # ✅ key | ||
| ) | ||
| | ||
| t = threading.Thread(target=target) | ||
| t.start() | ||
| t.join() | ||
| | ||
| # Ensure reranking branch was actually executed | ||
| assert called["init"] >= 1, "DataPostProcessor was never constructed; reranking branch may not have run." | ||
| | ||
| # Current buggy code should record an exception (not raise it) | ||
| assert not thread_exceptions, thread_exceptions | ||
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.
Uh oh!
There was an error while loading. Please reload this page.