detectors: add Llama Guard shield verdict parsing - #2127
Open
immu4989 wants to merge 2 commits into
Open
Conversation
Assisted-by: OpenAI Codex Signed-off-by: Imran Ahamed <immu4989@gmail.com>
|
Independent verification on Windows 11 / Python 3.11, against this branch at 321eb11: The parser handles the leading-whitespace case I mentioned when closing #2109 — Worth knowing that the same defect still stands in so a guard that pads its verdict reads as shields-down today. Nothing for this PR to do — your classes do their own parsing and are unaffected — but if #1893 lands, Two nits, neither blocking:
|
Assisted-by: OpenAI Codex Signed-off-by: Imran Ahamed <immu4989@gmail.com>
immu4989
marked this pull request as ready for review
August 29, 2026 14:04
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds
shields.LlamaGuardUpandshields.LlamaGuardDownfor targets that emit Llama Guard's published binary verdict format.LlamaGuardUptreatsunsafeas a fired shield.LlamaGuardDowntreatssafeas a quiet shield for known clean probes. Both read the first nonblank line as a standalone verdict, accept an optional category line afterunsafe, and store category codes per output inattempt.notes["llamaguard_hazard_categories"].The category codes remain unresolved because their meanings vary between Llama Guard versions. Malformed output and prose such as
safe to proceedare not accepted as verdicts. Missing outputs remain unscored.Closes #1191.
This follows @leondz's direction on #1191 that binary guard outputs belong in
shields, category results belong in notes, and the implementation should not prefer an endpoint vendor. Thanks to @Yigtwxx for sharing the leading whitespace and version dependent category findings after closing #2109.Duplicate work check
PR #2109 was closed after its author yielded #1191 to me. Before opening this PR, searches for
1191 in:bodyandLlamaGuardfound no other open implementation.Verification
.venv/bin/python -m pytest tests/detectors/test_detectors_shields.py -qResult: 15 passed.
.venv/bin/python -m pytest tests/detectors/ -qResult: 788 passed and 34 skipped.
.venv/bin/python -m pytest tests/detectors/test_detectors.py -q -k LlamaGuardResult: 8 passed.
.venv/bin/python -m pytest tests/plugins/test_plugin_load.py -q -k LlamaGuardResult: 2 passed.
.venv/bin/python -m pytest tests/test_docs.py -qResult: 682 passed.
.venv/bin/python -m black --config pyproject.toml --check garak/detectors/shields.py tests/detectors/test_detectors_shields.pyResult: both files unchanged.
.venv/bin/python -m garak --plugin_info detectors.shields.LlamaGuardUpResult: the detector loads and its metadata renders.
.venv/bin/python -m pytest tests/ -qResult: 5736 passed, 106 skipped, and 4 preexisting failures. The same four tests fail unchanged on untouched
origin/main: the Groq and LiteLLM tests passMessagewhere current generator code requiresConversation, and two AudioAchillesHeel tests lack the optionalsoundfileandlibrosadependencies.Positive cases cover safe and unsafe verdicts, leading blank lines, case variation, multiple category codes, Llama Guard 1
Ocodes, laterScodes, duplicate codes, and outputs without categories.Negative cases cover prose beginning with a verdict word, categories attached to
safe, malformed category lines, extra nonblank lines, empty text, and missing outputs.Documentation is provided through the shields module and class docstrings, which are included by the existing
automodulepage.AI assistance
AI assistance was used to develop this change. This PR remains a draft until I complete a line by line review of the two changed files and can explain the parser, score polarity, notes contract, and tests.