{"library":"mlflow-tracing","code":"import os\nimport mlflow\nfrom openai import OpenAI\n\n# Set your MLflow Tracking URI (replace with your server, e.g., 'http://localhost:5000')\n# For Databricks, use 'databricks' and ensure DATABRICKS_HOST/TOKEN are set.\nmlflow.set_tracking_uri(os.environ.get('MLFLOW_TRACKING_URI', 'http://127.0.0.1:5000'))\n\n# Set a new MLflow experiment to log traces to\nmlflow.set_experiment(\"my_genai_app_traces\")\n\n# Ensure OpenAI API key is set for the example\nif not os.environ.get(\"OPENAI_API_KEY\"): \n    # In a real app, use a secure way to load keys (e.g., environment variable, secret manager)\n    # For quick testing, you can set it directly here, but it's not recommended for production\n    print(\"WARNING: OPENAI_API_KEY environment variable not set. Skipping OpenAI example.\")\n    openai_client = None\nelse:\n    # Enable auto-tracing for OpenAI calls\n    mlflow.openai.autolog()\n    \n    # Initialize OpenAI client\n    openai_client = OpenAI(api_key=os.environ.get(\"OPENAI_API_KEY\"))\n\n    # Make an OpenAI call - this will be automatically traced\n    print(\"Invoking OpenAI completion...\")\n    response = openai_client.chat.completions.create(\n        model=\"gpt-3.5-turbo\",\n        messages=[\n            {\"role\": \"system\", \"content\": \"You are a helpful AI assistant.\"},\n            {\"role\": \"user\", \"content\": \"Tell me a fun fact about Python programming.\"}\n        ],\n        max_tokens=50\n    )\n    print(\"OpenAI Response:\", response.choices[0].message.content)\n    print(\"Trace should now be visible in MLflow UI under 'my_genai_app_traces' experiment.\")","lang":"python","description":"This quickstart demonstrates how to set up MLflow Tracing for an OpenAI call. It configures the MLflow tracking URI, sets an experiment, enables autologging for OpenAI, and then performs a simple API call. The trace for this call will be automatically logged and viewable in the MLflow UI. Make sure an MLflow server is running and `OPENAI_API_KEY` is set in your environment.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}