When calling the chatbot api service, the metadata field in the "event": "message_end" block is an empty dictionary when calling the stream return. #5586

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

Originally created by @NDBXQ on GitHub (Sep 12, 2024).

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

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

image

✔️ Expected Behavior

image

Actual Behavior

image
Originally created by @NDBXQ on GitHub (Sep 12, 2024). ### 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.8.1 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce <img width="1125" alt="image" src="https://github.com/user-attachments/assets/7b95b42c-fc95-4de0-8ec8-c6c1a68eca5a"> ### ✔️ Expected Behavior <img width="552" alt="image" src="https://github.com/user-attachments/assets/1c444d4c-56ab-49fa-9970-afe0a6ad4f61"> ### ❌ Actual Behavior <img width="1125" alt="image" src="https://github.com/user-attachments/assets/d11feee8-ab20-4de2-a636-dbbac5421ef1">
yindo added the 🐞 bug label 2026-02-21 18:11:45 -05:00
yindo closed this issue 2026-02-21 18:11:45 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 12, 2024):

@NDBXQ Hello there! I'm here to help you with bug fixes, answer your questions, and guide you on how to contribute. Let's work together to solve the issue you're facing.

The metadata field in the "event": "message_end" block is an empty dictionary when calling the chatbot API service with the stream return because the _message_end_to_stream_response method in the AdvancedChatAppGenerateTaskPipeline class only includes metadata if _task_state.metadata is not empty. If _task_state.metadata is empty, the extras dictionary remains empty, resulting in an empty metadata field in the MessageEndStreamResponse.

Here is the relevant code snippet:

def _message_end_to_stream_response(self) -> MessageEndStreamResponse:
    """
    Message end to stream response.
    :return:
    """
    extras = {}
    if self._task_state.metadata:
        extras["metadata"] = self._task_state.metadata.copy()

        if "annotation_reply" in extras["metadata"]:
            del extras["metadata"]["annotation_reply"]

    return MessageEndStreamResponse(
        task_id=self._application_generate_entity.task_id, id=self._message.id, **extras
    )

In this method, extras["metadata"] is only populated if self._task_state.metadata is not empty. If self._task_state.metadata is empty, extras remains an empty dictionary, leading to an empty metadata field in the response.

Additionally, according to the Dify documentation, the metadata field in the event: message_end block is expected to contain model usage information and a list of retriever resources. If these details are not available or not populated during the task execution, the metadata field will remain empty [1][2][3][4].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Sep 12, 2024): <!-- Greeting --> @NDBXQ Hello there! I'm here to help you with bug fixes, answer your questions, and guide you on how to contribute. Let's work together to solve the issue you're facing. <!-- Answer --> The metadata field in the "event": "message_end" block is an empty dictionary when calling the chatbot API service with the stream return because the `_message_end_to_stream_response` method in the `AdvancedChatAppGenerateTaskPipeline` class only includes metadata if `_task_state.metadata` is not empty. If `_task_state.metadata` is empty, the extras dictionary remains empty, resulting in an empty metadata field in the `MessageEndStreamResponse`. Here is the relevant code snippet: ```python def _message_end_to_stream_response(self) -> MessageEndStreamResponse: """ Message end to stream response. :return: """ extras = {} if self._task_state.metadata: extras["metadata"] = self._task_state.metadata.copy() if "annotation_reply" in extras["metadata"]: del extras["metadata"]["annotation_reply"] return MessageEndStreamResponse( task_id=self._application_generate_entity.task_id, id=self._message.id, **extras ) ``` In this method, `extras["metadata"]` is only populated if `self._task_state.metadata` is not empty. If `self._task_state.metadata` is empty, `extras` remains an empty dictionary, leading to an empty metadata field in the response. Additionally, according to the Dify documentation, the `metadata` field in the `event: message_end` block is expected to contain model usage information and a list of retriever resources. If these details are not available or not populated during the task execution, the metadata field will remain empty <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/app/apps/advanced_chat/generate_task_pipeline.py)</sup><sup>[[2]](https://github.com/langgenius/dify/blob/main/web/app/components/develop/template/template_chat.en.mdx)</sup><sup>[[3]](https://github.com/langgenius/dify/blob/main/api/core/app/apps/chat/generate_response_converter.py)</sup><sup>[[4]](https://github.com/langgenius/dify/blob/main/api/core/app/apps/advanced_chat/generate_response_converter.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@crazywoola commented on GitHub (Sep 12, 2024):

Duplicated https://github.com/langgenius/dify/issues/8264

@crazywoola commented on GitHub (Sep 12, 2024): Duplicated https://github.com/langgenius/dify/issues/8264
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#5586