Implementation:Evidentlyai Evidently Guardrails Trace
| Knowledge Sources | |
|---|---|
| Domains | Guardrails, Tracing, Observability |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
guardrails/trace.py implements a tracely interceptor that records guardrail validation results (pass/fail) as span attributes for distributed tracing and observability.
Description
The GuardrailsInterceptor class extends tracely.Interceptor and hooks into three lifecycle events of a traced span:
before_call-- A no-op; no pre-call processing is needed.after_call-- Invoked after successful function execution. Reads the"evidently.guardrails"context value from the span (set by the@guarddecorator) and marks each guardrail as"passed"by setting span attributes.on_exception-- Invoked when an exception occurs. If the exception is aGuardExceptionorGuardsException, it identifies which guards failed and which passed, setting appropriate span attributes for each guard with its name, status ("passed"or"failed"), and any error message.
Span attributes follow the naming convention evidently.guardrail.{guard_id}.name, evidently.guardrail.{guard_id}.status, and evidently.guardrail.{guard_id}.error.
This module requires the tracely package. If tracely is not installed, importing this module raises an ImportError with a warning.
Usage
Use GuardrailsInterceptor when you want guardrail validation results to appear in your distributed tracing system. Register it as a tracely interceptor to automatically capture guardrail pass/fail outcomes on spans created by the @guard decorator.
Code Reference
Source Location
- Repository: Evidentlyai_Evidently
- File:
src/evidently/guardrails/trace.py
Signature
class GuardrailsInterceptor(tracely.Interceptor):
def before_call(self, span: SpanObject, context: InterceptorContext, *args, **kwargs): ...
def after_call(self, span: SpanObject, context: InterceptorContext, return_value): ...
def on_exception(self, span: SpanObject, context: InterceptorContext, ex: Exception) -> bool: ...
def _set_span_for_guard(
self, span: SpanObject, guard_id: str, guard_name: str, status: str, error: Optional[str]
): ...
Import
from evidently.guardrails.trace import GuardrailsInterceptor
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| span | SpanObject |
Yes | The tracely span object on which guardrail attributes are set. |
| context | InterceptorContext |
Yes | The interceptor context from tracely providing call metadata. |
| ex | Exception |
Yes (on_exception) | The exception raised during the traced call; checked for GuardException type.
|
Outputs
| Name | Type | Description |
|---|---|---|
| on_exception return | bool |
Returns True if the exception was a guardrail exception and was handled; False otherwise.
|
Usage Examples
from evidently.guardrails.trace import GuardrailsInterceptor
# Register the interceptor with tracely
import tracely
interceptor = GuardrailsInterceptor()
tracely.init_tracing(
address="http://collector:4317",
interceptors=[interceptor],
)