-
Notifications
You must be signed in to change notification settings - Fork 20
Add unicode handling to keyword match #52
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 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fca08ae
Add unicode handling to keyword match
steven10a e5dd858
Handle non-word letter endings or startings
steven10a 69095c1
updated tests
steven10a b18308b
Ruff formatting
steven10a e3d3186
Merge branch 'main' of github.com:openai/openai-guardrails-python int…
steven10a e5ca5a9
Address copilot nits
steven10a 811e8d6
Copilot nits
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 |
|---|---|---|
|
|
@@ -65,3 +65,132 @@ async def test_keywords_does_not_trigger_on_benign_text() -> None: | |
| result = await keywords(ctx=None, data="Safe content", config=config) | ||
|
|
||
| assert result.tripwire_triggered is False # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_does_not_match_partial_words() -> None: | ||
| """Ensure substrings embedded in larger words are ignored.""" | ||
| config = KeywordCfg(keywords=["orld"]) | ||
| result = match_keywords("Hello, world!", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is False # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_handles_numeric_tokens() -> None: | ||
| """Keywords containing digits should match exact tokens.""" | ||
| config = KeywordCfg(keywords=["world123"]) | ||
| result = match_keywords("Hello, world123", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is True # noqa: S101 | ||
| assert result.info["matched"] == ["world123"] # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_rejects_partial_numeric_tokens() -> None: | ||
| """Numeric keywords should not match when extra digits follow.""" | ||
| config = KeywordCfg(keywords=["world123"]) | ||
| result = match_keywords("Hello, world12345", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is False # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_handles_underscored_tokens() -> None: | ||
| """Underscored keywords should be detected exactly once.""" | ||
| config = KeywordCfg(keywords=["w_o_r_l_d"]) | ||
| result = match_keywords("Hello, w_o_r_l_d", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is True # noqa: S101 | ||
| assert result.info["matched"] == ["w_o_r_l_d"] # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_rejects_words_embedded_in_underscores() -> None: | ||
| """Words surrounded by underscores should not trigger partial matches.""" | ||
| config = KeywordCfg(keywords=["world"]) | ||
| result = match_keywords("Hello, test_world_test", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is False # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_handles_chinese_characters() -> None: | ||
| """Unicode keywords such as Chinese characters should match.""" | ||
| config = KeywordCfg(keywords=["你好"]) | ||
| result = match_keywords("你好", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is True # noqa: S101 | ||
| assert result.info["matched"] == ["你好"] # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_handles_chinese_tokens_with_digits() -> None: | ||
| """Unicode keywords that include digits should match whole tokens.""" | ||
| config = KeywordCfg(keywords=["你好123"]) | ||
| result = match_keywords("你好123", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is True # noqa: S101 | ||
| assert result.info["matched"] == ["你好123"] # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_rejects_partial_chinese_tokens_with_digits() -> None: | ||
| """Unicode keywords with trailing digits should not match supersets.""" | ||
| config = KeywordCfg(keywords=["你好123"]) | ||
| result = match_keywords("你好12345", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is False # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_applies_boundaries_to_all_keywords() -> None: | ||
| """Every keyword in a multi-token pattern should respect Unicode boundaries.""" | ||
| config = KeywordCfg(keywords=["test", "hello", "world"]) | ||
| result = match_keywords("testing hello world", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is True # noqa: S101 | ||
| assert result.info["matched"] == ["hello", "world"] # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_detects_email_like_patterns() -> None: | ||
| """Email-like keywords starting with punctuation should match after word chars.""" | ||
| config = KeywordCfg(keywords=["@corp.com"]) | ||
| result = match_keywords("[email protected]", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is True # noqa: S101 | ||
| assert result.info["matched"] == ["@corp.com"] # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_detects_hashtag_patterns() -> None: | ||
| """Hashtag keywords starting with punctuation should match after word chars.""" | ||
| config = KeywordCfg(keywords=["#leak"]) | ||
| result = match_keywords("abc#leak", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is True # noqa: S101 | ||
| assert result.info["matched"] == ["#leak"] # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_respects_end_boundary_for_punctuation_prefixed() -> None: | ||
| """Punctuation-prefixed keywords ending with word chars need end boundary.""" | ||
| config = KeywordCfg(keywords=["@leak"]) | ||
| # Should not match when word chars continue after | ||
| result = match_keywords("foo@leakmore", config, guardrail_name="Test Guardrail") | ||
| assert result.tripwire_triggered is False # noqa: S101 | ||
|
|
||
| # Should match when followed by non-word char | ||
| result = match_keywords("foo@leak bar", config, guardrail_name="Test Guardrail") | ||
| assert result.tripwire_triggered is True # noqa: S101 | ||
| assert result.info["matched"] == ["@leak"] # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_handles_full_punctuation_keywords() -> None: | ||
| """Keywords consisting only of punctuation should match anywhere.""" | ||
| config = KeywordCfg(keywords=["@#$"]) | ||
| result = match_keywords("test@#$test", config, guardrail_name="Test Guardrail") | ||
|
|
||
| assert result.tripwire_triggered is True # noqa: S101 | ||
| assert result.info["matched"] == ["@#$"] # noqa: S101 | ||
|
|
||
|
|
||
| def test_match_keywords_mixed_punctuation_and_word_chars() -> None: | ||
| """Keywords with both punctuation prefix and suffix should work correctly.""" | ||
| config = KeywordCfg(keywords=["@user@"]) | ||
| # Should match when embedded | ||
| result = match_keywords("test@user@test", config, guardrail_name="Test Guardrail") | ||
| assert result.tripwire_triggered is True # noqa: S101 | ||
|
|
||
| # Should match even when followed by more text (no boundaries applied to punctuation edges) | ||
| result = match_keywords("test@user@more", config, guardrail_name="Test Guardrail") | ||
| assert result.tripwire_triggered is True # noqa: S101 | ||
steven10a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.