[PR #30549] fix: #30511 [Bug] knowledge_retrieval_node fails when using Rerank Model: "Working outside of application context" and add regression test #32872

Closed
opened 2026-02-21 20:52:14 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/30549

State: closed
Merged: Yes


Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

@crazywoola

Since this change diverged from the previous implementation, I re-submitted the fix and closed the earlier PR:
https://github.com/langgenius/dify/pull/30521

fix: #30511 [Bug] knowledge_retrieval_node fails when using Rerank Model: "Working outside of application context"

In the call chain, db.engine requires a Flask application context, but reranking_enable logic is currently executed outside of the application context.

with flask_app.app_context():
    ...
# ← the application context has already been exited here

if reranking_enable:
    data_post_processor = DataPostProcessor(...)

As a result, when the reranking logic initializes and eventually accesses db.engine, Flask raises “Working outside of application context.”

Tests

Added a regression test in test_knowledge_retrieval.py to cover this scenario.

The new test reproduces the failure in the previous implementation and passes after the fix, preventing future regressions.

Verification

uv run --project api pytest tests/unit_tests/core/rag/retrieval/test_knowledge_retrieval.py -k reranking_with_app_context -q

  • Before fix
_______________________________________________________________________ coverage: platform linux, python 3.12.3-final-0 _______________________________________________________________________

=================================================================================== short test summary info ===================================================================================
FAILED tests/unit_tests/core/rag/retrieval/test_knowledge_retrieval.py::TestRetrievalService::test_multiple_retrieve_reranking_with_app_context - AssertionError: [RuntimeError('Working outside of application context.
  • After fix
======================================================================================= tests coverage ========================================================================================
_______________________________________________________________________ coverage: platform linux, python 3.12.3-final-0 _______________________________________________________________________

1 passed, 1 warning in 3.75s

Screenshots

1f651ca1-ebe8-46b4-a508-8fe5d60a9fd6

Before

bd0d0caa5a82a4cd652024f20b0a29da

After

ok

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/30549 **State:** closed **Merged:** Yes --- > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 1. Ensure there is an associated issue and you have been assigned to it > 1. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary @crazywoola Since this change diverged from the previous implementation, I re-submitted the fix and closed the earlier PR: https://github.com/langgenius/dify/pull/30521 fix: #30511 [Bug] knowledge_retrieval_node fails when using Rerank Model: "Working outside of application context" In the call chain, `db.engine` requires a Flask **application context**, but `reranking_enable` logic is currently executed **outside** of the application context. ``` with flask_app.app_context(): ... # ← the application context has already been exited here if reranking_enable: data_post_processor = DataPostProcessor(...) ``` As a result, when the reranking logic initializes and eventually accesses `db.engine`, Flask raises **“Working outside of application context.”** ## Tests Added a regression test in `test_knowledge_retrieval.py` to cover this scenario. The new test reproduces the failure in the previous implementation and passes after the fix, preventing future regressions. ### Verification `uv run --project api pytest tests/unit_tests/core/rag/retrieval/test_knowledge_retrieval.py -k reranking_with_app_context -q` + Before fix ``` _______________________________________________________________________ coverage: platform linux, python 3.12.3-final-0 _______________________________________________________________________ =================================================================================== short test summary info =================================================================================== FAILED tests/unit_tests/core/rag/retrieval/test_knowledge_retrieval.py::TestRetrievalService::test_multiple_retrieve_reranking_with_app_context - AssertionError: [RuntimeError('Working outside of application context. ``` + After fix ``` ======================================================================================= tests coverage ======================================================================================== _______________________________________________________________________ coverage: platform linux, python 3.12.3-final-0 _______________________________________________________________________ 1 passed, 1 warning in 3.75s ``` ## Screenshots <img width="593" height="465" alt="1f651ca1-ebe8-46b4-a508-8fe5d60a9fd6" src="https://github.com/user-attachments/assets/10be36f0-36b4-49c1-816c-67d3ef5bf581" /> ### Before <img width="627" height="983" alt="bd0d0caa5a82a4cd652024f20b0a29da" src="https://github.com/user-attachments/assets/c3245bb6-dfea-49ad-86fc-ed590dfbd9e0" /> ### After <img width="596" height="790" alt="ok" src="https://github.com/user-attachments/assets/4d8ef0c9-c807-4d51-a42f-9976b491c9d7" /> ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:52:14 -05:00
yindo closed this issue 2026-02-21 20:52:14 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32872