raise orm_exc.DetachedInstanceError( sqlalchemy.orm.exc.DetachedInstanceError: Instance <WorkflowRun at 0x3263884d0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3) #7522

Closed
opened 2026-02-21 18:21:01 -05:00 by yindo · 2 comments
Owner

Originally created by @fxgeoffrey on GitHub (Jan 4, 2025).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.14

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

When running a workflow in local deployment mode via an API call, it throws following errors:"

2025-01-04 07:08:18,391,391 ERROR [workflow.py:100] 559559172c internal server error.
Traceback (most recent call last):
  File "/Users/fengxin09/Documents/llm/dify/api/controllers/service_api/app/workflow.py", line 84, in post
    response = AppGenerateService.generate(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/services/app_generate_service.py", line 93, in generate
    generator = WorkflowAppGenerator().generate(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 129, in generate
    return self._generate(
           ^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 173, in _generate
    response = self._handle_response(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 310, in _handle_response
    return generate_task_pipeline.process()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 125, in process
    return self._to_blocking_response(generator)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 132, in _to_blocking_response
    for stream_response in generator:
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 196, in _wrapper_process_stream_response
    for response in self._process_stream_response(tts_publisher=tts_publisher, trace_manager=trace_manager):
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 286, in _process_stream_response
    workflow_node_execution = self._handle_node_execution_start(workflow_run=workflow_run, event=event)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/task_pipeline/workflow_cycle_manage.py", line 293, in _handle_node_execution_start
    workflow_node_execution.tenant_id = workflow_run.tenant_id
                                        ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 566, in __get__
    return self.impl.get(state, dict_)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get
    value = self._fire_loader_callables(state, key, passive)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables
    return state._load_expired(state, passive)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired
    self.manager.expired_attribute_loader(self, toload, passive)
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes
    raise orm_exc.DetachedInstanceError(
sqlalchemy.orm.exc.DetachedInstanceError: Instance <WorkflowRun at 0x3263884d0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)
2025-01-04 07:08:18,404,404 ERROR [app.py:875] 559559172c Exception on /v1/workflows/run [POST]
Traceback (most recent call last):
  File "/Users/fengxin09/Documents/llm/dify/api/controllers/service_api/app/workflow.py", line 84, in post
    response = AppGenerateService.generate(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/services/app_generate_service.py", line 93, in generate
    generator = WorkflowAppGenerator().generate(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 129, in generate
    return self._generate(
           ^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 173, in _generate
    response = self._handle_response(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 310, in _handle_response
    return generate_task_pipeline.process()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 125, in process
    return self._to_blocking_response(generator)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 132, in _to_blocking_response
    for stream_response in generator:
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 196, in _wrapper_process_stream_response
    for response in self._process_stream_response(tts_publisher=tts_publisher, trace_manager=trace_manager):
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 286, in _process_stream_response
    workflow_node_execution = self._handle_node_execution_start(workflow_run=workflow_run, event=event)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/core/app/task_pipeline/workflow_cycle_manage.py", line 293, in _handle_node_execution_start
    workflow_node_execution.tenant_id = workflow_run.tenant_id
                                        ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 566, in __get__
    return self.impl.get(state, dict_)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get
    value = self._fire_loader_callables(state, key, passive)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables
    return state._load_expired(state, passive)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired
    self.manager.expired_attribute_loader(self, toload, passive)
  File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes
    raise orm_exc.DetachedInstanceError(
sqlalchemy.orm.exc.DetachedInstanceError: Instance <WorkflowRun at 0x3263884d0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)
During handling of the above exception, another exception occurred:

✔️ Expected Behavior

How to fix this ? or workaround this ?

Actual Behavior

No response

Originally created by @fxgeoffrey on GitHub (Jan 4, 2025). ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.14 ### Cloud or Self Hosted Self Hosted (Source) ### Steps to reproduce When running a workflow in local deployment mode via an API call, it throws following errors:" ``` 2025-01-04 07:08:18,391,391 ERROR [workflow.py:100] 559559172c internal server error. Traceback (most recent call last): File "/Users/fengxin09/Documents/llm/dify/api/controllers/service_api/app/workflow.py", line 84, in post response = AppGenerateService.generate( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/services/app_generate_service.py", line 93, in generate generator = WorkflowAppGenerator().generate( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 129, in generate return self._generate( ^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 173, in _generate response = self._handle_response( ^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 310, in _handle_response return generate_task_pipeline.process() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 125, in process return self._to_blocking_response(generator) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 132, in _to_blocking_response for stream_response in generator: File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 196, in _wrapper_process_stream_response for response in self._process_stream_response(tts_publisher=tts_publisher, trace_manager=trace_manager): File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 286, in _process_stream_response workflow_node_execution = self._handle_node_execution_start(workflow_run=workflow_run, event=event) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/task_pipeline/workflow_cycle_manage.py", line 293, in _handle_node_execution_start workflow_node_execution.tenant_id = workflow_run.tenant_id ^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 566, in __get__ return self.impl.get(state, dict_) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get value = self._fire_loader_callables(state, key, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables return state._load_expired(state, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired self.manager.expired_attribute_loader(self, toload, passive) File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes raise orm_exc.DetachedInstanceError( sqlalchemy.orm.exc.DetachedInstanceError: Instance <WorkflowRun at 0x3263884d0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3) 2025-01-04 07:08:18,404,404 ERROR [app.py:875] 559559172c Exception on /v1/workflows/run [POST] Traceback (most recent call last): File "/Users/fengxin09/Documents/llm/dify/api/controllers/service_api/app/workflow.py", line 84, in post response = AppGenerateService.generate( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/services/app_generate_service.py", line 93, in generate generator = WorkflowAppGenerator().generate( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 129, in generate return self._generate( ^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 173, in _generate response = self._handle_response( ^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/app_generator.py", line 310, in _handle_response return generate_task_pipeline.process() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 125, in process return self._to_blocking_response(generator) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 132, in _to_blocking_response for stream_response in generator: File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 196, in _wrapper_process_stream_response for response in self._process_stream_response(tts_publisher=tts_publisher, trace_manager=trace_manager): File "/Users/fengxin09/Documents/llm/dify/api/core/app/apps/workflow/generate_task_pipeline.py", line 286, in _process_stream_response workflow_node_execution = self._handle_node_execution_start(workflow_run=workflow_run, event=event) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/core/app/task_pipeline/workflow_cycle_manage.py", line 293, in _handle_node_execution_start workflow_node_execution.tenant_id = workflow_run.tenant_id ^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 566, in __get__ return self.impl.get(state, dict_) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get value = self._fire_loader_callables(state, key, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables return state._load_expired(state, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired self.manager.expired_attribute_loader(self, toload, passive) File "/Users/fengxin09/Documents/llm/dify/api/.venv/lib/python3.11/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes raise orm_exc.DetachedInstanceError( sqlalchemy.orm.exc.DetachedInstanceError: Instance <WorkflowRun at 0x3263884d0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3) During handling of the above exception, another exception occurred: ``` ### ✔️ Expected Behavior How to fix this ? or workaround this ? ### ❌ Actual Behavior _No response_
yindo added the 🌊 feat:workflow label 2026-02-21 18:21:01 -05:00
yindo closed this issue 2026-02-21 18:21:02 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jan 4, 2025):

Hey there, @fxgeoffrey! I'm here to assist you with any bugs, questions, or even becoming a contributor. Feel free to reach out to me for help while waiting for a human maintainer. Let's squash those bugs together! 🐞

The DetachedInstanceError you're encountering indicates that the WorkflowRun instance is not bound to an active SQLAlchemy session when you attempt to access its attributes. This typically happens when an instance is accessed outside the session it was originally associated with.

Here are some steps you can take to resolve or work around this issue:

  1. Ensure Proper Session Management: Make sure that the WorkflowRun instance is always accessed within the context of an active session. You can achieve this by using a with statement to manage the session lifecycle, ensuring that the instance is queried and used within the same session context.

  2. Reattach the Instance: If you need to access a WorkflowRun instance that has become detached, you can reattach it to a session using session.merge(instance) or session.add(instance). This will associate the instance with the session again, allowing you to access its attributes.

  3. Check for Session Scope: Ensure that the session scope is correctly defined, especially in asynchronous or multi-threaded environments. Using a session factory or scoped session can help manage session lifecycles more effectively.

  4. Review Event Handling: In the context of event handling, make sure that any operations on WorkflowRun instances are performed within the session that originally loaded them. This might involve passing the session explicitly to functions that handle these operations.

By following these practices, you should be able to manage the session lifecycle more effectively and avoid DetachedInstanceError in your application.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@dosubot[bot] commented on GitHub (Jan 4, 2025): <!-- Greeting --> Hey there, @fxgeoffrey! I'm here to assist you with any bugs, questions, or even becoming a contributor. Feel free to reach out to me for help while waiting for a human maintainer. Let's squash those bugs together! 🐞 <!-- Answer --> The `DetachedInstanceError` you're encountering indicates that the `WorkflowRun` instance is not bound to an active SQLAlchemy session when you attempt to access its attributes. This typically happens when an instance is accessed outside the session it was originally associated with. Here are some steps you can take to resolve or work around this issue: 1. **Ensure Proper Session Management**: Make sure that the `WorkflowRun` instance is always accessed within the context of an active session. You can achieve this by using a `with` statement to manage the session lifecycle, ensuring that the instance is queried and used within the same session context. 2. **Reattach the Instance**: If you need to access a `WorkflowRun` instance that has become detached, you can reattach it to a session using `session.merge(instance)` or `session.add(instance)`. This will associate the instance with the session again, allowing you to access its attributes. 3. **Check for Session Scope**: Ensure that the session scope is correctly defined, especially in asynchronous or multi-threaded environments. Using a session factory or scoped session can help manage session lifecycles more effectively. 4. **Review Event Handling**: In the context of event handling, make sure that any operations on `WorkflowRun` instances are performed within the session that originally loaded them. This might involve passing the session explicitly to functions that handle these operations. By following these practices, you should be able to manage the session lifecycle more effectively and avoid `DetachedInstanceError` in your application. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/c057232e-d1c8-472e-bc4d-df36f3979ed7?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/c057232e-d1c8-472e-bc4d-df36f3979ed7?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/c057232e-d1c8-472e-bc4d-df36f3979ed7?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/c057232e-d1c8-472e-bc4d-df36f3979ed7?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/c057232e-d1c8-472e-bc4d-df36f3979ed7?feedback_type=hallucination) | [Other](https://app.dosu.dev/response-feedback/c057232e-d1c8-472e-bc4d-df36f3979ed7?feedback_type=other)</sup>
Author
Owner

@crazywoola commented on GitHub (Jan 4, 2025):

If you choose to use source code to start the application, we can not guarantee there is no bug in the latest branch. Please resolve it by yourself, otherwise I will not suggest doing so.
And this is a duplicated question, please search the existing issue before posting one.

Fixed in https://github.com/langgenius/dify/pull/11913

@crazywoola commented on GitHub (Jan 4, 2025): If you choose to use source code to start the application, we can not guarantee there is no bug in the latest branch. Please resolve it by yourself, otherwise I will not suggest doing so. And this is a duplicated question, please search the existing issue before posting one. Fixed in https://github.com/langgenius/dify/pull/11913
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#7522