fix(ai-aws-content-moderation): moderate decoded LLM content instead of raw body#13528
Open
shreemaan-abhishek wants to merge 1 commit into
Conversation
…of raw body The plugin sent the raw HTTP request body to AWS Comprehend, so it scored the undecoded JSON envelope (e.g. the "messages" wrapper and escape sequences like a literal backslash-u sequence) instead of the actual prompt the upstream LLM acts on. This makes the moderation see different text than the model and adds noise to the toxicity result. Make the plugin protocol-aware, like ai-aliyun-content-moderation: require application/json, parse the body, detect the client protocol via ai-protocols, and send only the normalized, decoded LLM-visible content to Comprehend. Non-AI requests (non-JSON, unparseable, or bodies that carry no LLM content) are governed by the existing fail_mode.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
ai-aws-content-moderationplugin sent the raw HTTP request body to AWS Comprehend instead of parsing the LLM request and extracting the actual prompt content. As a result, moderation scored the undecoded JSON envelope, while the upstream LLM acts on the decoded content, so the two see different text:"content":"toxic"was scored by Comprehend as the literal escaped string, whereas the upstream model decodes it totoxic.{"model":...,"messages":[...]}envelope was scored as-is, adding noise to the toxicity result.This is inconsistent with the sibling
ai-aliyun-content-moderationplugin, which is protocol-aware.What changed
Make the plugin protocol-aware, in the same style as
ai-aliyun-content-moderation:application/jsoncontent type.ai-protocols, and extract the LLM-visible content (messages[].content, Responses input/instructions, Anthropic message content, etc.).Because the plugin runs in the
rewritephase (beforeai-proxy), it detects the protocol directly rather than relying onctx.ai_client_protocol. Requests that are not recognized AI requests (non-JSON content type, unparseable bodies, or JSON that carries no LLM content) are handled by the existingfail_mode(skipby default), so non-AI traffic on Consumer-bound plugins keeps its current pass-through behavior.Behavior change
Making the plugin JSON/LLM-only is a behavior change for anyone running it on non-JSON routes. With the default
fail_mode: skip, such requests now pass through unchecked instead of being forwarded verbatim to Comprehend; setfail_mode: errorto reject them.Which issue(s) this PR fixes:
Fixes #
Checklist