crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

A guardrail that raises leaves LLMGuardrailStartedEvent without its completed event, so the trace span closes as orphaned

Open Wilson0511 opened this issue 1 month ago • 0 comments

Description

process_guardrail (lib/crewai/src/crewai/utilities/guardrail.py) emits LLMGuardrailStartedEvent before calling the guardrail, and LLMGuardrailCompletedEvent after it returns. A HookAborted deny gets its terminal event before re-raising. An ordinary exception from the guardrail callable does not: it propagates immediately and the started event is left unpaired.

The tracing listener opens an evaluate guardrail span on the started event and closes it on the completed event (telemetry/tracing/handlers.py, handle_llm_guardrail_started / handle_llm_guardrail_completed). With the completed event missing, the span survives until TelemetrySession.finish_spans() force-closes it at end of execution with the status Span orphaned — execution ended before completion event. The real error (ValueError: ... from the guardrail) never reaches the trace; the orphan sweep label is all that remains. In a retry loop that mixes a raising guardrail with returning ones, every raise stacks another orphaned span.

Reproduction

from crewai.events.event_bus import crewai_event_bus
from crewai.events.types.llm_guardrail_events import (
    LLMGuardrailCompletedEvent,
    LLMGuardrailStartedEvent,
)
from crewai.tasks.task_output import TaskOutput
from crewai.utilities.guardrail import process_guardrail

started, completed = [], []

def raising_guardrail(_output):
    raise ValueError("guardrail exploded")

with crewai_event_bus.scoped_handlers():
    @crewai_event_bus.on(LLMGuardrailStartedEvent)
    def _s(_src, event): started.append(event)

    @crewai_event_bus.on(LLMGuardrailCompletedEvent)
    def _c(_src, event): completed.append(event)

    try:
        process_guardrail(
            output=TaskOutput(description="d", raw="r", agent="a"),
            guardrail=raising_guardrail,
            retry_count=0,
        )
    except ValueError:
        pass

assert len(started) == 1
assert len(completed) == 0  # <- the terminal event is missing

The same shape is already handled for HookAborted in process_guardrail (added with the model-call hook deny propagation), and tests/hooks/test_hook_abort_propagation.py::test_a_denied_guardrail_still_reports_the_validation_it_started asserts the deny keeps the pairing. An exception deserves the same terminal event: it ends the validation just as decisively, and the trace should carry the real error instead of an orphan label.

Suggested fix

Mirror the existing HookAborted branch: catch Exception after it, emit LLMGuardrailCompletedEvent(success=False, error=f"{type(e).__name__}: {e}, retry_count=..., guardrail_type/name from the started event), then re-raise so caller behavior is unchanged. I can open a PR with the regression test above.

Wilson0511 avatar Oct 22 '26 11:10 Wilson0511