Skip to content

Conversation

@rkritika1508
Copy link
Collaborator

@rkritika1508 rkritika1508 commented Jan 21, 2026

Summary

Target issue is #21.
Explain the motivation for making this change. What existing problem does the pull request solve?
This pull request implements the "rephrase" on_fail action. If any validator fails for a given message and the on_fail action is set to rephrase, we will nudge the user / LLM to rephrase the statement without the harmful content.

For example,

{
    "request_id": "3f6a9d2e-8c47-4b8a-9f3c-1d2a6e7f4c91",
    "input": "Rahul Mehta recently moved to Bengaluru, Karnataka, and can be contacted at +91-98765-43210 or via email at rahul.mehta92@example.com. His Aadhaar number is 470821987760, PAN is BQTPM7421K, voter ID is DL/05/123/456789, and passport number is K8239471. He owns a car registered as KA03MN4587 and runs a small business registered under GSTIN 29BQTPM7421K1Z5. Rahul recently paid a hospital bill using his credit card 4539 1488 0343 6467 and consulted a doctor holding medical license MH/MC/2021/778899. During an online consultation from IP address 192.168.1.42, he accessed the clinic’s website at https://www.healthcare-consult.in. For an international transfer, he shared his IBAN DE89 3704 0044 0532 0130 00 and later uploaded a notarized document with registration number NRP-IND-2024-556782.",
    "validators": [
        {
            "type": "pii_remover",
            "on_fail": "rephrase"
        }
    ]
}

Response - 
{
  "success": true,
  "data": {
    "response_id": "77cca7a8-882d-4ddb-a38f-284ebd04e0ef",
    "safe_input": "Please rephrase the query without unsafe content. PII detected in the text."
  },
  "error": null,
  "metadata": null
}

Checklist

Before submitting a pull request, please ensure that you mark these task.

  • Ran fastapi run --reload app/main.py or docker compose up in the repository root and test.
  • If you've fixed a bug or added code that is tested and has test cases.

Notes

Please add here if any other information is required for the reviewer.

Summary by CodeRabbit

Release Notes

  • New Features

    • Validators now support a "rephrase" failure action mode, enabling guardrails to respond with customized prompts requesting users to rephrase their input without unsafe content when validation checks fail, complementing existing "exception" and "fix" failure handling options.
  • Tests

    • Added integration test for the rephrase validator action feature.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 21, 2026

📝 Walkthrough

Walkthrough

This PR introduces a typed enum-based approach for handling guardrail validation failures. It replaces string-based on_fail configuration with a new GuardrailOnFail enum, adds a rephrase action handler, and refactors validator configs to explicitly pass on_fail during construction.

Changes

Cohort / File(s) Change Summary
Core Infrastructure
backend/app/core/enum.py, backend/app/core/on_fail_actions.py
Added new GuardrailOnFail enum with Exception, Fix, and Rephrase members; introduced rephrase_query_on_fail() function to format rephrase instruction messages.
Base Model Refactoring
backend/app/models/base_validator_config.py
Major refactoring: replaced string-based on_fail handling with typed GuardrailOnFail enum; removed resolve_on_fail() method; introduced _ON_FAIL_MAP for centralized mapping; updated build() signature to require keyword-only on_fail parameter returning Validator.
Validator Config Updates
backend/app/models/ban_list_safety_validator_config.py, backend/app/models/gender_assumption_bias_safety_validator_config.py, backend/app/models/lexical_slur_safety_validator_config.py, backend/app/models/pii_remover_safety_validator_config.py
Updated all build() methods to accept keyword-only on_fail parameter; changed constructor calls to use passed on_fail directly instead of internal resolution.
Controller Logic
backend/app/core/guardrail_controller.py
Refactored validator construction from list comprehension to explicit loop, explicitly resolving on_fail via v_item.resolve_on_fail() before passing to validators.
Tests
backend/app/tests/test_guardrails_api_integration.py
Added test_input_guardrails_with_validator_actions_rephrase integration test validating rephrase action behavior with 200 response and safe_input containing rephrase instruction.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 With enums bright and actions clear,
Guardrails now have rephrase cheer,
No more strings to parse with dread,
Types now guide what we've said!
~CodeRabbit's fuzzy friend 🌟

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately describes the main change: adding a new 'rephrase' on_fail action for validators across the codebase.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@rkritika1508 rkritika1508 self-assigned this Jan 21, 2026
@rkritika1508 rkritika1508 linked an issue Jan 22, 2026 that may be closed by this pull request
@rkritika1508 rkritika1508 marked this pull request as ready for review January 22, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add "rephrase" on_fail action

3 participants