Skip to main content

Fixed: "Trace with ID … cannot be loaded because its span data is corrupted" When You Call mlflow.get_trace

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

I traced a Claude call with MLflow and, in the next line, read the trace back to print its token usage, exactly as MLflow's documentation does. MLflow answered that the trace "cannot be loaded because its span data is corrupted", and the script crashed. The trace was fine; I had asked for it too early. Here's the whole story, with real output from MLflow 3.18.0.

What I Was Doing​

I was writing an example that reads a trace's token usage and cost in Python. It follows the Python example of MLflow's Token Usage and Cost page: get the ID of the last trace, load it with mlflow.get_trace(), and read trace.info:

token_cost.py (first version)
import anthropic
import mlflow

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("token-cost")
mlflow.anthropic.autolog()

client = anthropic.Anthropic()
client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Suggest a name for a camping stove, in three words or fewer."}],
)

trace = mlflow.get_trace(mlflow.get_last_active_trace_id())
print("Token usage:", trace.info.token_usage)
print("Cost (USD):", trace.info.cost)

The tracking server ran on http://127.0.0.1:8080.

What I Expected​

Two lines: the call's token usage and its cost.

What I Got​

Terminal
uv run --env-file .env token_cost.py
Output
2026/11/30 17:33:35 INFO mlflow.tracking.fluent: Experiment with name 'token-cost' does not exist. Creating a new experiment.
2026/11/30 17:33:37 WARNING mlflow.tracing.fluent: Failed to get trace from the tracking store: Trace with ID tr-5b3352a8223c306b98996c6bc81dbbde cannot be loaded because its span data is corrupted. Please try creating or loading another trace.. If using async trace logging, pass flush=True to wait for pending writes. For full traceback, set logging level to debug.
Traceback (most recent call last):
File "/Users/you/source/token-cost/token_cost.py", line 16, in <module>
print("Token usage:", trace.info.token_usage)
^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'info'
2026/11/30 17:33:37 INFO mlflow.tracing.export.async_export_queue: Flushing the async trace logging queue before program exit. This may take a while...

A terminal where token_cost.py logs that the trace cannot be loaded because its span data is corrupted, then fails with AttributeError: 'NoneType' object has no attribute 'info', and MLflow flushes its trace logging queue on exit.

get_trace() logged a warning and returned None, so trace.info failed. The last line shows MLflow still sending the trace as the program exited.

What I Checked​

1. The end of the warning. It says: "If using async trace logging, pass flush=True to wait for pending writes." MLflow sends traces in the background, which is what the final "Flushing the async trace logging queue" line is about.

2. The documentation of get_trace. The API reference has a flush argument: "If True and the trace is not found, flush any pending async trace writes and retry. Useful in tests or scripts where async logging may not have completed. Default to False." The Token Usage and Cost page's examples don't pass it.

3. A smaller test, without Claude. A function decorated with @mlflow.trace, called once, then get_trace() on the next line, failed the same way in 5 runs out of 5. With flush=True it returned the trace in 3 runs out of 3. Two of the "corrupted" traces, loaded again later from another script, came back complete, with state OK and their one span's output. Nothing was corrupted.

4. Where the wording comes from. In MLflow's code, get_trace finds the trace's summary but not yet its spans, then tries another storage location, finds nothing usable there and reports the span data as corrupted. So the message describes a trace that simply hasn't finished arriving.

I reported the failing documentation example and the misleading message as mlflow/mlflow#28298.

Why It Happens​

MLflow logs traces asynchronously: the call returns, and the trace is sent to the tracking server in the background a moment later. get_trace() on the very next line can arrive before the trace's spans have been saved. Without flush=True, it gives up at once and returns None.

The Fix​

Pass flush=True:

token_cost.py (last lines)
trace = mlflow.get_trace(mlflow.get_last_active_trace_id(), flush=True)
print("Token usage:", trace.info.token_usage)
print("Cost (USD):", trace.info.cost)

If the trace isn't there yet, get_trace now waits for the pending writes and tries again.

After the Fix​

Terminal
uv run --env-file .env token_cost.py
Output
Token usage: {'input_tokens': 29, 'output_tokens': 85, 'total_tokens': 114, 'cache_read_input_tokens': 0, 'cache_creation_input_tokens': 0, 'cache_creation_input_tokens_above_1hr': 0}
Cost (USD): {'input_cost': 0.000116, 'output_cost': 0.0017000000000000001, 'total_cost': 0.0018160000000000001}
2026/11/30 17:34:09 INFO mlflow.tracing.export.async_export_queue: Flushing the async trace logging queue before program exit. This may take a while...

A terminal where token_cost.py prints the trace's token usage of 114 tokens and its cost of about 0.0018 dollars.

The trace loaded, with its token usage and cost.

Common Mistakes​

MistakeWhat happensDo this instead
Copying the documentation's get_trace(trace_id=...) into a script that just made the callNone and the "span data is corrupted" warningAdd flush=True
Looking for a storage problem because of the word "corrupted"Time spent on a healthy databaseCheck whether the trace was read right after it was made

Troubleshooting​

SymptomLikely causeFix
The same warning later, for an old traceThe trace really is missing or damagedOpen it on the Traces page; if it's broken there too, the store has a problem

FAQ​

Does flush=True slow things down?​

Only when the trace isn't saved yet; then it waits for the pending writes. When the trace is already there, it returns at once.

Can I wait for every trace before my program goes on?​

flush=True is the simplest way for a single trace. MLflow also flushes on its own when the program exits, as the last output line shows.

Conclusion​

mlflow.get_trace() on the line after a traced call ran before MLflow had finished sending the trace in the background, returned None and blamed "corrupted" span data. get_trace(trace_id, flush=True) waited for the trace and returned it.

Additional Resources​