The regenerated conversation history might be incorrect. #13382

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

Originally created by @LNZ001 on GitHub (Apr 26, 2025).

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

v1.3.0

Cloud or Self Hosted

Self Hosted (Source), Self Hosted (Docker)

Steps to reproduce

Because the method for retrieving conversation history, TokenBufferMemory, does not use parent_message_id, it always retrieves the conversation history based on the latest record, rather than retrieving the history based on the parent_message_id.

✔️ Expected Behavior

Derive the current record's conversation history based on the provided parent_message_id.

Actual Behavior

The conversation history is derived based on the latest message record, which may actually be unrelated to the current record.

Originally created by @LNZ001 on GitHub (Apr 26, 2025). ### 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 v1.3.0 ### Cloud or Self Hosted Self Hosted (Source), Self Hosted (Docker) ### Steps to reproduce Because the method for retrieving conversation history, TokenBufferMemory, does not use `parent_message_id`, it always retrieves the conversation history based on the latest record, rather than retrieving the history based on the `parent_message_id`. ### ✔️ Expected Behavior Derive the current record's conversation history based on the provided parent_message_id. ### ❌ Actual Behavior The conversation history is derived based on the latest message record, which may actually be unrelated to the current record.
yindo added the 🐞 bug label 2026-02-21 19:11:55 -05:00
yindo closed this issue 2026-02-21 19:11:55 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Apr 26, 2025):

Hello @LNZ001! I'm Dosu, your friendly bot here to help you tackle bugs, answer questions, and guide you in becoming a contributor. I'm here while you wait for a human maintainer. Let's work together to resolve your issue!

I found a similar discussion regarding the use of the parent_message_id parameter in the chat messages API, but it remains unsolved [1].

To address the issue you're experiencing with the TokenBufferMemory method not using parent_message_id, you can modify the method to retrieve conversation history based on parent_message_id. Here's a suggested approach:

class TokenBufferMemory:
    def __init__(self, conversation: Conversation, model_instance: ModelInstance) -> None:
        self.conversation = conversation
        self.model_instance = model_instance

    def get_history_prompt_messages(
        self, max_token_limit: int = 2000, message_limit: Optional[int] = None
    ) -> Sequence[PromptMessage]:
        app_record = self.conversation.app

        query = (
            db.session.query(
                Message.id,
                Message.query,
                Message.answer,
                Message.created_at,
                Message.workflow_run_id,
                Message.parent_message_id,
                Message.answer_tokens,
            )
            .filter(
                Message.conversation_id == self.conversation.id,
            )
            .order_by(Message.created_at.desc())
        )

        if message_limit and message_limit > 0:
            message_limit = min(message_limit, 500)
        else:
            message_limit = 500

        messages = query.limit(message_limit).all()

        # Modify the logic to extract messages based on parent_message_id
        parent_message_id = self.conversation.parent_message_id  # Assuming parent_message_id is available
        thread_messages = [msg for msg in messages if msg.parent_message_id == parent_message_id]

        # for newly created message, its answer is temporarily empty, we don't need to add it to memory
        if thread_messages and not thread_messages[0].answer and thread_messages[0].answer_tokens == 0:
            thread_messages.pop(0)

        messages = list(reversed(thread_messages))

This modification assumes that parent_message_id is available in the conversation object. The logic now filters messages based on parent_message_id to retrieve the relevant conversation history [2].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Apr 26, 2025): <!-- Greeting --> Hello @LNZ001! I'm [Dosu](https://dosu.dev), your friendly bot here to help you tackle bugs, answer questions, and guide you in becoming a contributor. I'm here while you wait for a human maintainer. Let's work together to resolve your issue! <!-- Answer --> I found a similar discussion regarding the use of the `parent_message_id` parameter in the chat messages API, but it remains unsolved <sup>[[1]](https://github.com/langgenius/dify/discussions/13733)</sup>. To address the issue you're experiencing with the `TokenBufferMemory` method not using `parent_message_id`, you can modify the method to retrieve conversation history based on `parent_message_id`. Here's a suggested approach: ```python class TokenBufferMemory: def __init__(self, conversation: Conversation, model_instance: ModelInstance) -> None: self.conversation = conversation self.model_instance = model_instance def get_history_prompt_messages( self, max_token_limit: int = 2000, message_limit: Optional[int] = None ) -> Sequence[PromptMessage]: app_record = self.conversation.app query = ( db.session.query( Message.id, Message.query, Message.answer, Message.created_at, Message.workflow_run_id, Message.parent_message_id, Message.answer_tokens, ) .filter( Message.conversation_id == self.conversation.id, ) .order_by(Message.created_at.desc()) ) if message_limit and message_limit > 0: message_limit = min(message_limit, 500) else: message_limit = 500 messages = query.limit(message_limit).all() # Modify the logic to extract messages based on parent_message_id parent_message_id = self.conversation.parent_message_id # Assuming parent_message_id is available thread_messages = [msg for msg in messages if msg.parent_message_id == parent_message_id] # for newly created message, its answer is temporarily empty, we don't need to add it to memory if thread_messages and not thread_messages[0].answer and thread_messages[0].answer_tokens == 0: thread_messages.pop(0) messages = list(reversed(thread_messages)) ``` This modification assumes that `parent_message_id` is available in the `conversation` object. The logic now filters messages based on `parent_message_id` to retrieve the relevant conversation history <sup>[[2]](https://github.com/langgenius/dify/blob/main/api/core/memory/token_buffer_memory.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/0d8db8a5-1d50-4873-a458-fe2a9bcf1564?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/0d8db8a5-1d50-4873-a458-fe2a9bcf1564?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/0d8db8a5-1d50-4873-a458-fe2a9bcf1564?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/0d8db8a5-1d50-4873-a458-fe2a9bcf1564?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/0d8db8a5-1d50-4873-a458-fe2a9bcf1564?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/0d8db8a5-1d50-4873-a458-fe2a9bcf1564?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/0d8db8a5-1d50-4873-a458-fe2a9bcf1564?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#13382