Skip to main content

How to Log an MLflow Model from a Python File with set_model

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

MLflow can store a custom model as its source file instead of a pickled object, so the model stays readable code. The file ends with mlflow.models.set_model(...), and you log it by passing the file's path. This guide logs a small text model that way and looks at what MLflow keeps, with real output from MLflow 3.18.0.

Syntax​

Syntax
# in <model file>.py, after the model's class:
mlflow.models.set_model(<model object>)

# in the script that logs it:
mlflow.pyfunc.log_model(name="<name>", python_model="<model file>.py", input_example=<example>)
PartWhat it means
set_model(<model object>)Marks the object in the file that MLflow uses as the model
python_model="<model file>.py"The path of that file, as text. MLflow runs it and stores it with the model
input_example=An example input. MLflow saves it and runs predict on it to check the model

MLflow's Models from Code guide comments the set_model line in its example: "This tells MLflow which object to use for inference". And: "Your model script is executed during logging to validate correctness."

Prerequisites​

  • An MLflow tracking server at http://127.0.0.1:8080, such as mlflow server --port 8080.
  • mlflow in your project, for example with uv add mlflow. This guide uses MLflow 3.18.0 on Python 3.15.2.

Step 1: Write the Model File and the Logging Script​

shout.py is the model: it turns each text into capitals with an exclamation mark, and its last line marks the model. log_shout.py logs the file by its path, lists what MLflow stored, then loads the model and predicts.

shout.py
import mlflow


class Shout(mlflow.pyfunc.PythonModel):
def predict(self, model_input: list[str], params=None) -> list[str]:
return [text.upper() + "!" for text in model_input]


mlflow.models.set_model(Shout())
log_shout.py
import mlflow

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("model-from-file-example")

with mlflow.start_run():
info = mlflow.pyfunc.log_model(name="model", python_model="shout.py", input_example=["hello"])

print("Model URI:", info.model_uri)
print(sorted(item.path for item in mlflow.artifacts.list_artifacts(info.model_uri)))

model = mlflow.pyfunc.load_model(info.model_uri)
print(model.predict(["hello", "good morning"]))

Two macOS-style code windows: shout.py, a Shout PythonModel that upper-cases each text and adds an exclamation mark and ends with mlflow.models.set_model(Shout()), and log_shout.py, which logs it with python_model="shout.py", lists the model's files, loads it and predicts two texts.

Step 2: Run It​

Terminal
uv run python log_shout.py
Output (last lines)
2026/11/28 12:09:23 INFO mlflow.utils.uv_utils: Copied uv.lock to model artifacts
2026/11/28 12:09:23 INFO mlflow.utils.uv_utils: Copied pyproject.toml to model artifacts
🏃 View run angry-fish-671 at: http://127.0.0.1:8080/#/experiments/5/runs/615c022f49f14a2a8d9a1277132a58ea
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/5
Model URI: models:/m-3f8585aac30d4cd7918f29dd66bce83e
['MLmodel', '__pycache__', 'conda.yaml', 'input_example.json', 'pyproject.toml', 'python_env.yaml', 'requirements.txt', 'serving_input_example.json', 'shout.py', 'uv.lock']
['HELLO!', 'GOOD MORNING!']

The end of the script's output in a terminal: the model URI, the sorted list of the model's files including shout.py, uv.lock and pyproject.toml, and the predictions HELLO! and GOOD MORNING!

  • shout.py is stored with the model, as readable code.
  • uv.lock and pyproject.toml: MLflow found a uv project and copied them, next to the requirements.txt it wrote.
  • __pycache__ held shout.cpython-315.pyc, the compiled copy Python wrote when MLflow ran shout.py during logging.
  • The prediction came from the loaded model: ['HELLO!', 'GOOD MORNING!'].

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out set_modelMlflowException: If the model is logged as code, ensure the model is set using mlflow.models.set_model() within the code file code file.End the file with mlflow.models.set_model(<model object>)
Passing the object instead of the pathMLflow pickles it with CloudPickle and warns: Fixed: Passing a Python object as python_modelPass the file's path as text

Troubleshooting​

SymptomLikely causeFix
ensure the model is set using mlflow.models.set_model()The file has the class but never calls set_modelAdd the set_model line at the end

FAQ​

Why log from a file instead of an object?​

The stored model is the code itself, which you can read in the model's files, instead of a pickled object. The guide notes that Models from Code "is designed for models without optimized weights (GenAI Agents, applications, custom logic)".

Does MLflow run my file when it logs it?​

Yes. The guide: "Your model script is executed during logging to validate correctness." That run is where the __pycache__ folder in the model's files came from.

Conclusion​

mlflow.models.set_model(Shout()) at the end of shout.py, and python_model="shout.py" in the logging call, stored the model as its source file, with the uv files beside it. The loaded model answered ['HELLO!', 'GOOD MORNING!'].

Additional Resources​