error in annotation reply #4924

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

Originally created by @viewhang on GitHub (Aug 6, 2024).

Originally assigned to: @laipz8200, @JohnJyong 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.6.16

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

enable the annotated reply, an error occurs when the answer hits the annotated content. It is normal in 0.6.15, but an error occurs in the latest version 0.6.16

When the question does not hit the annotation, everything is OK

image

✔️ Expected Behavior

reply the annotated content

Actual Behavior

nothing relpy

image
[2024-08-07 01:56:33 +0000] [46] [ERROR] Error handling request /api/chat-messages
Traceback (most recent call last):
  File "/app/api/.venv/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 55, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/app/api/.venv/lib/python3.10/site-packages/gunicorn/workers/ggevent.py", line 128, in handle_request
    super().handle_request(listener_name, req, sock, addr)
  File "/app/api/.venv/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 115, in handle_request
    for item in respiter:
  File "/app/api/.venv/lib/python3.10/site-packages/werkzeug/wsgi.py", line 256, in __next__
    return self._next()
  File "/app/api/.venv/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded
    for item in iterable:
  File "/app/api/.venv/lib/python3.10/site-packages/flask/helpers.py", line 113, in generator
    yield from gen
  File "/app/api/libs/helper.py", line 168, in generate
    yield from response
  File "/app/api/core/app/features/rate_limiting/rate_limit.py", line 110, in __next__
    return next(self.generator)
  File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 37, 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 94, in convert_stream_simple_response
    for chunk in stream_response:
  File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 179, in _to_stream_response
    for stream_response in generator:
  File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 206, in _wrapper_process_stream_response
    for response in self._process_stream_response(publisher=publisher, trace_manager=trace_manager):
  File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 247, in _process_stream_response
    if hasattr(message.event, 'metadata') and message.event.metadata.get('is_answer_previous_node', False) and publisher:
AttributeError: 'NoneType' object has no attribute 'get'
Originally created by @viewhang on GitHub (Aug 6, 2024). Originally assigned to: @laipz8200, @JohnJyong 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.6.16 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce enable the annotated reply, an error occurs when the answer hits the annotated content. It is normal in 0.6.15, but an error occurs in the latest version 0.6.16 When the question does not hit the annotation, everything is OK <img width="997" alt="image" src="https://github.com/user-attachments/assets/2d016f32-bd55-4de0-a70d-9cbbd74cd6a3"> ### ✔️ Expected Behavior reply the annotated content ### ❌ Actual Behavior nothing relpy <img width="1143" alt="image" src="https://github.com/user-attachments/assets/39129f67-2fb8-4254-80e4-995dbad84348"> ``` [2024-08-07 01:56:33 +0000] [46] [ERROR] Error handling request /api/chat-messages Traceback (most recent call last): File "/app/api/.venv/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 55, in handle self.handle_request(listener_name, req, client, addr) File "/app/api/.venv/lib/python3.10/site-packages/gunicorn/workers/ggevent.py", line 128, in handle_request super().handle_request(listener_name, req, sock, addr) File "/app/api/.venv/lib/python3.10/site-packages/gunicorn/workers/base_async.py", line 115, in handle_request for item in respiter: File "/app/api/.venv/lib/python3.10/site-packages/werkzeug/wsgi.py", line 256, in __next__ return self._next() File "/app/api/.venv/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 32, in _iter_encoded for item in iterable: File "/app/api/.venv/lib/python3.10/site-packages/flask/helpers.py", line 113, in generator yield from gen File "/app/api/libs/helper.py", line 168, in generate yield from response File "/app/api/core/app/features/rate_limiting/rate_limit.py", line 110, in __next__ return next(self.generator) File "/app/api/core/app/apps/base_app_generate_response_converter.py", line 37, 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 94, in convert_stream_simple_response for chunk in stream_response: File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 179, in _to_stream_response for stream_response in generator: File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 206, in _wrapper_process_stream_response for response in self._process_stream_response(publisher=publisher, trace_manager=trace_manager): File "/app/api/core/app/apps/advanced_chat/generate_task_pipeline.py", line 247, in _process_stream_response if hasattr(message.event, 'metadata') and message.event.metadata.get('is_answer_previous_node', False) and publisher: AttributeError: 'NoneType' object has no attribute 'get' ```
yindo added the 🐞 bug label 2026-02-21 18:08:37 -05:00
yindo closed this issue 2026-02-21 18:08:38 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 6, 2024):

@viewhang Hello! I'm here to help you with any bugs, questions, or contributions you might have for the repository. Let me know how I can assist you!

The error in the annotation reply feature in Dify version 0.6.16 is likely due to a missing indentation in the if documents: block. This indentation issue causes the code inside the if documents: block to not execute, which includes the logic for retrieving and returning the annotation.

Here is the corrected code snippet with proper indentation:

documents = vector.search_by_vector(
    query=query,
    top_k=1,
    score_threshold=score_threshold,
    filter={
        'group_id': [dataset.id]
    }
)

if documents:
    annotation_id = documents[0].metadata['annotation_id']
    score = documents[0].metadata['score']
    annotation = AppAnnotationService.get_annotation_by_id(annotation_id)
    if annotation:
        if invoke_from in [InvokeFrom.SERVICE_API, InvokeFrom.WEB_APP]:
            from_source = 'api'
        else:
            from_source = 'console'

        # insert annotation history
        AppAnnotationService.add_annotation_history(annotation.id,
                                                    app_record.id,
                                                    annotation.question,
                                                    annotation.content,
                                                    query,
                                                    user_id,
                                                    message.id,
                                                    from_source,
                                                    score)

        return annotation

This correction ensures that the code for handling the documents and returning the annotation is executed properly [1].

Additionally, ensure that the _handle_annotation_reply method in the MessageCycleManage class is being called correctly and that the annotation ID is being passed properly:

def _handle_annotation_reply(self, event: QueueAnnotationReplyEvent) -> Optional[MessageAnnotation]:
    """
    Handle annotation reply.
    :param event: event
    :return:
    """
    annotation = AppAnnotationService.get_annotation_by_id(event.message_annotation_id)
    if annotation:
        account = annotation.account
        self._task_state.metadata['annotation_reply'] = {
            'id': annotation.id,
            'account': {
                'id': annotation.account_id,
                'name': account.name if account else 'Dify user'
            }
        }

        return annotation

    return None

Ensure that the QueueAnnotationReplyEvent is being triggered correctly and that the message_annotation_id is valid [2].

Moreover, verify that the handle_annotation_reply method in the AdvancedChatAppRunner class is correctly querying and publishing the annotation reply event:

def handle_annotation_reply(self, app_record: App,
                            message: Message,
                            query: str,
                            queue_manager: AppQueueManager,
                            app_generate_entity: AdvancedChatAppGenerateEntity) -> bool:
    """
    Handle annotation reply
    :param app_record: app record
    :param message: message
    :param query: query
    :param queue_manager: application queue manager
    :param app_generate_entity: application generate entity
    """
    # annotation reply
    annotation_reply = self.query_app_annotations_to_reply(
        app_record=app_record,
        message=message,
        query=query,
        user_id=app_generate_entity.user_id,
        invoke_from=app_generate_entity.invoke_from
    )

    if annotation_reply:
        queue_manager.publish(
            QueueAnnotationReplyEvent(message_annotation_id=annotation_reply.id),
            PublishFrom.APPLICATION_MANAGER
        )

        self._stream_output(
            queue_manager=queue_manager,
            text=annotation_reply.content,
            stream=app_generate_entity.stream,
            stopped_by=QueueStopEvent.StopBy.ANNOTATION_REPLY
        )
        return True

    return False

Ensure that the query_app_annotations_to_reply method is correctly fetching the annotation and that the QueueAnnotationReplyEvent is being published properly [3].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Aug 6, 2024): <!-- Greeting --> @viewhang Hello! I'm here to help you with any bugs, questions, or contributions you might have for the repository. Let me know how I can assist you! <!-- Answer --> The error in the annotation reply feature in Dify version 0.6.16 is likely due to a missing indentation in the `if documents:` block. This indentation issue causes the code inside the `if documents:` block to not execute, which includes the logic for retrieving and returning the annotation. Here is the corrected code snippet with proper indentation: ```python documents = vector.search_by_vector( query=query, top_k=1, score_threshold=score_threshold, filter={ 'group_id': [dataset.id] } ) if documents: annotation_id = documents[0].metadata['annotation_id'] score = documents[0].metadata['score'] annotation = AppAnnotationService.get_annotation_by_id(annotation_id) if annotation: if invoke_from in [InvokeFrom.SERVICE_API, InvokeFrom.WEB_APP]: from_source = 'api' else: from_source = 'console' # insert annotation history AppAnnotationService.add_annotation_history(annotation.id, app_record.id, annotation.question, annotation.content, query, user_id, message.id, from_source, score) return annotation ``` This correction ensures that the code for handling the documents and returning the annotation is executed properly <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/app/features/annotation_reply/annotation_reply.py)</sup>. Additionally, ensure that the `_handle_annotation_reply` method in the `MessageCycleManage` class is being called correctly and that the annotation ID is being passed properly: ```python def _handle_annotation_reply(self, event: QueueAnnotationReplyEvent) -> Optional[MessageAnnotation]: """ Handle annotation reply. :param event: event :return: """ annotation = AppAnnotationService.get_annotation_by_id(event.message_annotation_id) if annotation: account = annotation.account self._task_state.metadata['annotation_reply'] = { 'id': annotation.id, 'account': { 'id': annotation.account_id, 'name': account.name if account else 'Dify user' } } return annotation return None ``` Ensure that the `QueueAnnotationReplyEvent` is being triggered correctly and that the `message_annotation_id` is valid <sup>[[2]](https://github.com/langgenius/dify/blob/main/api/core/app/task_pipeline/message_cycle_manage.py)</sup>. Moreover, verify that the `handle_annotation_reply` method in the `AdvancedChatAppRunner` class is correctly querying and publishing the annotation reply event: ```python def handle_annotation_reply(self, app_record: App, message: Message, query: str, queue_manager: AppQueueManager, app_generate_entity: AdvancedChatAppGenerateEntity) -> bool: """ Handle annotation reply :param app_record: app record :param message: message :param query: query :param queue_manager: application queue manager :param app_generate_entity: application generate entity """ # annotation reply annotation_reply = self.query_app_annotations_to_reply( app_record=app_record, message=message, query=query, user_id=app_generate_entity.user_id, invoke_from=app_generate_entity.invoke_from ) if annotation_reply: queue_manager.publish( QueueAnnotationReplyEvent(message_annotation_id=annotation_reply.id), PublishFrom.APPLICATION_MANAGER ) self._stream_output( queue_manager=queue_manager, text=annotation_reply.content, stream=app_generate_entity.stream, stopped_by=QueueStopEvent.StopBy.ANNOTATION_REPLY ) return True return False ``` Ensure that the `query_app_annotations_to_reply` method is correctly fetching the annotation and that the `QueueAnnotationReplyEvent` is being published properly <sup>[[3]](https://github.com/langgenius/dify/blob/main/api/core/app/apps/advanced_chat/app_runner.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#4924