The Galileo Context Manager provides a convenient way to control the logging behavior of your application. It allows you to:

  1. Set the project and log stream for all logs
  2. Get the current logger
  3. Flush traces

In Python, you can also:

  1. Set the project and log stream for a particular scope
  2. Manage sessions
  3. Get the current span and trace

In the Python SDK, this is available through the galileo_context object, in TypeScript this is available as a set of distinct top-level functions.

Set the Project and Log Stream

By default, Galileo uses the GALILEO_PROJECT and GALILEO_LOG_STREAM environment variables to determine which project and log stream to log to. You can override this by initializing the Galileo context with a project and log stream name.

from galileo import galileo_context

galileo_context.init(project="my-project",log_stream="my-log-stream")

Set the Project and Log Stream for a Single Scope in Python

You can set the project and log stream for a scope using the Python galileo_context, object in a with statement. Every log inside this block, including nested calls, will use the specified project and log stream.

from galileo import galileo_context

# This will log to the specified project and log stream
with galileo_context(project="my-project", log_stream="my-log-stream"):
    # All operations within this block will be logged to the same trace
    # in the specified project and log stream
    result = your_function()
    print(result)

This also works with the @log decorator.

from galileo import log, galileo_context

@log
def make_nested_call():
    # Function implementation
    return "result"

# This will log to the specified project and log stream
with galileo_context(project="my-project", log_stream="my-log-stream"):
    content = make_nested_call()
    print(content)

Third-party integrations like the OpenAI wrapper also support this.

import os
from galileo import galileo_context, openai

# Initialize the Galileo wrapped OpenAI client
client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

# This will log to the specified project and log stream
with galileo_context(project="my-project", log_stream="my-log-stream"):
    chat_completion = client.chat.completions.create(
        messages=[{"role": "user", "content": "Say this is a test"}],
        model="gpt-4o"
    )
    print(chat_completion.choices[0].message.content)

Nesting Scopes

You can nest galileo_context calls to temporarily override the project or log stream:

from galileo import galileo_context

with galileo_context(project="my-project", log_stream="my-log-stream"):
    # This will log to my-project/my-log-stream
    operation_1()

    with galileo_context(log_stream="sub-stream"):
        # This will log to my-project/sub-stream
        operation_2()

    # Back to logging to my-project/my-log-stream
    operation_3()

Get the Current Logger

The Galileo context management keeps track of loggers. You can get the current logger, which will create a new one if there isn’t an existing logger.

from galileo import galileo_context

# Get the current logger
logger = galileo_context.get_logger_instance()

If you are using any of the decorators, wrappers, or third-party integrations then this allows you to get the logger created by those components. For example, if you are adding a call inside a method decorated by the Python @log decorator, wrapped in the TypeScript log wrapper, or created automatically by an experiment, then this will return that logger instance so you can manually add additional spans.

from galileo import log, galileo_context

@log(span_type="llm")
def logged_function(input_text):
    # Get the current logger
    logger = galileo_context.get_logger_instance()

    # Add a span
    workflow_span = logger.add_workflow_span(
        input="My workflow"
    )

Flush logs

To keep your app performant, logs are not continually flushed. In some cases, you may want to flush traces explicitly:

import os
from galileo import galileo_context, openai

# Initialize the Galileo wrapped OpenAI client
client = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

def call_openai():
    chat_completion = client.chat.completions.create(
        messages=[{"role": "user", "content": "Say this is a test"}],
        model="gpt-4o"
    )
    return chat_completion.choices[0].message.content

# This will create a single span trace with the OpenAI call
call_openai()

# This will upload the trace to Galileo
galileo_context.flush()

This approach is particularly useful for long-running applications where you need to control when traces are flushed to Galileo.

When using a Python Notebook (such as Jupyter, Google Colab, etc.), you should use the galileo_context and make sure to call galileo_context.flush() at the end of your notebook.

Manage Sessions in Python

With the Python SDK, you can manage sessions from the context level in addition to the logger level. All the session management functions are applied to the current logger if called from the context. If you want to apply these to a specific logger instance, call the same methods directly on that logger instance.

In the TypeScript SDK, these functions are only available on a GalileoLogger instance.

Create a New Session

To create a new session, use the start_session function.

from galileo import galileo_context

# Start a new session
galileo_context.start_session(name="My session")

When you start a session you can optionally give it a name. If you don’t provide a name, one is created for you based off the traces in the session.

You can also provide an external ID to link a session in Galileo to an external identifier.

from galileo import galileo_context

# Start a new session
galileo_context.start_session(external_id=conversation_id)

Continue an Existing Session

If you want to add a trace to an existing session, you can use the set_session function, passing the session ID. This is useful if you want to persist a session, for example saving a chatbot conversation with a user mid-conversation, then resuming the next time a user connects.

from galileo import galileo_context

# Start an existing session to add new traces to it
galileo_context.set_session(session_id=conversation_session_id)

You can also continue a conversation using an external ID using the start_session function.

from galileo import galileo_context

# Start an existing session by its external ID to add new traces to it
galileo_context.start_session(external_id=conversation_id)

End a Session

To stop logging to a session, you can clear the current session.

from galileo import galileo_context

# Clear the current session
galileo_context.clear_session()

Next Steps

Basic logging components

Integrations with third-party SDKs