Galileo Context
Manage trace context and control logging behavior with the Galileo Context Manager
The Galileo Context Manager provides a convenient way to control the logging behavior of your application. It allows you to:
- Set the project and log stream for all logs
- Get the current logger
- Flush traces
In Python, you can also:
- Set the project and log stream for a particular scope
- Manage sessions
- 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.
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.
This also works with the @log
decorator.
Third-party integrations like the OpenAI wrapper also support this.
Nesting Scopes
You can nest galileo_context
calls to temporarily override the project or log stream:
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.
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.
Flush logs
To keep your app performant, logs are not continually flushed. In some cases, you may want to flush traces explicitly:
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.
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.
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.
You can also continue a conversation using an external ID using the start_session
function.
End a Session
To stop logging to a session, you can clear the current session.
Next Steps
Basic logging components
Galileo logger
Log with full control over sessions, traces, and spans using the Galileo logger.
Log decorator
Quickly add logging to your code with the log decorator and wrapper.
Integrations with third-party SDKs
OpenAI wrapper
Automatically log calls to the OpenAI SDK with a wrapper.
OpenAI Agents trace processor
Automatically log all the steps in your OpenAI Agent SDK apps using the Galileo trace processor.
LangChain callback
Automatically log all the steps in your LangChain or LangGraph application with the Galileo callback.