When using LangChain or LangGraph, Galileo provides a callback class 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 callback 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 callback setting parameters to not start or flush the trace.
# 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 callback checking to see if we are in an experiment
# If we are, set start_new_trace and flush_on_chain_end to False
# so that the existing trace is used, and not flushed automatically
galileo_callback = GalileoAsyncCallback(
logger,
start_new_trace=not is_in_experiment,
flush_on_chain_end=not is_in_experiment,
)
callbacks: Callbacks = [galileo_callback]
This behavior is also useful if you are logging to an existing logger, such as when you want the LangGraph 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
(Python), or startNewTrace
is set to false
(TypeScript).
- If you get the following error:
Error occurred during execution: _conclude: No existing workflow to conclude
, you need to ensure flush_on_chain_end
is set to False
(Python), or flushOnChainEnd
is set to false
(TypeScript).