Skip to main content

Setting Trace Tags

Tags are mutable key-value pairs that you can attach to traces to add valuable metadata and context. This metadata is useful for organizing, searching, and filtering your traces. For example, you might tag your traces based on the topic of the user's input, the environment they're running in, or the model version being used.

MLflow provides the flexibility to add, update, or remove tags at any pointβ€”even after a trace is loggedβ€”through its APIs or the MLflow UI.

When to Use Trace Tags​

Trace tags are particularly useful for:

  • Session Management: Group traces by conversation sessions or user interactions
  • Environment Tracking: Distinguish between production, staging, and development traces
  • Model Versioning: Track which model version generated specific traces
  • User Context: Associate traces with specific users or customer segments
  • Performance Monitoring: Tag traces based on performance characteristics
  • A/B Testing: Differentiate between different experimental variants

Setting Tags on Active Traces​

Use mlflow.update_current_trace() to add tags during trace execution.

import mlflow


@mlflow.trace
def my_func(x):
mlflow.update_current_trace(tags={"fruit": "apple"})
return x + 1


result = my_func(5)

Example: Setting Service Tags in Production System​

import mlflow
import os


@mlflow.trace
def process_user_request(user_id: str, session_id: str, request_text: str):
# Add comprehensive tags for production monitoring
mlflow.update_current_trace(
tags={
"user_id": user_id,
"session_id": session_id,
"environment": os.getenv("ENVIRONMENT", "development"),
"model_version": os.getenv("MODEL_VERSION", "1.0.0"),
"request_type": "chat_completion",
"priority": "high" if "urgent" in request_text.lower() else "normal",
}
)

response = f"Processed: {request_text}"
return response
note

The mlflow.update_current_trace() function adds the specified tag(s) to the current trace when the key is not already present. If the key is already present, it updates the key with the new value.

Summary​

Trace tags provide a powerful way to add context and metadata to your MLflow traces, enabling:

  • Better Organization: Group related traces together
  • Powerful Filtering: Find specific traces quickly using search
  • Operational Monitoring: Track performance and issues by category
  • User Analytics: Understand user behavior patterns
  • Debugging: Add context that helps with troubleshooting

Whether you're adding tags during trace execution or after the fact, tags make your tracing data more valuable and actionable for production monitoring and analysis.