Keyword output moderation misses terms split across buffers
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
- Enable app output moderation with the
keywordsrule and configureblockedas a keyword. - For a small reproduction, set
MODERATION_BUFFER_SIZE=5andMODERATION_NON_STREAM_SEGMENT_SIZE=5. With the defaults, use a 300-character streaming segment and a 2,000-character non-streaming segment. - Stream the answer in chunks
xxbloandcked-safe, so the keyword crosses the buffer boundary. For a non-streaming response, usexxblocked-safewith the 5-character non-streaming segment size. - 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.
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.
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.
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?