When using CrewAI, Galileo provides an event listener that handles creating a logger, starting a trace, logging spans, then concluding and flushing the trace. This behavior is inconsistent with that required for experiments, where the logger is created and trace started at the start of the experiment, and the logger is concluded and flushed at the end.
To work around this, you can tell the event listener to not start or flush the trace by detecting if there is already an active trace. If there is, then don’t start a new trace or flush it on completion.
The easiest way to do this is to get the current logger from the Galileo context, and check to see if it contains a parent trace.
- If there is no parent trace, then it is a new logger instance and you can start and flush the trace.
- If there is a parent trace, then it is an existing logger created from the experiment, and you can create the event listener setting parameters to not start or flush the trace.
from galileo import galileo_context
from galileo.handlers.crewai.handler import CrewAIEventListener
# Get the logger from the current Galileo context
logger = galileo_context.get_logger_instance()
is_in_experiment = logger.current_parent() is not None
# Create the event listener checking to see if we are in an experiment
# If we are, set start_new_trace and flush_on_crew_completed to False
# so that the existing trace is used, and not flushed automatically
CrewAIEventListener(
galileo_logger=logger,
start_new_trace=not is_in_experiment,
flush_on_crew_completed=not is_in_experiment,
)
# Run the crew
inputs = {
'topic': 'AI Agents'
}
MyCrew().crew().kickoff(inputs=inputs)
This behavior is also useful if you are logging to an existing logger, such as when you want the CrewAI agent to only be a part of a larger trace.
Troubleshooting
Here are some standard troubleshooting steps:
- If you get the following error:
Error occurred during execution: start_trace: You must conclude the existing trace before adding a new one.
, you need to ensure the start_new_trace
parameter is set to False
.
- If you get the following error:
Error occurred during execution: _conclude: No existing workflow to conclude
, you need to ensure flush_on_crew_completed
is set to False
.