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_contextgalileo_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 streamwith 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@logdef make_nested_call(): # Function implementation return "result"# This will log to the specified project and Log streamwith 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 osfrom galileo import galileo_context, openai# Initialize the Galileo wrapped OpenAI clientclient = openai.OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))# This will log to the specified project and Log streamwith 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)
You can nest galileo_context calls to temporarily override the project or Log stream:
from galileo import galileo_contextwith 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()
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 loggerlogger = 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" )
To keep your app performant, logs are not continually flushed. In some cases, you may want to flush traces explicitly:
import osfrom galileo import galileo_context, openai# Initialize the Galileo wrapped OpenAI clientclient = 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 callcall_openai()# This will upload the trace to Galileogalileo_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.
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.
To create a new session, use the start_session function.
from galileo import galileo_context# Start a new sessiongalileo_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 sessiongalileo_context.start_session(external_id=conversation_id)
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 itgalileo_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 itgalileo_context.start_session(external_id=conversation_id)