Repository navigation
Description
Impact. Guardian can't be used as a requirement any more. You used to pass GuardianCheck to requirements=[...] and a failed check fed back into the repair loop. It was deprecated, and the adapter functions that replaced it return a score rather than a Requirement. Anyone wanting Guardian checks inside m.instruct(..., requirements=[...]) has to wire that up by hand today.
The old pattern
GuardianCheck (deprecated since v0.4) implemented Requirement.validate(), so it went straight into RepairTemplateStrategy or m.validate(). With thinking=True it also put Guardian's reasoning into ValidationResult.reason, and the repair strategy fed that back as targeted feedback. Condensed from docs/examples/safety/repair_with_guardian.py, which #935 deleted:
guardian = GuardianCheck(GuardianRisk.FUNCTION_CALL, thinking=True, tools=tool_schemas)
result = m.instruct(prompt, requirements=[guardian], strategy=RepairTemplateStrategy(loop_budget=3))That example ran against Ollama with llama3.2, and nothing tested the class against a Guardian 4.1 model.
What we want
A Requirement subclass backed by the Guardian adapter functions (guardian_check(), policy_guardrails()) that drops into the same requirements= slot and works with RepairTemplateStrategy.
Why it doesn't exist yet
The adapter functions return a plain risk score, a float from 0.0 to 1.0, and no reasoning text, so there's no one-line replacement for the old pattern. #935 removed the example rather than migrating it, and docs/examples/safety/ no longer exists, so this means writing a new example rather than restoring a deleted file.
The old class is also wedded to chat-template keyword arguments. The Guardian 4.1 template only declares messages, tools, documents, available_tools and add_generation_prompt, and _filter_for_chat_template (mellea/backends/huggingface.py:3135) silently drops anything else, including guardian_config. So pointing the old class at 4.1 loses its criteria without raising anything. The intrinsic path avoids this: it builds its prompt from the adapter's io.yaml instead of template kwargs.
The workaround today, documented under Limitations in docs/docs/how-to/safety-guardrails.md, is to call guardian_check() after generation and re-run m.instruct() with an extra requirement if the score crosses your threshold.
What the new class needs
- Live in
mellea/stdlib/requirements/safety/and subclassRequirement, overridingvalidate()the wayGroundednessRequirementdoes (mellea/stdlib/requirements/rag.py:72). - Take
criteria(aCRITERIA_BANKkey or free text),scoring_schema(aSCORING_SCHEMA_BANKkey, defaulting to"assistant_response"), andthreshold, all at construction. - Call
guardian_check()(mellea/stdlib/components/intrinsic/guardian.py:183) and pass whenscore < threshold. The score measures risk, where 0.0 is no risk and 1.0 is risk detected, which matches howdocs/examples/intrinsics/guardian_core.pyreads it. - Decide whether
policy_guardrails()(:35) is in scope. It returns"Yes","No"or"Ambiguous"rather than a score, so a threshold can't drive it and the label mapping needs settling first.
ALoraRequirement (mellea/stdlib/requirements/requirement.py:65) can't be reused for this. It passes a fixed {"requirement": description} to its adapter and treats a score above 0.5 as a pass, so it can't carry criteria, a scoring schema or your own threshold.
Removing the old class
Once the replacement exists, GuardianCheck and GuardianRisk go, which is what resolves #1351.
This is a small removal. Neither name appears in any __all__, so only code importing mellea.stdlib.requirements.safety.guardian directly is affected. #1579 removed the deprecated context-relevance adapter the same kind of way, dropping the function and its stale doc references in one change. It touches:
mellea/stdlib/requirements/safety/guardian.pytest/stdlib/requirements/test_guardian_check_unit.py- entries in
docs/docs/reference/glossary.md:429,docs/CONTRIBUTING_DOCS.md:229,docs/docs/observability/metrics.md:282,docs/docs/troubleshooting/common-errors.md:251anddocs/scripts/check_docs.py:392 - a CHANGELOG line
Done when
- Unit tests in
test/stdlib/requirements/test_guardian_requirement.pycover a passing score, a failing score and a schema mismatch, with a mocked backend rather than a loaded model, astest_guardian_check_unit.pydoes today. - Nothing imports
GuardianCheckorGuardianRisk. - The new example under
docs/examples/safety/is collected by pytest from its# pytest:marker. safety-guardrails.mdpoints at the example instead of describing the workaround.
Open questions
- Repair feedback. With only a score to go on,
ValidationResult.reasoncan't carry Guardian's reasoning the way it used to. What should the reason say? Quoting the criteria and the score is the obvious option. Note thatRequirementMetricsPluginrecords"LLM judgment"as the failure reason whenvalidation_fnisNone(mellea/telemetry/metrics_plugins.py:541), which theGroundednessRequirementpattern implies, so the recorded reason needs a decision too. - Threshold default. Should
thresholdbe required? A default is convenient but hides a safety decision.
Related
- docs: migrate Guardian documentation from deprecated GuardianCheck to Intrinsics API #935: Guardian docs migration, which removed
repair_with_guardian.py. - feat: groundedness requirement #773: the groundedness requirement, the closest existing pattern to copy.
- granite guardian requirement has template kwarg differences in new model #1351: the old class's trouble with the Guardian 4.1 chat template, closed against this removal.
- fix(hf): parse <think> tags into mot.thinking on LocalHFBackend #1616: made the HF backend drop chat-template options that a template doesn't declare.
- fix(adapter-functions): remove deprecated context relevance intrinsic #1579: precedent for removing a deprecated adapter-function item along with its docs.
mellea/stdlib/requirements/safety/guardian.py: the deprecated implementation, for reference.mellea/stdlib/components/intrinsic/guardian.py: the currentguardian_check()andpolicy_guardrails().
Activity
planetf1 commentedon May 26, 2026
The
docs/examples/safety/README.mdreferenced here was removed in #935 (Guardian docs migration) — v0.4 → v0.7 felt long enough to drop the deprecated page entirely. The gap context still lives in the #935 PR thread. The issue itself stands; this is just a note that the README pointer is stale.docs(safety): fix dead link in Limitations section after README deletion
docs(safety): address psschwei review comments on PR generative-compu…
docs: migrate Guardian documentation from deprecated GuardianCheck to…
planetf1 commentedon Nov 10, 2026
Proposals for the two open questions, plus the reason/metrics question and two corrections to the body.
Threshold: required keyword, no default.
GuardianRequirement("harm", threshold=0.2). A default lets the class make a safety decision on the caller's behalf, and0.5is Guardian's detection convention rather than a risk tolerance anyone chose. One keyword at each call site is cheap. If a default is wanted anyway,0.5is the only defensible value, and the docstring has to say it means "the model card's detection convention", not "safe".policy_guardrails(): not in this PR. It returns a label, sothresholdcannot drive it, and mapping"Ambiguous"(fail closed, presumably) is a decision of its own. A follow-up for a label-based class keeps this PR's Done-when list honest. I will file that once this lands.Reason.
Guardian 'harm': risk score 0.7412 >= threshold 0.5, quoting theCRITERIA_BANKkey when one was used and the caller's text otherwise. The bank strings are paragraphs and would swamp a repair prompt. The float also goes onValidationResult.score.Metrics.
RequirementMetricsPlugin(mellea/telemetry/metrics_plugins.py:541) discardsresult.reasonwhenevervalidation_fn is None, so this class would record"LLM judgment"as its failure reason.GroundednessRequirementalready has that problem. Proposal:getattr(result, "reason", None) or "LLM judgment", which keeps the fallback and stops discarding a reason the requirement deliberately produced.Two corrections to the body:
CHANGELOG.mdhas no unreleased section. It is generated per release from release notes, and fix(adapter-functions): remove deprecated context relevance intrinsic #1579 did not touch it. I will skip the CHANGELOG line unless you want an## Unreleasedsection introduced.docs/docs/tutorials/04-making-agents-reliable.md:411and:492also mentionGuardianRisk, beyond the five references listed.refactor(safety)!: remove deprecated GuardianCheck, document Guardian…