Skip to content

fix(workflow): guard on_tool_execution stdout traces behind DEBUG#38200

Merged
MRZHUH merged 1 commit into
langgenius:mainfrom
MRZHUH:fix/workflow-tool-callback-debug-guard
Jun 30, 2026
Merged

fix(workflow): guard on_tool_execution stdout traces behind DEBUG#38200
MRZHUH merged 1 commit into
langgenius:mainfrom
MRZHUH:fix/workflow-tool-callback-debug-guard

Conversation

@MRZHUH

@MRZHUH MRZHUH commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

DifyWorkflowCallbackHandler.on_tool_execution was the only callback in the DifyAgentCallbackHandler family that wrote to stdout without the if dify_config.DEBUG: guard that every sibling method uses (on_tool_start, on_tool_end, on_tool_error, on_agent_start, on_agent_finish, on_datasource_start; ignore_agent returns not dify_config.DEBUG). This wraps its print_text(...) calls in the same guard.

Two improvements when DEBUG is off (the default):

  • No unconditional stdout output — the calls use a raw print() that bypasses LOG_LEVEL, so they were emitted in every deployment.
  • tool_output.model_dump_json() is no longer invoked just to build a 1000-char preview, removing avoidable full serialization of large tool outputs on a hot path.

Functional behavior is unchanged — outputs are still yielded; only the debug printing is gated.

Local checks: ruff check is clean on the changed files and tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py passes (10 passed), with a new case covering the DEBUG-off short-circuit.

Fixes #38199

Screenshots

Before After
N/A — backend stdout/logging behavior N/A

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and cd web && pnpm exec vp staged (frontend) to appease the lint gods — ran ruff check (clean) and the affected unit tests (10 passed); full make type-check not run locally

DifyWorkflowCallbackHandler.on_tool_execution printed tool outputs to
stdout on every tool-node execution, unlike every sibling callback in
DifyAgentCallbackHandler which guards print_text with dify_config.DEBUG.

Besides the unconditional stdout noise (a raw print(), not gated by
LOG_LEVEL), it called tool_output.model_dump_json() to fully serialize
each output just to print a 1000-char preview. Wrap the prints in the
same DEBUG guard so the serialization is skipped when disabled.
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-06-30 07:06:00.034157800 +0000
+++ /tmp/pyrefly_pr.txt	2026-06-30 07:05:45.775067530 +0000
@@ -4238,13 +4238,13 @@
 ERROR Argument `None` is not assignable to parameter `dataset_id` with type `str` in function `core.callback_handler.index_tool_callback_handler.DatasetIndexToolCallbackHandler.on_query` [bad-argument-type]
   --> tests/unit_tests/core/callback_handler/test_index_tool_callback_handler.py:72:32
 ERROR Argument `list[DummyToolInvokeMessage]` is not assignable to parameter `tool_outputs` with type `Iterable[ToolInvokeMessage]` in function `core.callback_handler.workflow_tool_callback_handler.DifyWorkflowCallbackHandler.on_tool_execution` [bad-argument-type]
-  --> tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py:47:30
+  --> tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py:55:30
 ERROR Argument `list[DummyToolInvokeMessage]` is not assignable to parameter `tool_outputs` with type `Iterable[ToolInvokeMessage]` in function `core.callback_handler.workflow_tool_callback_handler.DifyWorkflowCallbackHandler.on_tool_execution` [bad-argument-type]
-  --> tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py:79:30
+  --> tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py:89:30
 ERROR Argument `list[DummyToolInvokeMessage]` is not assignable to parameter `tool_outputs` with type `Iterable[ToolInvokeMessage]` in function `core.callback_handler.workflow_tool_callback_handler.DifyWorkflowCallbackHandler.on_tool_execution` [bad-argument-type]
-   --> tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py:139:30
+   --> tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py:174:30
 ERROR Argument `list[DummyToolInvokeMessage]` is not assignable to parameter `tool_outputs` with type `Iterable[ToolInvokeMessage]` in function `core.callback_handler.workflow_tool_callback_handler.DifyWorkflowCallbackHandler.on_tool_execution` [bad-argument-type]
-   --> tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py:181:30
+   --> tests/unit_tests/core/callback_handler/test_workflow_tool_callback_handler.py:218:30
 ERROR Class member `ConcreteDatasourcePlugin.datasource_provider_type` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
   --> tests/unit_tests/core/datasource/__base/test_datasource_plugin.py:15:9
 ERROR Class member `ConcreteDatasourcePluginProviderController.get_datasource` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]

@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 51.49% 51.49% -0.01%
Strict coverage 51.01% 51.00% -0.01%
Typed symbols 30,861 30,864 +3
Untyped symbols 29,350 29,359 +9
Modules 2932 2932 0

@hjlarry hjlarry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 30, 2026
@MRZHUH MRZHUH enabled auto-merge June 30, 2026 07:18
@MRZHUH MRZHUH added this pull request to the merge queue Jun 30, 2026
Merged via the queue into langgenius:main with commit 03d59db Jun 30, 2026
31 checks passed
@MRZHUH MRZHUH deleted the fix/workflow-tool-callback-debug-guard branch June 30, 2026 07:35
hjlarry pushed a commit to hjlarry/dify that referenced this pull request Jul 1, 2026
hjlarry pushed a commit that referenced this pull request Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

on_tool_execution prints tool outputs to stdout unconditionally (missing DEBUG guard)

2 participants