Skip to content

fix(guardrails): Propagate mutated text to proxied request/response - #813

Merged
JashG merged 5 commits into
mainfrom
jgulabrai/guardrails-pii-propagate
Jul 22, 2026
Merged

fix(guardrails): Propagate mutated text to proxied request/response#813
JashG merged 5 commits into
mainfrom
jgulabrai/guardrails-pii-propagate

Conversation

@JashG

@JashG JashG commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Write input-rail masked/transformed text back onto the last user message in the proxied request (string content only). This matches the library behavior I observe - i.e. only the last user message is modified by rails that might redact/modify content (such as PII redaction).
  • Write output-rail masked text back onto choices[0].message.content.
  • Mutate request.body in place (sanitize + messages replace) instead of reconstructing InferenceRequest, so fields like typed_body are preserved; IGW re-derives typed_body after the plugin.

Summary by CodeRabbit

  • New Features
    • Input rail transformations now sanitize and forward updated upstream messages, rewriting the last role=user message content when text-only.
    • Output rail transformations are propagated into the assistant content returned to clients.
  • Bug Fixes
    • Prevents masking/write-back for non-text (multimodal-like) user inputs and avoids updates when rail output matches the original content.
    • Improves safety by avoiding unintended request/response mutations.
  • Documentation
    • Clarified the OpenAI-shaped generation response format used for rail-modified content.
  • Tests
    • Expanded unit coverage for input masking behavior, multimodal skipping, and output write-back/update detection.

@JashG
JashG requested review from a team as code owners July 21, 2026 16:01
@github-actions github-actions Bot added the fix label Jul 21, 2026
@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Input rails now propagate transformed user content into sanitized upstream requests without mutating originals. Output rails rewrite assistant content in returned responses while preserving upstream data and handling non-string message content.

Changes

Rail masking propagation

Layer / File(s) Summary
Response write-back utilities
plugins/nemo-guardrails/src/nemo_guardrails_plugin/responses.py, plugins/nemo-guardrails/tests/unit/test_responses.py
Adds input/output rail content resolution, defensive message updates, masked assistant response construction, and unit coverage for unchanged, multimodal, and non-mutating cases.
Middleware input propagation
plugins/nemo-guardrails/src/nemo_guardrails_plugin/middleware.py, plugins/nemo-guardrails/tests/unit/test_middleware.py
Sanitizes the existing request body, applies input-rail modifications to a copied message list, forwards updated messages upstream, and tests string versus non-string user content.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant process_request
  participant apply_input_rail_modifications
  participant UpstreamModel
  participant build_output_response_body
  Client->>process_request: Request with messages
  process_request->>apply_input_rail_modifications: Input-rail GenerationResponse
  apply_input_rail_modifications-->>process_request: Copied messages with modified user content
  process_request->>UpstreamModel: Sanitized request with updated messages
  UpstreamModel-->>build_output_response_body: Upstream response
  build_output_response_body-->>Client: Response with masked assistant content
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Title accurately summarizes the main change: propagating rail-mutated text through proxied requests and responses.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch jgulabrai/guardrails-pii-propagate

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor
Suite Lines Covered Line Rate Branch Rate
Unit Tests 27113/34832 77.8% 62.1%
Integration Tests 15891/33443 47.5% 19.9%

@JashG
JashG requested a review from gabwow July 21, 2026 16:59
Comment thread plugins/nemo-guardrails/src/nemo_guardrails_plugin/responses.py Outdated
@gabwow
gabwow self-requested a review July 21, 2026 21:00

@gabwow gabwow 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.

thanks, one additional optional nit.

Comment thread plugins/nemo-guardrails/tests/unit/test_middleware.py Outdated
JashG added 5 commits July 22, 2026 10:00
Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>
Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>
Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>
Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>
Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>
@JashG
JashG force-pushed the jgulabrai/guardrails-pii-propagate branch from 8a2034a to f1834aa Compare July 22, 2026 14:00
@JashG
JashG added this pull request to the merge queue Jul 22, 2026
Merged via the queue into main with commit 969393e Jul 22, 2026
59 checks passed
@JashG
JashG deleted the jgulabrai/guardrails-pii-propagate branch July 22, 2026 15:11
soluwalana pushed a commit that referenced this pull request Jul 22, 2026
…813)

* fix(guardrails): Propagate mutated text to proxied request/response

Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>

* Minor fix

Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>

* remove redundant resolver

Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>

* Remove redundant param in test helper

Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>

* fix lint

Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>

---------

Signed-off-by: Jash Gulabrai <jgulabrai@nvidia.com>
Signed-off-by: Sam Oluwalana <soluwalana@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants