Implementation:Truera Trulens Block Output Decorator
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Guardrails, Safety |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
Concrete tool for blocking application outputs that fail a quality or safety check at runtime, provided by the trulens-core library.
Description
The block_output class is a Python decorator that evaluates the output of a method after execution. If the output scores below the threshold (or above for inverse metrics), a safe default value is returned instead.
Usage
Import and apply @block_output to methods whose output needs quality or safety validation before reaching the user.
Code Reference
Source Location
- Repository: trulens
- File: src/core/trulens/core/guardrails/base.py
- Lines: L199-268
Signature
class block_output:
def __init__(
self,
feedback: core_metric.Metric,
threshold: float,
return_value: Optional[str] = None,
):
"""
Args:
feedback: Metric instance for output evaluation.
Must accept a single argument.
threshold: Minimum score to allow output.
return_value: Value to return if output is blocked. Defaults to None.
"""
Import
from trulens.core.guardrails.base import block_output
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| feedback | Metric | Yes | Feedback function returning a float (single argument) |
| threshold | float | Yes | Minimum score to allow output |
| return_value | str | No | Safe response returned when output is blocked |
Outputs
| Name | Type | Description |
|---|---|---|
| (decorator) | Callable | Wrapped function that blocks low-scoring outputs |
Usage Examples
Block Unsafe Outputs
from trulens.core.guardrails.base import block_output
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_output(
feedback=f_criminality,
threshold=0.5,
return_value="Sorry, I can't provide that information."
)
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