Repository navigation
Conversation
Reviewer's GuideImplementationCompile toxicity patterns for redaction and merge toxicity and profanity spans in one pass, prioritizing toxicity while preserving unscanned text.
TestsCover toxicity and profanity redaction, overlap priority, scan-tail preservation, and forwarding redacted text through the pipeline.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by QodoRedact toxic phrases in moderation guard output
AI Description
Merge Risk
|
Code Review by Qodo
1.
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="sdk/python/src/openlit/guard/moderation.py" line_range="102" />
<code_context>
if self._action.value == "redact":
- transformed = self._profanity_re.sub("[REDACTED:profanity]", text)
+ redacted = self._profanity_re.sub("[REDACTED:profanity]", text)
+ transformed = _TOXICITY_RE.sub("[REDACTED:toxicity]", redacted)
return GuardResult(
</code_context>
<issue_to_address>
**Toxic text passes through**
When a TypeScript caller uses redact mode on toxicity-only text, `Moderation.evaluate` substitutes only profanity, so `transformedText` stays null and the pipeline forwards the toxic text unchanged.
Add toxicity redaction to the TypeScript moderation implementation, matching Python.
</issue_to_address>
### Comment 2
<location path="sdk/python/src/openlit/guard/moderation.py" line_range="101-102" />
<code_context>
transformed: Optional[str] = None
if self._action.value == "redact":
- transformed = self._profanity_re.sub("[REDACTED:profanity]", text)
+ redacted = self._profanity_re.sub("[REDACTED:profanity]", text)
+ transformed = _TOXICITY_RE.sub("[REDACTED:toxicity]", redacted)
return GuardResult(
</code_context>
<issue_to_address>
**Toxic phrase remains visible**
When a configured custom word matches part of a detected toxicity phrase, `Moderation.evaluate` replaces the custom word before applying `_TOXICITY_RE`, breaking the toxicity match and leaving the rest of the phrase visible to downstream guards.
Apply toxicity redaction before custom-word redaction so a custom-word replacement cannot break the toxicity match.
</issue_to_address>
### Comment 3
<location path="sdk/python/src/openlit/guard/moderation.py" line_range="102" />
<code_context>
if self._action.value == "redact":
- transformed = self._profanity_re.sub("[REDACTED:profanity]", text)
+ redacted = self._profanity_re.sub("[REDACTED:profanity]", text)
+ transformed = _TOXICITY_RE.sub("[REDACTED:toxicity]", redacted)
return GuardResult(
</code_context>
<issue_to_address>
**Long input loses its suffix**
When the input exceeds `max_scan_length` and a toxicity match occurs within the scanned prefix, `Guard.run` passes only the capped prefix to `Moderation.evaluate`, which returns it as `transformed_text`; `Pipeline.evaluate` and provider integrations then replace the full input with that prefix, dropping the suffix.
Preserve the unscanned suffix when returning the redacted text so the capped scan does not truncate the input.
</issue_to_address>
### Comment 4
<location path="sdk/python/tests/test_guard_pipeline.py" line_range="218" />
<code_context>
+
+ def test_pipeline_forwards_redacted_toxicity(self):
+ """The pipeline no longer forwards toxic text to later guards."""
+ pipeline = Pipeline(guards=[Moderation(action="redact")])
+ result = pipeline.evaluate("kill yourself", phase="preflight")
+
</code_context>
<issue_to_address>
**Downstream receives toxic text**
When the pipeline passes the original text to later guards while preserving the first guard’s redacted text as its final result, when the pipeline keeps the original input for later guards but still returns the first guard’s redacted result, `test_pipeline_forwards_redacted_toxicity` passes: line 218 configures only `Moderation`, so no later guard observes what text it receives, and lines 221–224 check only the aggregate result.
Add a second guard that records or asserts the text it receives, and verify that it sees the redacted toxicity placeholder rather than the original phrase.
</issue_to_address>Sourcery assessment
Approval pending. 4 findings to address first.
Blocking findings: sdk/python/src/openlit/guard/moderation.py:102, sdk/python/src/openlit/guard/moderation.py:102, sdk/python/src/openlit/guard/moderation.py:102, sdk/python/tests/test_guard_pipeline.py:218
Give toxicity priority over custom profanity matches on the original text, preserve unscanned suffixes, and mirror the behavior in TypeScript. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Co-Authored-By: Claude Sonnet 5 <[email protected]>
528d4d0 to
05555e7
Compare
Issue number: #2000
Change description:
Moderation(action="redact")replaced only profanity; toxicity was classified but left in the text withtransformed_text=None, so the pipeline forwarded the toxic string unchanged. Toxicity patterns are now compiled once and substituted into the text alongside profanity, producing a populatedtransformed_textwith no toxic phrase. Classification, scoring and other actions are unchanged.Checklist
Base fails the two toxicity cases; with the fix
test_guard_pipeline.pyis 19 passed. Ruff findings are pre-existing on base. Provider live tests were not run (no credentials); offline guard tests cover the change.Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.
Summary by Sourcery
Redact toxic phrases alongside profanity across the moderation guards.
Bug Fixes:
Enhancements:
Tests: