-
Notifications
You must be signed in to change notification settings - Fork 9
fix: Remove overly broad NRP and PERSON entities from default PII detection (#47) #51
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
65239e7
fix: Remove NRP and PERSON from default PII entities
srivatsan0611 5a1e171
test: Add tests for NRP/PERSON deprecation
srivatsan0611 7ad9d85
docs: Document NRP/PERSON deprecation
srivatsan0611 122b452
Update version number, typing, md comment
steven10a 4b40b68
Shorten warning message
steven10a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,8 @@ | |
| * Unit tests for the PII guardrail functionality. | ||
| */ | ||
|
|
||
| import { describe, it, expect } from 'vitest'; | ||
| import { pii, PIIConfig, PIIEntity } from '../../../checks/pii'; | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { pii, PIIConfig, PIIEntity, _clearDeprecationWarnings } from '../../../checks/pii'; | ||
|
|
||
| describe('pii guardrail', () => { | ||
| it('masks detected PII when block=false', async () => { | ||
|
|
@@ -286,4 +286,141 @@ describe('pii guardrail', () => { | |
| ); | ||
| expect(result.info?.checked_text).toBe('Ship to <LOCATION> for delivery.'); | ||
| }); | ||
|
|
||
| describe('NRP and PERSON deprecation (Issue #47)', () => { | ||
| beforeEach(() => { | ||
| // Clear deprecation warnings before each test to ensure clean state | ||
| _clearDeprecationWarnings(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // Restore all mocks to prevent leaking between tests | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('excludes NRP and PERSON from default entities', () => { | ||
| const config = PIIConfig.parse({}); | ||
|
|
||
| expect(config.entities).not.toContain(PIIEntity.NRP); | ||
| expect(config.entities).not.toContain(PIIEntity.PERSON); | ||
| }); | ||
|
|
||
| it('does not mask common two-word phrases when using defaults', async () => { | ||
| const config = PIIConfig.parse({ | ||
| block: false, | ||
| }); | ||
| const text = 'crea un nuevo cliente con email [email protected]'; | ||
|
|
||
| const result = await pii({}, text, config); | ||
|
|
||
| // Should only mask the email, not "crea un" or "nuevo cliente" | ||
| expect(result.info?.checked_text).toBe('crea un nuevo cliente con email <EMAIL_ADDRESS>'); | ||
| expect((result.info?.detected_entities as Record<string, string[]>)?.NRP).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('does not mask capitalized phrases when using defaults', async () => { | ||
| const config = PIIConfig.parse({ | ||
| block: false, | ||
| }); | ||
| const text = 'Welcome to New York, The User can access the system.'; | ||
|
|
||
| const result = await pii({}, text, config); | ||
|
|
||
| // Should not mask "New York" or "The User" | ||
| expect(result.info?.checked_text).toBe('Welcome to New York, The User can access the system.'); | ||
| expect((result.info?.detected_entities as Record<string, string[]>)?.PERSON).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('still detects NRP when explicitly configured', async () => { | ||
| const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| const config = PIIConfig.parse({ | ||
| entities: [PIIEntity.NRP], | ||
| block: false, | ||
| }); | ||
| const text = 'hello world'; | ||
|
|
||
| const result = await pii({}, text, config); | ||
|
|
||
| expect((result.info?.detected_entities as Record<string, string[]>)?.NRP).toEqual(['hello world']); | ||
| expect(result.info?.checked_text).toBe('<NRP>'); | ||
|
|
||
| consoleWarnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('still detects PERSON when explicitly configured', async () => { | ||
| const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| const config = PIIConfig.parse({ | ||
| entities: [PIIEntity.PERSON], | ||
| block: false, | ||
| }); | ||
| const text = 'John Smith lives in New York'; | ||
|
|
||
| const result = await pii({}, text, config); | ||
|
|
||
| expect((result.info?.detected_entities as Record<string, string[]>)?.PERSON).toContain('John Smith'); | ||
| expect((result.info?.detected_entities as Record<string, string[]>)?.PERSON).toContain('New York'); | ||
|
|
||
| consoleWarnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('shows deprecation warning for NRP', async () => { | ||
| const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| const config = PIIConfig.parse({ | ||
| entities: [PIIEntity.NRP], | ||
| block: false, | ||
| }); | ||
|
|
||
| await pii({}, 'test data', config); | ||
|
|
||
| expect(consoleWarnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('DEPRECATION WARNING: PIIEntity.NRP') | ||
| ); | ||
| expect(consoleWarnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('https://github.com/openai/openai-guardrails-js/issues/47') | ||
| ); | ||
|
|
||
| consoleWarnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('shows deprecation warning for PERSON', async () => { | ||
| const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| const config = PIIConfig.parse({ | ||
| entities: [PIIEntity.PERSON], | ||
| block: false, | ||
| }); | ||
|
|
||
| await pii({}, 'test data', config); | ||
|
|
||
| expect(consoleWarnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('DEPRECATION WARNING: PIIEntity.PERSON') | ||
| ); | ||
| expect(consoleWarnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('https://github.com/openai/openai-guardrails-js/issues/47') | ||
| ); | ||
|
|
||
| consoleWarnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('only shows deprecation warning once per entity', async () => { | ||
| const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| const config = PIIConfig.parse({ | ||
| entities: [PIIEntity.NRP, PIIEntity.PERSON], | ||
| block: false, | ||
| }); | ||
|
|
||
| await pii({}, 'test data', config); | ||
| await pii({}, 'more test data', config); | ||
| await pii({}, 'even more data', config); | ||
|
|
||
| // Should only be called once for each entity (2 total) | ||
| expect(consoleWarnSpy).toHaveBeenCalledTimes(2); | ||
|
|
||
| consoleWarnSpy.mockRestore(); | ||
| }); | ||
| }); | ||
| }); | ||
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
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.
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.
The deprecation warning message says "To suppress this warning, remove PIIEntity.${entity} from your entities configuration." This is misleading because removing the entity is exactly what the user should do to fix the issue, not just to suppress the warning.
Consider rephrasing to something like: "This warning will appear when ${entity} is explicitly included in your entities configuration." or simply remove this line since the warning naturally disappears when the entity is removed.
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.
@steven10a - hey Steven! Thanks for taking a look, is this significant or does the original "suppress warning" message make more sense?
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.
@srivatsan0611 Thank you for the PR, we'll be merging it later today. I would say this Copilot comment is not significant and doesn't need to be addressed. I did just push a commit to short the message as I felt it was verbose but that was also [nitpick].
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.
Yes, saw that, what you pushed made more sense lol.
Thanks for the merge @steven10a :)