Skip to content

feat: intrinsic-backed Requirement subclass for Guardian safety validation in repair loops #1071

Open

Description

@planetf1

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 subclass Requirement, overriding validate() the way GroundednessRequirement does (mellea/stdlib/requirements/rag.py:72).
  • Take criteria (a CRITERIA_BANK key or free text), scoring_schema (a SCORING_SCHEMA_BANK key, defaulting to "assistant_response"), and threshold, all at construction.
  • Call guardian_check() (mellea/stdlib/components/intrinsic/guardian.py:183) and pass when score < threshold. The score measures risk, where 0.0 is no risk and 1.0 is risk detected, which matches how docs/examples/intrinsics/guardian_core.py reads 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.py
  • test/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:251 and docs/scripts/check_docs.py:392
  • a CHANGELOG line

Done when

  • Unit tests in test/stdlib/requirements/test_guardian_requirement.py cover a passing score, a failing score and a schema mismatch, with a mocked backend rather than a loaded model, as test_guardian_check_unit.py does today.
  • Nothing imports GuardianCheck or GuardianRisk.
  • The new example under docs/examples/safety/ is collected by pytest from its # pytest: marker.
  • safety-guardrails.md points at the example instead of describing the workaround.

Open questions

  • Repair feedback. With only a score to go on, ValidationResult.reason can'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 that RequirementMetricsPlugin records "LLM judgment" as the failure reason when validation_fn is None (mellea/telemetry/metrics_plugins.py:541), which the GroundednessRequirement pattern implies, so the recorded reason needs a decision too.
  • Threshold default. Should threshold be required? A default is convenient but hides a safety decision.

Related

Activity

  1. added
    enhancement
    on May 13, 2026
  2. added
    area/adapter-functions
    area/stdlib
    area/tools
    area/requirement
    on May 20, 2026
  3. planetf1 commented on May 26, 2026

    @planetf1
    ContributorAuthor

    The docs/examples/safety/README.md referenced 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.

  4. added
    p2
    on Jun 18, 2026
  5. planetf1 commented on Nov 10, 2026

    @planetf1
    ContributorAuthor

    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, and 0.5 is 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.5 is 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, so threshold cannot 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 the CRITERIA_BANK key 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 on ValidationResult.score.

    Metrics. RequirementMetricsPlugin (mellea/telemetry/metrics_plugins.py:541) discards result.reason whenever validation_fn is None, so this class would record "LLM judgment" as its failure reason. GroundednessRequirement already 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.md has 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 ## Unreleased section introduced.
    • docs/docs/tutorials/04-making-agents-reliable.md:411 and :492 also mention GuardianRisk, beyond the five references listed.
  6. self-assigned this
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/adapter-functionsarea/requirementarea/stdlibarea/toolsenhancementp2

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions