Skip to content

fix: preserve nullable values when applying filters - #1679

Open
longfie wants to merge 1 commit into
guardrails-ai:mainfrom
longfie:fix/preserve-null-values-in-filter
Open

longfie wants to merge 1 commit into
guardrails-ai:mainfrom
longfie:fix/preserve-null-values-in-filter

Conversation

@longfie

@longfie longfie commented Oct 17, 2026

Copy link
Copy Markdown

Problem

apply_filters removes list items and dictionary fields whenever their recursively processed value is None. That conflates the Filter sentinel with legitimate JSON null, even when no filter validator is configured.

For example, a Pydantic output schema with a required nullable note and nullable list elements accepts:

{"note": null, "values": [1, null, 2]}

Both Guard.parse and AsyncGuard.parse currently report validation_passed=True but return {"values": [1, 2]}. The result loses a required field, changes list positions, and no longer validates against the original schema.

Changes

  • Check the original item/value for a Filter instance before omitting it.
  • Preserve legitimate None values, including in nested containers.
  • Retain removal of actual Filter sentinels and the existing top-level Filter() → None behavior.
  • Add mixed None/Filter unit cases and public API regressions for Guard with both GUARDRAILS_RUN_SYNC settings and for AsyncGuard.

Validation

All tests below used local, mocked providers; no model API calls were needed.

  • Focused regression suite:
    pytest tests/unit_tests/actions/test_filter.py tests/integration_tests/test_nullable_output.py -q
    • Original implementation with the new tests: 8 failed, 11 passed.
    • This change: 19 passed.
  • Related actions, validator service, and Guard integration tests: 125 passed, 2 skipped.
  • Full pytest tests/ -q comparison:
    • Original main (06d0ff2): 865 passed, 54 skipped, 4 failed.
    • This change: 875 passed, 54 skipped, the same 4 failed.
    • The existing failures are the three TestTelemetry span-count tests and TestConfigureExtended::test_configure_with_both_parameters.
  • ruff check guardrails/ tests/ and ruff format guardrails/ tests/ --check: passed.
  • pyright guardrails/actions/filter.py: 0 errors, 0 warnings.
  • Full pyright guardrails/: the same 17 existing errors on both revisions (optional integration imports and existing type errors); normalized diagnostic output is identical.
  • git diff --check: passed.

AI assistance was used for implementation, regression tests, and verification. The results above were produced by running the reported checks, and the patch received an independent code review.

Remove only Filter sentinels from nested lists and dictionaries so valid JSON null values survive post-processing. Add sentinel regression cases and public Guard/AsyncGuard parse coverage.
@vercel

vercel Bot commented Oct 17, 2026

Copy link
Copy Markdown

@longfie is attempting to deploy a commit to the Guardrails AI Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
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.

1 participant