In a round of session, the same tool is called many times, and the tracing information is incorrect #5513

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

Originally created by @HeyDorvin on GitHub (Sep 10, 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.7.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

  1. call the same tool twice in a session
    image
  2. The input for two calls is the same
    image
    image
    3.The actual call is different:
    image
    image

✔️ Expected Behavior

Output correct call information.

Actual Behavior

Output incorrect call information.

Originally created by @HeyDorvin on GitHub (Sep 10, 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.7.2 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce 1. call the same tool twice in a session ![image](https://github.com/user-attachments/assets/14d70322-4100-4a33-82d9-f192360a1a4f) 2. The input for two calls is the same ![image](https://github.com/user-attachments/assets/8ae44bea-da95-4b21-a78a-d0a82b0bbe50) ![image](https://github.com/user-attachments/assets/145fb02d-653a-4f32-9d68-8ab4e87b32b7) 3.The actual call is different: ![image](https://github.com/user-attachments/assets/01055fda-2226-4a86-b824-5d8037aa9839) ![image](https://github.com/user-attachments/assets/8584c9f0-29af-4cc7-a3af-8764b749f9ff) ### ✔️ Expected Behavior Output correct call information. ### ❌ Actual Behavior Output incorrect call information.
yindo added the 🐞 bug label 2026-02-21 18:11:22 -05:00
yindo closed this issue 2026-02-21 18:11:22 -05:00
Author
Owner

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

The tracing information is incorrect when the same tool is called multiple times in a session in Dify version 0.7.2 because the trace_manager is not consistently updated or managed across multiple invocations. Specifically, the trace_manager is only passed to the agent_tool_callback.on_tool_end method but not updated or reset between calls, leading to potential inconsistencies in the tracing data [1].

To address this issue, you can ensure that the trace_manager is properly updated or reset between tool calls. This can be done by adding a new trace task in the on_tool_end method of the DifyAgentCallbackHandler class, which already has a mechanism to update the trace_manager:

def on_tool_end(
    self,
    tool_name: str,
    tool_inputs: Mapping[str, Any],
    tool_outputs: str,
    message_id: Optional[str] = None,
    timer: Optional[Any] = None,
    trace_manager: Optional[TraceQueueManager] = None
) -> None:
    """If not the final action, print out observation."""
    print_text("\n[on_tool_end]\n", color=self.color)
    print_text("Tool: " + tool_name + "\n", color=self.color)
    print_text("Inputs: " + str(tool_inputs) + "\n", color=self.color)
    print_text("Outputs: " + tool_outputs[:1000] + "\n", color=self.color)
    print_text("\n")

    if trace_manager:
        trace_manager.add_trace_task(
            TraceTask(
                TraceTaskName.TOOL_TRACE,
                message_id=message_id,
                tool_name=tool_name,
                tool_inputs=tool_inputs,
                tool_outputs=tool_outputs,
                timer=timer,
            )
        )

This ensures that each tool call is properly logged and traced, maintaining accurate tracing information across multiple invocations [2].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Sep 10, 2024): <!-- Answer --> The tracing information is incorrect when the same tool is called multiple times in a session in Dify version 0.7.2 because the `trace_manager` is not consistently updated or managed across multiple invocations. Specifically, the `trace_manager` is only passed to the `agent_tool_callback.on_tool_end` method but not updated or reset between calls, leading to potential inconsistencies in the tracing data <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/tools/tool_engine.py)</sup>. To address this issue, you can ensure that the `trace_manager` is properly updated or reset between tool calls. This can be done by adding a new trace task in the `on_tool_end` method of the `DifyAgentCallbackHandler` class, which already has a mechanism to update the `trace_manager`: ```python def on_tool_end( self, tool_name: str, tool_inputs: Mapping[str, Any], tool_outputs: str, message_id: Optional[str] = None, timer: Optional[Any] = None, trace_manager: Optional[TraceQueueManager] = None ) -> None: """If not the final action, print out observation.""" print_text("\n[on_tool_end]\n", color=self.color) print_text("Tool: " + tool_name + "\n", color=self.color) print_text("Inputs: " + str(tool_inputs) + "\n", color=self.color) print_text("Outputs: " + tool_outputs[:1000] + "\n", color=self.color) print_text("\n") if trace_manager: trace_manager.add_trace_task( TraceTask( TraceTaskName.TOOL_TRACE, message_id=message_id, tool_name=tool_name, tool_inputs=tool_inputs, tool_outputs=tool_outputs, timer=timer, ) ) ``` This ensures that each tool call is properly logged and traced, maintaining accurate tracing information across multiple invocations <sup>[[2]](https://github.com/langgenius/dify/blob/main/api/core/callback_handler/agent_tool_callback_handler.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#5513