Replies: 1 comment 4 replies
-
|
This error happens because a Document ORM object is being accessed outside an active SQLAlchemy session—usually after being passed between pipeline nodes, threads, or tasks, which causes it to become detached. This is a known issue in Dify's pipeline-based document parsing and indexing workflows. Legacy (non-pipeline) indexing works because it keeps the session context intact, but pipelines can lose session context between steps, triggering this error. The official fix is to only pass primary key IDs (not ORM objects) between pipeline steps and always re-query the Document inside each node or task using a new session. This pattern is implemented in the feat/rag-2 branch and PR #26480, and is included in Dify 2.0.0 beta and later. Upgrading to 2.0.0 beta or newer will resolve this issue for most users. Downgrading does not help, as the problem exists in 1.9.0 and 1.9.1 as well [discussion]. If you cannot upgrade, a temporary workaround is to modify your pipeline or custom node code to avoid passing ORM objects directly—pass only IDs and re-query the object inside the node using a new session, for example: # Bad: passing ORM object
def node_func(document: Document):
print(document.id) # document may be detached
# Good: passing only ID and re-querying
def node_func(document_id: int):
with Session(engine) as session:
document = session.get(Document, document_id)
print(document.id)Recent PRs like #24656 and #24135 improved session management by using explicit session scopes, but the error can still occur if ORM objects are passed between threads or tasks. For advanced debugging, add assertions or logging to detect ORM objects passed between threads, enable SQLAlchemy echo mode, or instrument the workflow engine to check for ORM objects in variable pools or node outputs [issue #26511]. Relevant code for pipeline indexing and session management is in To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Self Checks
1. Is this request related to a challenge you're experiencing? Tell me about your story.
Why does uploading files using a pipeline-style knowledge base cause an error: Instance <Document at 0x7f72c31c0980> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)? What should I do? This problem has been ongoing for a long time.
2. Additional context or comments
Instance <Document at 0x7f72c31c0980> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)
Beta Was this translation helpful? Give feedback.
All reactions