decorator
Galileo Decorator Module
This module provides decorators for logging and tracing function calls in your application. Decorators allow you to add logging functionality to your existing code with minimal changes.
How to use decorators:
-
Basic usage - decorate any function to log its execution:
-
You can annotate your logs to help categorize and search logs later on:
In this case, we are using:
- span_type - This groups the logs here into the “llm” category.
- name - This assigns a searchable name to all logs within this function.
-
Using context manager for grouping related operations:
Setup requirements:
- Galileo API key must be set (via environment variable GALILEO_API_KEY or programmatically)
- Project and Log Stream names should be defined if using the
log
decorator (either via environment variables GALILEO_PROJECT and GALILEO_LOG_STREAM, or viagalileo_context.init()
)
For more examples and detailed usage, see the Galileo SDK documentation.
GalileoDecorator Objects
Main decorator class that provides both decorator and context manager functionality
for logging and tracing in Galileo.
This class can be used as:
- A function decorator via the
log
method - A context manager via the
__call__
method
log
Main decorator function for logging function calls.
This decorator can be used with or without arguments:
- @log
- @log(name=“my_function”, span_type=“llm”)
Arguments:
func
: The function to decorate (when used without parentheses)name
: Optional custom name for the span (defaults to function name)span_type
: Optional span type (“llm”, “retriever”, “tool”, “workflow”)params
: Optional parameter mapping for extracting specific valuesdataset_record
: Optional parameter for dataset values. This is used by the local experiment module to set the dataset fields on the trace/spans and not generally provided for logging to log streams.
Returns:
A decorated function that logs its execution
get_logger_instance
Get the Galileo Logger instance for the current decorator context.
Arguments:
project
: Optional project name to uselog_stream
: Optional log stream name to use
Returns:
GalileoLogger instance configured with the specified project and log stream
get_current_project
Retrieve the current project name from context.
Returns:
str | None: The current project context
get_current_log_stream
Retrieve the current log stream name from context.
Returns:
str | None: The current log stream context
get_current_span_stack
Retrieve the current span stack from context.
Returns:
List[WorkflowSpan]
: The current span stack
get_current_trace
Retrieve the current trace from context.
Returns:
Trace | None: The current trace
flush
Upload all captured traces under a project and log stream context to Galileo.
If no project or log stream is provided, then the currently initialized context is used.
Arguments:
project
: The project name. Defaults to None.log_stream
: The log stream name. Defaults to None.
flush_all
Upload all captured traces under all contexts to Galileo.
This method flushes all traces regardless of project or log stream.
reset
Reset the entire context, which also deletes all traces that haven’t been flushed.
This method clears all context variables and resets the logger singleton.
reset_trace_context
Reset the trace context inside the decorator.
init
Initialize the context with a project and log stream. Optionally, it can also be used
to start a trace.
This method resets the existing active context with a new context with the specified project and log stream.
Arguments:
project
: The project name. Defaults to None.log_stream
: The log stream name. Defaults to None.experiment_id
: The experiment id. Defaults to None.local_metrics
: Local metrics configs to run on the traces/spans before submitting them for ingestion. Defaults to None.
start_session
Start a session in the active context logger instance.
Arguments:
name
: The name of the session.previous_session_id
: The id of the previous session. Defaults to None.external_id
: The external id of the session. Defaults to None.
clear_session
Clear the session in the active context logger instance.