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:

  1. Basic usage - decorate any function to log its execution:

    from galileo import log
    
    @log
    def my_function(arg1, arg2):
        return arg1 + arg2
    
  2. You can annotate your logs to help categorize and search logs later on:

    @log(span_type="llm", name="GPT-4 Call")
    def call_llm(prompt, temperature=0.7):
        # LLM call implementation
        return response
    

    In this case, we are using:

    1. span_type - This groups the logs here into the “llm” category.
    2. name - This assigns a searchable name to all logs within this function.
  3. Using context manager for grouping related operations:

    from galileo import galileo_context
    
    with galileo_context(project="my-project", log_stream="production"):
        result1 = my_function()
        result2 = another_function()
    

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 via galileo_context.init())

For more examples and detailed usage, see the Galileo SDK documentation.

GalileoDecorator Objects

class GalileoDecorator()

Main decorator class that provides both decorator and context manager functionality

for logging and tracing in Galileo.

This class can be used as:

  1. A function decorator via the log method
  2. A context manager via the __call__ method

log

def log(
    func: Optional[Callable[P, R]] = None,
    *,
    name: Optional[str] = None,
    span_type: Optional[SPAN_TYPE] = None,
    params: Optional[dict[str, Union[str, Callable]]] = None,
    dataset_record: Optional[DatasetRecord] = None
) -> Callable[[Callable[P, R]], Callable[P, R]]

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 values
  • dataset_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

def get_logger_instance(project: Optional[str] = None,
                        log_stream: Optional[str] = None,
                        experiment_id: Optional[str] = None) -> GalileoLogger

Get the Galileo Logger instance for the current decorator context.

Arguments:

  • project: Optional project name to use
  • log_stream: Optional log stream name to use

Returns:

GalileoLogger instance configured with the specified project and log stream

get_current_project

def get_current_project() -> Optional[str]

Retrieve the current project name from context.

Returns:

str | None: The current project context

get_current_log_stream

def get_current_log_stream() -> Optional[str]

Retrieve the current log stream name from context.

Returns:

str | None: The current log stream context

get_current_span_stack

def get_current_span_stack() -> list[WorkflowSpan]

Retrieve the current span stack from context.

Returns:

List[WorkflowSpan]: The current span stack

get_current_trace

def get_current_trace() -> Optional[Trace]

Retrieve the current trace from context.

Returns:

Trace | None: The current trace

flush

def flush(project: Optional[str] = None,
          log_stream: Optional[str] = None,
          experiment_id: Optional[str] = None) -> None

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

def flush_all() -> None

Upload all captured traces under all contexts to Galileo.

This method flushes all traces regardless of project or log stream.

reset

def reset() -> None

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

def reset_trace_context() -> None

Reset the trace context inside the decorator.

init

def init(project: Optional[str] = None,
         log_stream: Optional[str] = None,
         experiment_id: Optional[str] = None,
         local_metrics: Optional[list[LocalMetricConfig]] = None) -> None

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

def start_session(name: str,
                  previous_session_id: Optional[str] = None,
                  external_id: Optional[str] = None) -> None

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

def clear_session() -> None

Clear the session in the active context logger instance.