Use Chatflow, stream chat, click Stop, ERR_INCOMPLETE_CHUNKED_ENCODING #7316

Closed
opened 2026-02-21 18:19:58 -05:00 by yindo · 1 comment
Owner

Originally created by @MrXionGe on GitHub (Dec 20, 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

Self Hosted (Docker)

Steps to reproduce

https://github.com/user-attachments/assets/ac2fb763-08ef-4bd4-a9f4-56e94940fa46

As shown on video.
Using Chatflow, start a conversation and click the stop button while the conversation is producing text.
The status of API /api/chat-messages is ERR_INCOMPLETE_CHUNKED_ENCODING.
This failed state causes the program to fail to stop properly.

✔️ Expected Behavior

Stop properly.

Actual Behavior

Unable to stop correctly.

https://github.com/user-attachments/assets/9a67f2da-cc79-4b79-a630-6f148b70fadb

At the same time, the container "dify-api" has error logs.

[2024-12-20 08:01:07 +0000] [86] [ERROR] Error handling request
Traceback (most recent call last):
  File "/app/api/.venv/lib/python3.12/site-packages/gunicorn/workers/base_async.py", line 114, in handle_request
    for item in respiter:
                ^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/werkzeug/wsgi.py", line 256, in __next__
    return self._next()
           ^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded
    for item in iterable:
                ^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/flask/helpers.py", line 125, in generator
    yield from gen
  File "/app/api/libs/helper.py", line 191, in generate
    yield from response
  File "/app/api/core/app/features/rate_limiting/rate_limit.py", line 112, in __next__
    return next(self.generator)
           ^^^^^^^^^^^^^^^^^^^^
  File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 40, in _generate_simple_response
    for chunk in cls.convert_stream_simple_response(response):
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/core/app/apps/advanced_chat/generate_response_converter.py", line 98, in convert_stream_simple_response
    for chunk in stream_response:
                 ^^^^^^^^^^^^^^^
  File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 191, in _to_stream_response
    for stream_response in generator:
                           ^^^^^^^^^
  File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 222, in _wrapper_process_stream_response
    for response in self._process_stream_response(tts_publisher=tts_publisher, trace_manager=trace_manager):
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 452, in _process_stream_response
    yield self._workflow_finish_to_stream_response(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 461, in _workflow_finish_to_stream_response
    if workflow_run.created_by_role == CreatedByRole.ACCOUNT.value:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 566, in __get__
    return self.impl.get(state, dict_)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get
    value = self._fire_loader_callables(state, key, passive)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables
    return state._load_expired(state, passive)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired
    self.manager.expired_attribute_loader(self, toload, passive)
  File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes
    raise orm_exc.DetachedInstanceError(
sqlalchemy.orm.exc.DetachedInstanceError: Instance <WorkflowRun at 0x7f0e23306bd0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3)
Originally created by @MrXionGe on GitHub (Dec 20, 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 Self Hosted (Docker) ### Steps to reproduce https://github.com/user-attachments/assets/ac2fb763-08ef-4bd4-a9f4-56e94940fa46 As shown on video. Using Chatflow, start a conversation and click the stop button while the conversation is producing text. The status of API /api/chat-messages is ERR_INCOMPLETE_CHUNKED_ENCODING. This failed state causes the program to fail to stop properly. ### ✔️ Expected Behavior Stop properly. ### ❌ Actual Behavior Unable to stop correctly. https://github.com/user-attachments/assets/9a67f2da-cc79-4b79-a630-6f148b70fadb At the same time, the container "dify-api" has error logs. ``` [2024-12-20 08:01:07 +0000] [86] [ERROR] Error handling request Traceback (most recent call last): File "/app/api/.venv/lib/python3.12/site-packages/gunicorn/workers/base_async.py", line 114, in handle_request for item in respiter: ^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/werkzeug/wsgi.py", line 256, in __next__ return self._next() ^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded for item in iterable: ^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/flask/helpers.py", line 125, in generator yield from gen File "/app/api/libs/helper.py", line 191, in generate yield from response File "/app/api/core/app/features/rate_limiting/rate_limit.py", line 112, in __next__ return next(self.generator) ^^^^^^^^^^^^^^^^^^^^ File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 40, in _generate_simple_response for chunk in cls.convert_stream_simple_response(response): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/core/app/apps/advanced_chat/generate_response_converter.py", line 98, in convert_stream_simple_response for chunk in stream_response: ^^^^^^^^^^^^^^^ File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 191, in _to_stream_response for stream_response in generator: ^^^^^^^^^ File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 222, in _wrapper_process_stream_response for response in self._process_stream_response(tts_publisher=tts_publisher, trace_manager=trace_manager): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 452, in _process_stream_response yield self._workflow_finish_to_stream_response( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/core/app/task_pipeline/workflow_cycle_manage.py", line 461, in _workflow_finish_to_stream_response if workflow_run.created_by_role == CreatedByRole.ACCOUNT.value: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 566, in __get__ return self.impl.get(state, dict_) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1086, in get value = self._fire_loader_callables(state, key, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 1116, in _fire_loader_callables return state._load_expired(state, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/state.py", line 803, in _load_expired self.manager.expired_attribute_loader(self, toload, passive) File "/app/api/.venv/lib/python3.12/site-packages/sqlalchemy/orm/loading.py", line 1603, in load_scalar_attributes raise orm_exc.DetachedInstanceError( sqlalchemy.orm.exc.DetachedInstanceError: Instance <WorkflowRun at 0x7f0e23306bd0> is not bound to a Session; attribute refresh operation cannot proceed (Background on this error at: https://sqlalche.me/e/20/bhk3) ```
yindo added the 🐞 bug label 2026-02-21 18:19:58 -05:00
yindo closed this issue 2026-02-21 18:19:58 -05:00
Author
Owner

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

The error you're encountering, ERR_INCOMPLETE_CHUNKED_ENCODING, during streaming responses in the Dify application seems to be related to a DetachedInstanceError in SQLAlchemy. This error occurs when an instance of a SQLAlchemy model is accessed without being bound to an active session, which prevents attribute refresh operations from proceeding.

In the context of your issue, the error log indicates that the workflow_run.created_by_role attribute is being accessed on a WorkflowRun instance that is not bound to a session. This typically happens if the session that loaded the instance has been closed or if the instance was detached from the session.

To address this issue, you can try the following:

  1. Ensure Session Management: Make sure that the SQLAlchemy session is properly managed and that instances are not accessed outside the session's scope. This might involve ensuring that the session is open when accessing attributes or re-attaching the instance to an active session.

  2. Review Streaming Logic: Check the logic in the streaming process, particularly in the _to_stream_response and _workflow_finish_to_stream_response functions, to ensure that instances are accessed within a valid session context.

  3. Error Handling: Implement error handling to catch and manage DetachedInstanceError exceptions, possibly by re-attaching the instance to a session or by logging the error for further investigation.

These steps should help mitigate the issue and ensure that the application can handle stopping a conversation without encountering this error.

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 20, 2024): <!-- Answer --> The error you're encountering, `ERR_INCOMPLETE_CHUNKED_ENCODING`, during streaming responses in the Dify application seems to be related to a `DetachedInstanceError` in SQLAlchemy. This error occurs when an instance of a SQLAlchemy model is accessed without being bound to an active session, which prevents attribute refresh operations from proceeding. In the context of your issue, the error log indicates that the `workflow_run.created_by_role` attribute is being accessed on a `WorkflowRun` instance that is not bound to a session. This typically happens if the session that loaded the instance has been closed or if the instance was detached from the session. To address this issue, you can try the following: 1. **Ensure Session Management**: Make sure that the SQLAlchemy session is properly managed and that instances are not accessed outside the session's scope. This might involve ensuring that the session is open when accessing attributes or re-attaching the instance to an active session. 2. **Review Streaming Logic**: Check the logic in the streaming process, particularly in the `_to_stream_response` and `_workflow_finish_to_stream_response` functions, to ensure that instances are accessed within a valid session context. 3. **Error Handling**: Implement error handling to catch and manage `DetachedInstanceError` exceptions, possibly by re-attaching the instance to a session or by logging the error for further investigation. These steps should help mitigate the issue and ensure that the application can handle stopping a conversation without encountering this error. <!-- 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/9f9583c2-eb08-4428-9e23-5f2b07f6f566?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/9f9583c2-eb08-4428-9e23-5f2b07f6f566?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/9f9583c2-eb08-4428-9e23-5f2b07f6f566?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/9f9583c2-eb08-4428-9e23-5f2b07f6f566?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/9f9583c2-eb08-4428-9e23-5f2b07f6f566?feedback_type=hallucination) | [Other](https://app.dosu.dev/response-feedback/9f9583c2-eb08-4428-9e23-5f2b07f6f566?feedback_type=other)</sup>
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#7316