MLflow Tracing SDK
MLflow Tracing SDK (mlflow-tracing) is an open-source, lightweight Python package that provides a minimum set of dependencies and functionality to instrument your code, models, or agents with MLflow Tracing. It is designed for production environments to enable faster deployment, simplified dependency management, enhanced portability, and reduced security risks compared to the full MLflow package. It supports LLM and AI agent observability, capturing inputs, outputs, and metadata for each step of a request.
Common errors
-
ModuleNotFoundError: No module named 'mlflow'
cause The `mlflow` package, which contains the full MLflow SDK, is not installed or accessible in the current Python environment. The `mlflow-tracing` package is a lightweight subset and does not include the full `mlflow` package, leading to this error if you try to import `mlflow` directly.fixIf full MLflow functionality is needed, install the complete package: `pip install mlflow`. If only tracing is intended, ensure `mlflow-tracing` is installed (`pip install mlflow-tracing`) and use imports specifically from `mlflow.tracing` where applicable. -
UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in position X: character maps to <undefined>
cause This error occurs when MLflow Tracing attempts to log data containing special characters (e.g., non-ASCII) using a system default encoding (like 'charmap' on Windows) that cannot represent those characters, typically during the process of exporting trace data to the backend.fixEnsure all strings passed to MLflow tracing APIs are UTF-8 encoded. A common workaround is to set the `PYTHONIOENCODING` environment variable to `utf-8` before running your application (e.g., `export PYTHONIOENCODING=utf-8` on Linux/macOS or `set PYTHONIOENCODING=utf-8` on Windows). -
WARNING mlflow.tracing.fluent: Failed to start span ... 'NoneType' object has no attribute 'set_span_type'.
cause This warning/error indicates a failure in MLflow's autologging or manual span creation, often due to conflicts with OpenTelemetry auto-instrumentation or unexpected `None` values being passed where a valid object or span context is expected, preventing a new span from being properly initialized or linked.fixVerify that MLflow's tracing provider is correctly initialized and not being inadvertently reset or overridden by other OpenTelemetry configurations. Ensure that data being traced (especially in autologging scenarios) is valid and not `None` for required attributes. Upgrading `mlflow-tracing` and related AI/LLM library integrations may also resolve known compatibility issues. -
name 'MlflowClient' is not defined
cause The `mlflow-tracing` package, being a minimalist SDK, intentionally does not include the `MlflowClient` class or other components of the full MLflow tracking client. This error occurs when code attempts to instantiate or use `mlflow.tracking.MlflowClient` while only `mlflow-tracing` is installed.fixIf programmatic interaction with the MLflow Tracking Server (e.g., `search_runs`, `get_run`, model registry operations) is required, install the full `mlflow` package (`pip install mlflow`). If only tracing functionality is needed, refactor your code to use the `mlflow.tracing` fluent APIs and context managers without relying on `MlflowClient`.
Warnings
- breaking Do NOT co-install `mlflow-tracing` with the full `mlflow` package.
- gotcha Calling `mlflow.set_tracking_uri()` after OpenTelemetry auto-instrumentation can reset the global `TracerProvider`.
- gotcha Using a file-based backend store for the MLflow server can lead to poor UI/SDK performance.
- gotcha Traces might get stuck in 'in progress' and not be viewable if a process hangs or runs too long.
- gotcha MLflow client and server version compatibility is important for new features.
- breaking The test script failed to run due to a missing Python module (`openai`). This prevents any evaluation of the library's features.
- breaking A `ModuleNotFoundError` for 'openai' indicates a missing dependency for functionalities that might integrate MLflow with OpenAI, such as tracing OpenAI API calls, or for running specific test scenarios.
Install
-
pip install mlflow-tracing
Imports
- mlflow
from mlflow import tracing
import mlflow
- mlflow.tracing.configure
from mlflow import tracing tracing.configure(...)
- mlflow.tracing.disable
from mlflow import tracing tracing.disable()
- mlflow.tracing.enable
from mlflow import tracing tracing.enable()
- @mlflow.trace
import mlflow @mlflow.trace def my_traced_function(): pass - mlflow.start_span
import mlflow with mlflow.start_span('my_span_name'): # ... code to trace ...
Quickstart
import os
import mlflow
from openai import OpenAI
# Set your MLflow Tracking URI (replace with your server, e.g., 'http://localhost:5000')
# For Databricks, use 'databricks' and ensure DATABRICKS_HOST/TOKEN are set.
mlflow.set_tracking_uri(os.environ.get('MLFLOW_TRACKING_URI', 'http://127.0.0.1:5000'))
# Set a new MLflow experiment to log traces to
mlflow.set_experiment("my_genai_app_traces")
# Ensure OpenAI API key is set for the example
if not os.environ.get("OPENAI_API_KEY"):
# In a real app, use a secure way to load keys (e.g., environment variable, secret manager)
# For quick testing, you can set it directly here, but it's not recommended for production
print("WARNING: OPENAI_API_KEY environment variable not set. Skipping OpenAI example.")
openai_client = None
else:
# Enable auto-tracing for OpenAI calls
mlflow.openai.autolog()
# Initialize OpenAI client
openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# Make an OpenAI call - this will be automatically traced
print("Invoking OpenAI completion...")
response = openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": "Tell me a fun fact about Python programming."}
],
max_tokens=50
)
print("OpenAI Response:", response.choices[0].message.content)
print("Trace should now be visible in MLflow UI under 'my_genai_app_traces' experiment.")