Skip to main content

hackagent.models.guardrail

Guardrails around a model: check prompts before, and responses after.

A guardrail classifies text as safe or unsafe. :class:Guarded wraps an LLM with an optional before guardrail (checks the prompt; a blocked prompt never reaches the model) and an optional after guardrail (checks the response; a flagged response is withheld). Either way the call returns a completion whose guardrail field says what happened.

The default :class:LLMGuardrail asks a classifier model for a JSON verdict. It fails open: an unavailable or unparseable classifier lets the text through, so a misconfigured guardrail never blocks all traffic.

GuardrailSpec Objects​

class GuardrailSpec(ModelSpec)

A guardrail classifier model, with an optional custom system prompt.

GuardrailResult Objects​

@dataclass(frozen=True)
class GuardrailResult()

Outcome of one guardrail check.

Attributes:

  • is_safe - True if the text passed the check.
  • explanation - The classifier's reason.
  • categories - Harm categories flagged (empty when safe).
  • raw_response - The classifier's raw text, if any.

Guardrail Objects​

class Guardrail(Protocol)

Anything that can classify a piece of text.

ModelGuardrail Objects​

class ModelGuardrail()

A guardrail that asks a classifier model for a JSON verdict.

describe​

def describe() -> ModelSpec

The classifier model's spec.

check​

def check(text: str) -> GuardrailResult

Classify text; fails open when the classifier is unavailable.

parse_verdict​

def parse_verdict(raw: str) -> GuardrailResult

Parse {"safe": ..., "categories": [...], "reasoning": ...}.

Falls back to keyword detection when the text is not JSON.

GuardedModel Objects​

class GuardedModel(Model)

Apply the shared guardrail policy to native completions.