Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Evidentlyai Evidently Guardrails Trace

From Leeroopedia
Revision as of 12:27, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Evidentlyai_Evidently_Guardrails_Trace.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 @guard decorator) and marks each guardrail as "passed" by setting span attributes.
  • on_exception -- Invoked when an exception occurs. If the exception is a GuardException or GuardsException, 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

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],
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment