The `opentelemetry-util-genai` package provides boilerplate and helper functions to standardize instrumentation for Generative AI applications within the OpenTelemetry Python ecosystem. It aims to minimize the effort required to instrument GenAI libraries by offering standardized approaches for generating spans, metrics, and events. The library is currently in a beta development stage (v0.3b0) and is part of the larger `opentelemetry-python-contrib` project, with ongoing updates and a focus on evolving semantic conventions.
pip install opentelemetry-util-genaiNo compatibility data collected yet for this library.
This quickstart demonstrates how to configure the essential environment variables that control the behavior of `opentelemetry-util-genai` when used by other OpenTelemetry GenAI instrumentation libraries. It also includes a basic OpenTelemetry tracer setup, which is a prerequisite for any instrumentation. This package primarily provides internal utilities and relies on these environment variables for user-facing configuration, rather than direct API calls for typical end-user instrumentation.
Refer to the latest official documentation and release notes for breaking changes before upgrading. Regularly test applications after updates.
Set `os.environ['OTEL_SEMCONV_STABILITY_OPT_IN'] = 'gen_ai_latest_experimental'` at the start of your application.
Set `os.environ['OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT']` to `SPAN_ONLY`, `EVENT_ONLY`, or `SPAN_AND_EVENT` depending on your requirements.
Explicitly set `os.environ['OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT'] = 'true'` (or `'false'`) if you need to override the default event emission behavior.
Ensure all `opentelemetry` related packages are up-to-date. If using a library that requires experimental or unreleased features, you might need to install a specific development version or a version directly from the source repository if available, or check for compatibility notes from the dependent library. For standard use, `pip install opentelemetry-util-genai` should provide the official release. If the dependent library requires a custom extension, its documentation should specify how to install that extended version.
Before setting `gen_ai` attributes or events, ensure that complex objects like `Part` are properly converted into a JSON string (e.g., using `json.dumps()`) or their relevant primitive attributes are extracted. For example, to capture the content of a `Part` object, you might extract `part.text` or `part.inline_data` as a string. The library should ideally handle this serialization internally, so an update to `opentelemetry-instrumentation-google-genai` might also resolve it.
Convert the complex message payload into a JSON string using `json.dumps()` before setting it as a span attribute, or extract and flatten relevant primitive parts of the messages into separate attributes following OpenTelemetry's GenAI semantic conventions.
Ensure the package is correctly installed using pip: `pip install opentelemetry-util-genai`. Verify that you are running your code in the same Python environment where the package was installed. If using other OpenTelemetry components, it's often recommended to install `opentelemetry-distro` and run `opentelemetry-bootstrap -a install` for a consistent setup.