Implementation:Truera Trulens Block Input Decorator
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Guardrails, Safety |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
Concrete tool for blocking user inputs that fail a safety or quality check at runtime, provided by the trulens-core library.
Description
The block_input class is a Python decorator that evaluates user input before the decorated method executes. If the input scores below the threshold (or above for inverse metrics), the method returns a safe default value without processing the input.
Usage
Import and apply @block_input to methods that receive user input. Pass a Feedback/Metric instance, threshold, and optional safe return value.
Code Reference
Source Location
- Repository: trulens
- File: src/core/trulens/core/guardrails/base.py
- Lines: L108-196
Signature
class block_input:
def __init__(
self,
feedback: core_metric.Metric,
threshold: float,
keyword_for_prompt: Optional[str] = None,
return_value: Optional[str] = None,
):
"""
Args:
feedback: Metric instance for input evaluation.
threshold: Minimum score to allow processing.
keyword_for_prompt: Kwarg name for the input. Auto-detected if not set.
return_value: Value to return if input is blocked. Defaults to None.
"""
Import
from trulens.core.guardrails.base import block_input
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| feedback | Metric | Yes | Feedback function returning a float score |
| threshold | float | Yes | Minimum score to allow input processing |
| keyword_for_prompt | str | No | Kwarg name for user input (auto-detected) |
| return_value | str | No | Safe response returned when input is blocked |
Outputs
| Name | Type | Description |
|---|---|---|
| (decorator) | Callable | Wrapped function that blocks low-scoring inputs |
Usage Examples
Block Criminal/Unsafe Inputs
from trulens.core.guardrails.base import block_input
from trulens.core import Feedback
from trulens.providers.openai import OpenAI
provider = OpenAI()
f_criminality = Feedback(provider.criminality, higher_is_better=False)
class SafeApp:
@block_input(
feedback=f_criminality,
threshold=0.9,
keyword_for_prompt="question",
return_value="I'm unable to respond to that request."
)
def respond(self, question: str) -> str:
return self.llm.generate(question)
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment