DetachedInstanceError in Workflow Response Generation #7270

Closed
opened 2026-02-21 18:19:44 -05:00 by yindo · 3 comments
Owner

Originally created by @fdb02983rhy on GitHub (Dec 18, 2024).

Originally assigned to: @laipz8200 on GitHub.

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.1

Cloud or Self Hosted

Cloud, Self Hosted (Docker)

Steps to reproduce

test.yml.zip
image

✔️ Expected Behavior

No response

Actual Behavior

api-1 | [2024-12-18 18:17:13 +0000] [24] [ERROR] Error handling request
api-1 | Traceback (most recent call last):
api-1 | File "/app/api/.venv/lib/python3.12/site-packages/gunicorn/workers/base_async.py", line 114, in handle_request
api-1 | for item in respiter:
api-1 | ^^^^^^^^
api-1 | File "/app/api/.venv/lib/python3.12/site-packages/werkzeug/wsgi.py", line 256, in next
api-1 | return self._next()
api-1 | ^^^^^^^^^^^^
api-1 | File "/app/api/.venv/lib/python3.12/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded
api-1 | for item in iterable:
api-1 | ^^^^^^^^
api-1 | File "/app/api/.venv/lib/python3.12/site-packages/flask/helpers.py", line 125, in generator
api-1 | yield from gen
api-1 | File "/app/api/libs/helper.py", line 191, in generate
api-1 | yield from response
api-1 | File "/app/api/core/app/features/rate_limiting/rate_limit.py", line 112, in next
api-1 | return next(self.generator)
api-1 | ^^^^^^^^^^^^^^^^^^^^
api-1 | File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 27, in _generate_full_response
api-1 | for chunk in cls.convert_stream_full_response(response):
api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
api-1 | File "/app/api/core/app/apps/workflow/generate_response_converter.py", line 46, in convert_stream_full_response
api-1 | for chunk in stream_response:
api-1 | ^^^^^^^^^^^^^^^
api-1 | File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 167, in _to_stream_response
api-1 | for stream_response in generator:
api-1 | ^^^^^^^^^
api-1 | File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 196, in _wrapper_process_stream_response
api-1 | for response in self._process_stream_response(tts_publisher=tts_publisher, trace_manager=trace_manager):
api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
api-1 | File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 397, in _process_stream_response
api-1 | yield self._workflow_finish_to_stream_response(
api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
api-1 | File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 461, in workflow_finish_to_stream_response
api-1 | if workflow_run.created_by_role == CreatedByRole.ACCOUNT.value:
api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 566, in get
api-1 | return self.impl.get(state, dict) # type: ignore[no-any-return]
api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get
api-1 | value = self._fire_loader_callables(state, key, passive)
api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables
api-1 | return state._load_expired(state, passive)
api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired
api-1 | self.manager.expired_attribute_loader(self, toload, passive)
api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes
api-1 | raise orm_exc.DetachedInstanceError(
api-1 | sqlalchemy.orm.exc.DetachedInstanceError: Instance <WorkflowRun at 0x7fa2cd4e8f80> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)

image

Originally created by @fdb02983rhy on GitHub (Dec 18, 2024). Originally assigned to: @laipz8200 on GitHub. ### 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.1 ### Cloud or Self Hosted Cloud, Self Hosted (Docker) ### Steps to reproduce [test.yml.zip](https://github.com/user-attachments/files/18187407/test.yml.zip) ![image](https://github.com/user-attachments/assets/5a41dd96-0d78-4034-888f-551d80f5c461) ### ✔️ Expected Behavior _No response_ ### ❌ Actual Behavior ``` api-1 | [2024-12-18 18:17:13 +0000] [24] [ERROR] Error handling request api-1 | Traceback (most recent call last): api-1 | File "/app/api/.venv/lib/python3.12/site-packages/gunicorn/workers/base_async.py", line 114, in handle_request api-1 | for item in respiter: api-1 | ^^^^^^^^ api-1 | File "/app/api/.venv/lib/python3.12/site-packages/werkzeug/wsgi.py", line 256, in next api-1 | return self._next() api-1 | ^^^^^^^^^^^^ api-1 | File "/app/api/.venv/lib/python3.12/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded api-1 | for item in iterable: api-1 | ^^^^^^^^ api-1 | File "/app/api/.venv/lib/python3.12/site-packages/flask/helpers.py", line 125, in generator api-1 | yield from gen api-1 | File "/app/api/libs/helper.py", line 191, in generate api-1 | yield from response api-1 | File "/app/api/core/app/features/rate_limiting/rate_limit.py", line 112, in next api-1 | return next(self.generator) api-1 | ^^^^^^^^^^^^^^^^^^^^ api-1 | File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 27, in _generate_full_response api-1 | for chunk in cls.convert_stream_full_response(response): api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ api-1 | File "/app/api/core/app/apps/workflow/generate_response_converter.py", line 46, in convert_stream_full_response api-1 | for chunk in stream_response: api-1 | ^^^^^^^^^^^^^^^ api-1 | File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 167, in _to_stream_response api-1 | for stream_response in generator: api-1 | ^^^^^^^^^ api-1 | File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 196, in _wrapper_process_stream_response api-1 | for response in self._process_stream_response(tts_publisher=tts_publisher, trace_manager=trace_manager): api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ api-1 | File "/app/api/core/app/apps/workflow/generate_task_pipeline.py", line 397, in _process_stream_response api-1 | yield self._workflow_finish_to_stream_response( api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ api-1 | File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 461, in workflow_finish_to_stream_response api-1 | if workflow_run.created_by_role == CreatedByRole.ACCOUNT.value: api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 566, in get api-1 | return self.impl.get(state, dict) # type: ignore[no-any-return] api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get api-1 | value = self._fire_loader_callables(state, key, passive) api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables api-1 | return state._load_expired(state, passive) api-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired api-1 | self.manager.expired_attribute_loader(self, toload, passive) api-1 | File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes api-1 | raise orm_exc.DetachedInstanceError( api-1 | sqlalchemy.orm.exc.DetachedInstanceError: Instance <WorkflowRun at 0x7fa2cd4e8f80> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3) ``` ![image](https://github.com/user-attachments/assets/878541d6-710e-4d39-b890-e77470d2cf8f)
yindo added the 🐞 bug label 2026-02-21 18:19:44 -05:00
yindo closed this issue 2026-02-21 18:19:45 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Dec 18, 2024):

The DetachedInstanceError you're encountering typically occurs when an object is accessed outside of a session in SQLAlchemy. This error suggests that the WorkflowRun instance is not associated with an active session when trying to access its attributes.

Here are some steps to resolve this issue:

  1. Ensure Active Session: Make sure that the WorkflowRun instance is accessed within an active session. This can be done by ensuring that any operations on the WorkflowRun object are performed within a session context, such as using a with block to manage the session lifecycle.

  2. Session Management: Check the session management patterns in your code. Ensure that the session is not prematurely closed or expired before the WorkflowRun instance is fully processed. You can use session.refresh(instance) to re-associate a detached instance with the session if needed.

  3. Lazy Loading: If the created_by_role attribute is lazily loaded, ensure that the session is still active when accessing it. You might need to eagerly load this attribute when querying the WorkflowRun instance to avoid accessing it outside of a session.

  4. Refetching Instances: If the instance is detached due to being fetched in a different session, consider refetching it in the current session before accessing its attributes.

By ensuring that the WorkflowRun instance is always accessed within an active session, you should be able to avoid the DetachedInstanceError. If the issue persists, reviewing the specific session management logic in the generate_task_pipeline.py and workflow_cycle_manage.py files might provide further insights [1][2].

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 (Dec 18, 2024): <!-- Answer --> The `DetachedInstanceError` you're encountering typically occurs when an object is accessed outside of a session in SQLAlchemy. This error suggests that the `WorkflowRun` instance is not associated with an active session when trying to access its attributes. Here are some steps to resolve this issue: 1. **Ensure Active Session**: Make sure that the `WorkflowRun` instance is accessed within an active session. This can be done by ensuring that any operations on the `WorkflowRun` object are performed within a session context, such as using a `with` block to manage the session lifecycle. 2. **Session Management**: Check the session management patterns in your code. Ensure that the session is not prematurely closed or expired before the `WorkflowRun` instance is fully processed. You can use `session.refresh(instance)` to re-associate a detached instance with the session if needed. 3. **Lazy Loading**: If the `created_by_role` attribute is lazily loaded, ensure that the session is still active when accessing it. You might need to eagerly load this attribute when querying the `WorkflowRun` instance to avoid accessing it outside of a session. 4. **Refetching Instances**: If the instance is detached due to being fetched in a different session, consider refetching it in the current session before accessing its attributes. By ensuring that the `WorkflowRun` instance is always accessed within an active session, you should be able to avoid the `DetachedInstanceError`. If the issue persists, reviewing the specific session management logic in the `generate_task_pipeline.py` and `workflow_cycle_manage.py` files might provide further insights <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/app/task_pipeline/workflow_cycle_manage.py)</sup><sup>[[2]](https://github.com/langgenius/dify/blob/main/api/core/app/apps/workflow/generate_task_pipeline.py)</sup>. <!-- 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/de0bbbde-20dc-44b3-9657-9bbf3a32cc26?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/de0bbbde-20dc-44b3-9657-9bbf3a32cc26?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/de0bbbde-20dc-44b3-9657-9bbf3a32cc26?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/de0bbbde-20dc-44b3-9657-9bbf3a32cc26?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/de0bbbde-20dc-44b3-9657-9bbf3a32cc26?feedback_type=hallucination) | [Other](https://app.dosu.dev/response-feedback/de0bbbde-20dc-44b3-9657-9bbf3a32cc26?feedback_type=other)</sup>
Author
Owner

@crazywoola commented on GitHub (Dec 18, 2024):

FYI, Claude does not have a role called system, can you try to remove that. But I don't think this is raised by this. We will take a look later.

@crazywoola commented on GitHub (Dec 18, 2024): FYI, Claude does not have a role called `system`, can you try to remove that. But I don't think this is raised by this. We will take a look later.
Author
Owner

@fdb02983rhy commented on GitHub (Dec 19, 2024):

FYI, Claude does not have a role called system, can you try to remove that. But I don't think this is raised by this. We will take a look later.

Could you please guide me how to remove the system msg?
image

@fdb02983rhy commented on GitHub (Dec 19, 2024): > FYI, Claude does not have a role called `system`, can you try to remove that. But I don't think this is raised by this. We will take a look later. Could you please guide me how to remove the system msg? ![image](https://github.com/user-attachments/assets/dbddbff8-8aed-4bb5-9874-7fe811289b87)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#7270