[BUG]A guardrail that returns a malformed result leaves LLMGuardrailStartedEvent without its completed event
Description
process_guardrail emits LLMGuardrailStartedEvent, calls the guardrail, then converts its return value with
GuardrailResult.from_tuple. #8403 put the guardrail call in a try so an exception still emits
LLMGuardrailCompletedEvent, but the conversion runs after that block. A guardrail that returns the wrong shape,
for example (False, 42) (the error must be a string) or a three-item tuple, raises from from_tuple and no
completed event is emitted.
The started scope stays on the event stack. Agent.kickoff logs an event pairing mismatch for
lite_agent_execution_error, Crew.kickoff logs mismatches for task_failed and crew_kickoff_failed, and later
events in the same context get the wrong parent. The "evaluate guardrail" trace span is left open, the same symptom
#8403 fixed for a guardrail that raises.
Steps to Reproduce
- Give an agent or a task a callable guardrail that returns
(False, 42). - Run
agent.kickoff("hi")orcrew.kickoff(). - Listen for
LLMGuardrailStartedEventandLLMGuardrailCompletedEvent.
Expected behavior
The run fails with the same error as today, and the started event is closed by a LLMGuardrailCompletedEvent with
success=False, as it is when the guardrail raises.
Screenshots/Code snippets
from typing import Any
from crewai import Agent
from crewai.events.event_bus import crewai_event_bus
from crewai.events.types.llm_guardrail_events import (
LLMGuardrailCompletedEvent,
LLMGuardrailStartedEvent,
)
from crewai.llms.base_llm import BaseLLM
class FinalLLM(BaseLLM):
def __init__(self) -> None:
super().__init__(model="offline")
def supports_function_calling(self) -> bool:
return False
def call(self, messages: Any, **kwargs: Any) -> str:
return "Thought: done\nFinal Answer: ok"
async def acall(self, messages: Any, **kwargs: Any) -> str:
return self.call(messages, **kwargs)
seen = []
with crewai_event_bus.scoped_handlers():
@crewai_event_bus.on(LLMGuardrailStartedEvent)
def _started(_source: Any, _event: Any) -> None:
seen.append("started")
@crewai_event_bus.on(LLMGuardrailCompletedEvent)
def _completed(_source: Any, _event: Any) -> None:
seen.append("completed")
agent = Agent(
role="r", goal="g", backstory="b", llm=FinalLLM(), guardrail=lambda o: (False, 42)
)
try:
agent.kickoff("hi")
except Exception as e:
print(type(e).__name__)
crewai_event_bus.flush()
print(seen)
Operating System
macOS Monterey
Python Version
3.12
crewAI Version
1.15.30 (main @ bb4d892e6)
crewAI Tools Version
N/A (not used in the repro)
Virtual Environment
Venv
Evidence
[CrewAIEventsBus] Warning: Event pairing mismatch. 'lite_agent_execution_error' ...
ValidationError
['started']
A guardrail that raises instead prints ['started', 'completed'] since #8403.
Possible Solution
Call GuardrailResult.from_tuple(result) inside the existing try block, so a conversion error takes the same
terminal path as a guardrail that raises.
Additional context
Reproduced on Python 3.13 with a uv-managed venv (3.13 is not in the version dropdown). Follow-up to #8403.