Skip to main content

How to Send a CSV File to an MLflow Model Server

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

An MLflow model server reads CSV as well as JSON, so a file exported from a spreadsheet or by pandas can go to it as it is. The request needs the header Content-Type: text/csv, and curl needs --data-binary to send the file unchanged. This guide sends two rows from a CSV file to a served model, with real output from MLflow 3.18.0.

Syntax​

Syntax
curl http://HOST:PORT/invocations -H "Content-Type: text/csv" --data-binary @FILE.csv
PartWhat it means
Content-Type: text/csvTells the server the body is CSV
--data-binary @FILE.csvSends the file exactly as it is; -InFile does the same in PowerShell
FILE.csvA header row with the model's column names, then one row per prediction

MLflow's guide to the inference server: "CSV input must be a valid pandas.DataFrame CSV representation." The same guide names the content type application/csv, but the server only accepts text/csv: Fixed: This predictor only supports the following content types ... Got 'application/csv'.

Prerequisites​

  • An MLflow tracking server at http://127.0.0.1:8080, such as mlflow server --port 8080.
  • mlflow and scikit-learn in your project, for example with uv add mlflow scikit-learn pandas.

Step 1: Log a Model and Write a CSV File​

The example trains a logistic regression on the iris flowers, logs it, and writes two flowers, a setosa and a virginica, to flowers.csv:

The log_csv_model.py script in a macOS-style code window: a LogisticRegression trained on iris with species names, logged with an input example, and rows 0 and 100 written to flowers.csv with to_csv.

log_csv_model.py
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("csv-request-example")

iris = load_iris(as_frame=True)
X, y = iris.data, iris.target_names[iris.target]
model = LogisticRegression(max_iter=1000).fit(X, y)
with mlflow.start_run():
info = mlflow.sklearn.log_model(model, name="model", input_example=X.head(3))
print("Model URI:", info.model_uri)

X.iloc[[0, 100]].to_csv("flowers.csv", index=False) # a setosa and a virginica
Terminal
uv run python log_csv_model.py
Output (last line)
Model URI: models:/m-04f61a4651704fe78403fe4c99feb8b7

The file it wrote:

flowers.csv
sepal length (cm),sepal width (cm),petal length (cm),petal width (cm)
5.1,3.5,1.4,0.2
6.3,3.3,6.0,2.5

Step 2: Serve the Model​

Terminal 2
export MLFLOW_TRACKING_URI=http://127.0.0.1:8080
uv run mlflow models serve -m "models:/m-04f61a4651704fe78403fe4c99feb8b7" -p 5001 --env-manager uv

Step 3: Send the CSV File​

Terminal 3
curl http://127.0.0.1:5001/invocations -H "Content-Type: text/csv" --data-binary @flowers.csv
Output
{"predictions": ["setosa", "virginica"]}

curl's output in a terminal: the predictions setosa and virginica as JSON, for the two rows of flowers.csv.

The server read the header for the column names and answered with one species per row. The answer is JSON even though the request was CSV.

Common Mistakes​

MistakeWhat happensDo this instead
Content-Type: application/csv, as in MLflow's guide415 Unsupported Media Typetext/csv
--data @flowers.csv instead of --data-binarycurl removes the line breaks, so the rows run together--data-binary: How to Send a File in a POST Request with curl

Troubleshooting​

SymptomLikely causeFix
Model is missing inputs [...]. Note that there were extra inputs: ['0.2', '3.5', '5.1', '1.4']The file has no header, so its first row was read as the column namesPut the model's column names in the first row
Failed to parse input as a Pandas DataFrame ... could not convert string to float: 'wide'A value in a number column isn't a numberFix the value in the file

FAQ​

Are whole numbers in the file a problem?​

Not for the server: it reads the CSV with the column types in the model's signature. mlflow models predict -t csv on the command line reads it without them, which is why the same file can fail there: Fixed: Incompatible input types for column ... Can not safely convert int64 to float64.

Can the answer be CSV too?​

No: the server always answers with JSON, {"predictions": [...]}.

Conclusion​

flowers.csv, a header and two rows, sent with Content-Type: text/csv and --data-binary (or PowerShell's -InFile), came back from the iris model as {"predictions": ["setosa", "virginica"]}.

Additional Resources​