Skip to content
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

DetachedInstanceError in Workflow Response Generation #11814

Closed
5 tasks done
fdb02983rhy opened this issue Dec 18, 2024 · 3 comments · Fixed by #11858 or #11913
Closed
5 tasks done

DetachedInstanceError in Workflow Response Generation #11814

fdb02983rhy opened this issue Dec 18, 2024 · 3 comments · Fixed by #11858 or #11913
Assignees
Labels
🐞 bug Something isn't working

Comments

@fdb02983rhy
Copy link
Contributor

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

@dosubot dosubot bot added the 🐞 bug Something isn't working label Dec 18, 2024
Copy link

dosubot bot commented 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

@crazywoola
Copy link
Member

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.

@fdb02983rhy
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
3 participants