dify icon indicating copy to clipboard operation
dify copied to clipboard

Keyword output moderation misses terms split across buffers

Open mikamikasuki opened this issue 2 weeks ago • 3 comments

Self Checks

  • [x] I have read the Contributing Guide and Language Policy.
  • [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
  • [x] I have searched for existing issues search for existing issues, including closed ones.
  • [x] I confirm that I am using English to submit this report, otherwise it will be closed.
  • [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • [x] Please do not modify this template :) and fill in all the required fields.

Dify version

main at 733454eae330f8e5c6478874a540951a42a9a14b

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

  1. Enable app output moderation with the keywords rule and configure blocked as a keyword.
  2. For a small reproduction, set MODERATION_BUFFER_SIZE=5 and MODERATION_NON_STREAM_SEGMENT_SIZE=5. With the defaults, use a 300-character streaming segment and a 2,000-character non-streaming segment.
  3. Stream the answer in chunks xxblo and cked-safe, so the keyword crosses the buffer boundary. For a non-streaming response, use xxblocked-safe with the 5-character non-streaming segment size.
  4. Observe whether the configured refusal is applied.

✔️ Expected Behavior

The configured keyword is detected across segment boundaries, and the output moderation response is applied before the matching text is released.

❌ Actual Behavior

Each segment is checked independently. The first segment contains blo and the next contains cked, so neither contains the configured keyword and the original xxblocked-safe response passes.

mikamikasuki avatar Nov 18 '26 05:11 mikamikasuki

This matches the OutputModeration path: streaming checks pass only buffer[_checked_length:], blocking checks slice independent segments, and stream completion checks only the remaining tail. KeywordsModeration then performs a case-insensitive substring search on each supplied segment, without cross-segment state. That explains why neither half of blocked triggers the refusal. This is a code-path confirmation, not an executed reproduction. [1] [2]

There are two requirements for a correction: retain enough boundary context to detect a split keyword, and withhold any suffix that could still become a keyword until the next text resolves it. Overlapping checks alone would detect the match later, but the current append_new_token() returns the entire approved segment for delivery, so part of the keyword could already have been released. Rechecking the full answer only at completion would have the same timing problem. [1]

Suggested regression coverage: your streaming and blocking examples, a match spanning three or more segments, and a final short tail. Increasing either buffer size would only move the vulnerable boundary, not remove it. [1]

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about dify with Dosu

dosubot[bot] avatar Nov 18 '26 05:11 dosubot[bot]

I'll take this one.

Plan: reproduce the split-keyword miss in OutputModeration (streaming append_new_token checks only the new segment, and the non-streaming path in moderation_completion checks independent fixed-size segments), then fix it so a keyword split across segment boundaries is detected before any part of it is released — retaining enough boundary context between segments and withholding the still-ambiguous suffix until the next text resolves it.

I'll add regression tests covering the reported streaming and non-streaming cases, a keyword spanning three or more segments, and a final short tail, and run the moderation unit tests plus lint/typecheck before opening a PR.

jimmyallinai avatar Nov 18 '26 05:11 jimmyallinai

I opened PR #46653 for this report before seeing your earlier comment. It covers the split-keyword cases in streaming and non-streaming output and includes regression tests. Since you were already planning the same fix, would you prefer to coordinate on one PR, or leave both for maintainers to compare?

mikamikasuki avatar Nov 18 '26 05:11 mikamikasuki