> ## Documentation Index
> Fetch the complete documentation index at: https://docs.galileo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# decorator

## Module

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:
   ```python theme={null}
   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:
   ```python theme={null}
   @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:
   ```python theme={null}
   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

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

### clear\_session

```python theme={null}
def clear_session(self) -> None
```

Clear the session in the active context logger instance.

### flush

```python theme={null}
def flush(self,
          project: Optional[str]=None,
          log_stream: Optional[str]=None,
          experiment_id: Optional[str]=None,
          mode: 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.
* `experiment_id`: The experiment ID. Defaults to None.
* `mode`: The logger mode. Defaults to None.

### flush\_all

```python theme={null}
def flush_all(self) -> None
```

Upload all captured traces under all contexts to Galileo.

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

### get\_current\_log\_stream

```python theme={null}
def get_current_log_stream(self) -> Optional[str]
```

Retrieve the current log stream name from context.

**Returns**

* `str | None`: The current log stream context

### get\_current\_mode

```python theme={null}
def get_current_mode(self) -> Optional[LoggerModeType]
```

Retrieve the current mode from context.

**Returns**

* `Optional[LoggerModeType]`: The current mode context, or None if not initialized

### get\_current\_project

```python theme={null}
def get_current_project(self) -> Optional[str]
```

Retrieve the current project name from context.

**Returns**

* `str | None`: The current project context

### get\_current\_span\_stack

```python theme={null}
def get_current_span_stack(self) -> list[WorkflowSpan]
```

Retrieve the current span stack from context.

**Returns**

* `List[WorkflowSpan]`: The current span stack

### get\_current\_trace

```python theme={null}
def get_current_trace(self) -> Optional[Trace]
```

Retrieve the current trace from context.

**Returns**

* `Trace | None`: The current trace

### get\_logger\_instance

```python theme={null}
def get_logger_instance(self,
                        project: Optional[str]=None,
                        log_stream: Optional[str]=None,
                        experiment_id: Optional[str]=None,
                        mode: Optional[str]=None,
                        ingestion_hook: Optional[Callable]=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
* `experiment_id`: Optional experiment ID to use
* `mode`: Optional logger mode to use

**Returns**

* `GalileoLogger instance configured with the specified project and log stream`:

### init

```python theme={null}
def init(self,
         project: Optional[str]=None,
         log_stream: Optional[str]=None,
         experiment_id: Optional[str]=None,
         local_metrics: Optional[list[LocalMetricConfig]]=None,
         mode: Optional[str]=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.
* `mode`: The logger mode.

### log

```python theme={null}
def log(self,
        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", "agent")
* `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`:

### reset

```python theme={null}
def reset(self) -> 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

```python theme={null}
def reset_trace_context(self) -> None
```

Reset the trace context inside the decorator.

### set\_session

```python theme={null}
def set_session(self, session_id: str) -> None
```

Set the session in the active context logger instance. This is useful when you want to continue logging to an existing session.

**Arguments**

* `session_id`: The id of the session to set.

### start\_session

```python theme={null}
def start_session(self,
                  name: Optional[str]=None,
                  previous_session_id: Optional[str]=None,
                  external_id: Optional[str]=None,
                  metadata: Optional[dict[str, str]]=None) -> str
```

Start a session in the active context logger instance.

**Arguments**

* `name`: The name of the session. If not provided, a session name will be generated automatically.
* `previous_session_id`: The id of the previous session. Defaults to None.
* `external_id`: The external id of the session. Defaults to None.
* `metadata`: User metadata for the session. Defaults to None.

**Returns**

* `str`: The id of the newly created session.
