pip install mellea, Ollama running locally with a Granite Guardian model
pulled.
Mellea integrates IBM Granite Guardian
via GuardianCheck — a Requirement subclass that validates LLM outputs for a wide
range of safety and quality risks. GuardianCheck can be used:
- As a requirement in
instruct()oract() - Standalone via
m.validate() - As an input gate to block unsafe messages before generation
Backend note:GuardianCheckruns a separate Granite Guardian model to perform validation. It supports two backends:"ollama"(default, requires pulling a Guardian model) and"huggingface"(pip install "mellea[hf]"). The backend used for validation is independent of the session’s generation backend.
Basic safety check
Validate the last conversation turn for general harm:thinking=True enables extended reasoning mode in the Guardian model for more
accurate results. results is a list of ValidationResult objects — one per
requirement passed to validate().
Risk types
GuardianRisk covers a broad set of safety and quality dimensions:
| Risk | Description |
|---|---|
HARM | General harm detection |
JAILBREAK | Jailbreak attempt detection |
SOCIAL_BIAS | Social bias and discrimination |
PROFANITY | Profanity and offensive language |
VIOLENCE | Violent content |
SEXUAL_CONTENT | Sexual content |
UNETHICAL_BEHAVIOR | Unethical behavior |
GROUNDEDNESS | Whether a response is grounded in provided context |
ANSWER_RELEVANCE | Whether a response answers the question |
FUNCTION_CALL | Whether a tool call matches the user’s intent |
Custom criteria
For domain-specific checks, pass a natural-language criterion instead of aGuardianRisk value:
Groundedness detection
Verify that a response is grounded in a provided reference context:As a requirement in instruct()
Use GuardianCheck directly as a requirement to gate generation output:
As an input gate
Validate incoming user messages before generation. See Context and Sessions for an example of wrapping this in a session subclass that checks all inputs automatically.
Full example: docs/examples/safety/guardian.py