-
Notifications
You must be signed in to change notification settings - Fork 595
fix(guardrails_ai): add valid alias to action results (#1578)
#1611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
fix(guardrails_ai): add valid alias to action results (#1578)
#1611
Conversation
NVIDIA-NeMo#1578) Fixes NVIDIA-NeMo#1578 GuardrailsAI actions previously returned only {"validation_result": ...}, while the GuardrailsAI flows in Colang expect $result["valid"]` to decide whether to block. This caused a KeyError: 'valid' at runtime. This change updates validate_guardrails_ai_input and validate_guardrails_ai_output to return a dict containing both validation_result and a valid boolean derived via guardrails_ai_validation_mapping. Colang flows can keep using $result["valid"], and existing callers of validation_result remain backwards compatible. Docs and CHANGELOG are updated to describe the new result format.
Documentation preview |
Greptile OverviewGreptile SummaryThis PR fixes a critical runtime issue where Colang flows expected Key Changes:
The implementation maintains backward compatibility by preserving the original
|
| Filename | Overview |
|---|---|
| nemoguardrails/library/guardrails_ai/actions.py | Added valid key to action return dicts to fix Colang flow compatibility |
| tests/test_guardrails_ai_actions.py | Added unit tests to verify both validation_result and valid keys are returned |
| tests/test_guardrails_ai_e2e_actions.py | Fixed validation mapping test and added e2e test for Colang flow compatibility |
Sequence Diagram
sequenceDiagram
participant Flow as Colang Flow
participant Action as validate_guardrails_ai_input/output
participant Core as validate_guardrails_ai
participant Mapping as guardrails_ai_validation_mapping
participant Guard as GuardrailsAI Guard
Flow->>Action: Call action with validator, text
Action->>Core: validate_guardrails_ai(validator, text, params)
Core->>Guard: guard.validate(text, metadata)
Guard-->>Core: ValidationResult (with validation_passed)
Core-->>Action: {"validation_result": ValidationResult}
Action->>Mapping: guardrails_ai_validation_mapping(result)
Mapping-->>Action: boolean (validation_passed)
Action-->>Flow: {"validation_result": ..., "valid": boolean}
Note over Flow: Can now access $result["valid"]<br/>without KeyError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, no comments
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Related Issues
Summary
This PR fixes a mismatch between the GuardrailsAI actions and the
GuardrailsAI Colang flows:
nemoguardrails/library/guardrails_ai/flows.coandflows.v1.co)expect
$result["valid"]when deciding whether to block.validate_guardrails_ai_input/validate_guardrails_ai_output) previously returned only{"validation_result": <ValidationOutcome>}without avalidkey.$result, whileoutput_mappingis only used byis_output_blocked(). As a result,$resultcontainedvalidation_result(withvalidation_passedinGuardrailsAI ≥ 0.3) but no
validkey, leading toKeyError: 'valid'when flows evaluated
$result["valid"].What this PR changes
GuardrailsAI actions
File:
nemoguardrails/library/guardrails_ai/actions.pyvalidate_guardrails_ai_inputandvalidate_guardrails_ai_outputnow:validate_guardrails_aicontinues to return a dict with a singlevalidation_resultkey, so passing its result intoguardrails_ai_validation_mappingis safe.This makes
$result["valid"]available in Colang flows while preservingvalidation_resultunchanged.Tests
Files:
tests/test_guardrails_ai_actions.pytests/test_guardrails_ai_e2e_actions.pyUpdates:
validate_guardrails_ai_inputandvalidate_guardrails_ai_outputreturn bothvalidation_resultandvalid, withvalidreflecting the GuardrailsAIvalidation_passedoutcome.
test_validation_mapping_e2ewith the contract ofguardrails_ai_validation_mapping(returns a boolean).validation_resultandvalidsuitable for use in Colang flows.Docs
File:
docs/user-guides/community/guardrails-ai.mdGuardrailsAI actions return a dict stored in
$resultwith:validation_result: raw GuardrailsAI outcome.valid: boolean derived fromvalidation_passed, intended forconditions like
if not $result["valid"].Changelog
File:
CHANGELOG.mdUnder
## [0.20.0]→### 🐛 Bug Fixes:Rationale
During investigation I checked the current
developbranch (actions, Colangflows, and related tests/commits) to confirm this is not intentional behavior
and that there is no existing adapter writing
validinto$result.There were two possible fixes:
validation_passeddirectly (e.g.
$result["validation_result"].validation_passed).in the GuardrailsAI action layer, so actions return
validation_resultplus a
validboolean.This PR implements Option B to maintain backward compatibility with
existing and custom flows that already use
$result["valid"], whilestill aligning with GuardrailsAI’s
validation_passedfield under thehood.
Testing
Local tests (virtualenv):
All GuardrailsAI unit tests pass. GuardrailsAI e2e tests that do not
depend on external validators/services pass; the rest are skipped when
those validators are not available.
Description
Checklist