Implementation:Mlflow Mlflow Openai Autolog
| Knowledge Sources | |
|---|---|
| Domains | ML_Ops, LLM_Observability |
| Last Updated | 2026-02-13 20:00 GMT |
Overview
Concrete tool for automatically capturing OpenAI and LangChain LLM API calls as MLflow traces provided by the MLflow library.
Description
The OpenAI and LangChain autolog functions enable automatic tracing of LLM API calls by monkey-patching the respective client libraries at runtime. When mlflow.openai.autolog() is called, MLflow applies safe patches to the OpenAI client so that every call to chat.completions.create, embeddings.create, and similar methods is automatically captured as a span. The OpenAI integration also patches the OpenAI Agent SDK's AgentRunner.run() method and installs an MLflow trace processor for agent workflows.
Similarly, mlflow.langchain.autolog() patches the LangChain BaseCallbackManager.__init__ method to inject the MlflowLangchainTracer callback into every chain invocation. It also patches RunnableSequence.batch and BaseCallbackManager.merge for edge-case handling. The LangChain integration supports an additional run_tracer_inline parameter for proper context propagation in async scenarios like LangGraph.
Both integrations record spans that include input prompts, output completions, model parameters, and token usage. They require no changes to application code beyond a single enable call.
Usage
Use these autolog functions when building applications that call OpenAI or LangChain APIs and you want immediate tracing visibility without modifying each call site. Enable autologging early in your application startup, typically alongside experiment setup. Disable it when running unit tests that should not emit traces, or set silent=True to suppress warnings in production.
Code Reference
Source Location
- Repository: mlflow
- File (OpenAI):
mlflow/openai/autolog.py - Lines (OpenAI): L35-101
- File (LangChain):
mlflow/langchain/autolog.py - Lines (LangChain): L13-82
Signature
# OpenAI autolog
mlflow.openai.autolog(
disable=False,
exclusive=False,
disable_for_unsupported_versions=False,
silent=False,
log_traces=True,
) -> None
# LangChain autolog
mlflow.langchain.autolog(
disable=False,
exclusive=False,
disable_for_unsupported_versions=False,
silent=False,
log_traces=True,
run_tracer_inline=False,
) -> None
Import
import mlflow.openai
import mlflow.langchain
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| disable | bool | No | If True, disables the autologging integration. Default False. |
| exclusive | bool | No | If True, autologged content is not logged to user-created fluent runs. Default False. |
| disable_for_unsupported_versions | bool | No | If True, disables autologging for untested library versions. Default False. |
| silent | bool | No | If True, suppresses all event logs and warnings during autologging. Default False. |
| log_traces | bool | No | If True, traces are logged for LLM calls. Default True. |
| run_tracer_inline | bool | No | (LangChain only) If True, the tracer callback runs in the main async task for proper context propagation. Default False. |
Outputs
| Name | Type | Description |
|---|---|---|
| (side effect) | None | Monkey-patches are applied to OpenAI or LangChain libraries; subsequent API calls automatically produce MLflow trace spans. |
Usage Examples
Basic Usage
import mlflow
import openai
# Enable OpenAI autologging
mlflow.openai.autolog()
# All subsequent OpenAI calls are automatically traced
client = openai.OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello, world!"}],
)
# A trace with a span for this call is automatically logged
LangChain Usage
import mlflow
# Enable LangChain autologging with inline tracing for async
mlflow.langchain.autolog(log_traces=True, run_tracer_inline=True)
# All LangChain chain invocations are now traced automatically
Disabling Autologging
import mlflow
# Disable OpenAI autologging
mlflow.openai.autolog(disable=True)