Repository navigation
Add Lakera v2 post-call hook and tests (fixed PII masking) #21783
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
9 commits
Select commit
Hold shift + click to select a range
8528481
Add post-call hook for Lakera guardrail and mask PII in responses
chkp-stevegi f1665c4
Add post-call hook for Lakera and mask PII in responses
chkp-stevegi b5ed788
Merge upstream/main into feature/lakera-post-call-hook-clean
chkp-stevegi cb2a2ee
Fix post-call hook: pass event_type to call_v2_guard
chkp-stevegi d77d480
Address Greptile review: return ModelResponse, fix mutation, add head…
chkp-stevegi 275c056
Updated ponteital for index mismatch when choices have null content a…
chkp-stevegi 33257c3
Update litellm/proxy/guardrails/guardrail_hooks/lakera_ai_v2.py
eurogig 610dc2b
Update to explicitly state supported endpoints - chat completions
chkp-stevegi d78869d
Fix minor lint error on masked_entity_count
chkp-stevegi 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
66 changes: 66 additions & 0 deletions
66
tests/test_litellm/proxy/guardrails/guardrail_hooks/test_lakera_ai_v2.py
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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| """ | ||
| Tests for Lakera AI v2 guardrail hook (post-call and shared behavior). | ||
|
|
||
| PR checklist requires at least one test in tests/test_litellm/. | ||
| Additional tests live in tests/guardrails_tests/test_lakera_v2.py. | ||
| """ | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from litellm.proxy._types import UserAPIKeyAuth | ||
| from litellm.proxy.guardrails.guardrail_hooks.lakera_ai_v2 import LakeraAIGuardrail | ||
| from litellm.types.utils import ModelResponse | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_lakera_post_call_success_hook_returns_model_response_when_pii_masked(): | ||
| """ | ||
| Post-call hook must return a ModelResponse (not a dict) when PII is masked, | ||
| so the parent async_post_call_success_deployment_hook accepts it via _is_valid_response_type. | ||
| """ | ||
| lakera_guardrail = LakeraAIGuardrail(api_key="test_key") | ||
| mock_response = { | ||
| "payload": [ | ||
| {"detector_type": "pii/email", "start": 11, "end": 26, "message_id": 1} | ||
| ], | ||
| "flagged": True, | ||
| "breakdown": [ | ||
| {"detector_type": "pii/email", "detected": True, "message_id": 1}, | ||
| ], | ||
| } | ||
| llm_response = MagicMock() | ||
| llm_response.model_dump.return_value = { | ||
| "choices": [ | ||
| { | ||
| "message": { | ||
| "role": "assistant", | ||
| "content": "Your email is test@example.com", | ||
| } | ||
| }, | ||
| ] | ||
| } | ||
|
|
||
| with patch.object( | ||
| lakera_guardrail, "call_v2_guard", new_callable=AsyncMock | ||
| ) as mock_call: | ||
| mock_call.return_value = (mock_response, {}) | ||
| data = { | ||
| "messages": [{"role": "user", "content": "Hello"}], | ||
| "model": "gpt-3.5-turbo", | ||
| "metadata": {}, | ||
| } | ||
| user_api_key_dict = UserAPIKeyAuth(api_key="test_key") | ||
|
|
||
| result = await lakera_guardrail.async_post_call_success_hook( | ||
| data=data, | ||
| user_api_key_dict=user_api_key_dict, | ||
| response=llm_response, | ||
| ) | ||
|
|
||
| assert isinstance( | ||
| result, ModelResponse | ||
| ), "Must return ModelResponse so deployment hook does not discard masked response" | ||
| result_dict = result.model_dump() | ||
| assert "[MASKED" in result_dict["choices"][0]["message"]["content"] | ||
| assert "test@example.com" not in result_dict["choices"][0]["message"]["content"] |
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.