<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Problem loading a pyfunc model in job run in Machine Learning</title>
    <link>https://community.databricks.com/t5/machine-learning/problem-loading-a-pyfunc-model-in-job-run/m-p/122792#M4134</link>
    <description>&lt;P&gt;found a momentary solution : use a serving endpoint but it increase costs&lt;/P&gt;</description>
    <pubDate>Wed, 25 Jun 2025 10:12:41 GMT</pubDate>
    <dc:creator>AmineM</dc:creator>
    <dc:date>2025-06-25T10:12:41Z</dc:date>
    <item>
      <title>Problem loading a pyfunc model in job run</title>
      <link>https://community.databricks.com/t5/machine-learning/problem-loading-a-pyfunc-model-in-job-run/m-p/122787#M4132</link>
      <description>&lt;P&gt;Hi, I'm currently working on a automated job to predict forecasts using a notebook than work just fine when I run it manually, but keep failling when schedueled, here is my code:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import mlflow

# Load model as a PyFuncModel.
loaded_model = mlflow.pyfunc.load_model(logged_model)
# Predict using the model
results_df = loaded_model.predict(predict_df)

# Define group_column and time_column
group_column = "id"  # Replace with your actual group column name
time_column = "week_date_format"    # Replace with your actual time column name
target_column = "sales_value"

# Display the prediction results with timestamp for each id
final_df = results_df.reset_index()[[group_column, time_column, "yhat"]].tail(
    forecast_horizon * predict_df[group_column].nunique()
)
final_df = final_df.rename(columns={'yhat': target_column})
display(final_df)&lt;/LI-CODE&gt;&lt;P&gt;The other cells where mflow is installed and model dependecies are working fyi.&amp;nbsp;&lt;BR /&gt;PS: I use serverless job compute.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jun 2025 09:31:40 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/problem-loading-a-pyfunc-model-in-job-run/m-p/122787#M4132</guid>
      <dc:creator>AmineM</dc:creator>
      <dc:date>2025-06-25T09:31:40Z</dc:date>
    </item>
    <item>
      <title>Re: Problem loading a pyfunc model in job run</title>
      <link>https://community.databricks.com/t5/machine-learning/problem-loading-a-pyfunc-model-in-job-run/m-p/122788#M4133</link>
      <description>&lt;P&gt;and here is the error that I get:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;TypeError: code() argument 13 must be str, not int
File &amp;lt;command-32974490616971&amp;gt;, line 16
     13 logged_model = 'runs:/f715739d09624676b443cb02e7c98cc0/model'
     15 # Load model as a PyFuncModel.
---&amp;gt; 16 loaded_model = mlflow.pyfunc.load_model(logged_model)
     17 # Predict using the model
     18 results_df = loaded_model.predict(predict_df)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-222c73bc-a540-4c26-aa9b-af028baf9eca/lib/python3.11/site-packages/mlflow/pyfunc/model.py:659, in _load_context_model_and_signature(model_path, model_config)
    657         raise MlflowException("Python model path was not specified in the model configuration")
    658     with open(os.path.join(model_path, python_model_subpath), "rb") as f:
--&amp;gt; 659         python_model = cloudpickle.load(f)
    661 artifacts = {}
    662 for saved_artifact_name, saved_artifact_info in pyfunc_config.get(
    663     CONFIG_KEY_ARTIFACTS, {}
    664 ).items():&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 25 Jun 2025 09:33:48 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/problem-loading-a-pyfunc-model-in-job-run/m-p/122788#M4133</guid>
      <dc:creator>AmineM</dc:creator>
      <dc:date>2025-06-25T09:33:48Z</dc:date>
    </item>
    <item>
      <title>Re: Problem loading a pyfunc model in job run</title>
      <link>https://community.databricks.com/t5/machine-learning/problem-loading-a-pyfunc-model-in-job-run/m-p/122792#M4134</link>
      <description>&lt;P&gt;found a momentary solution : use a serving endpoint but it increase costs&lt;/P&gt;</description>
      <pubDate>Wed, 25 Jun 2025 10:12:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/problem-loading-a-pyfunc-model-in-job-run/m-p/122792#M4134</guid>
      <dc:creator>AmineM</dc:creator>
      <dc:date>2025-06-25T10:12:41Z</dc:date>
    </item>
    <item>
      <title>Re: Problem loading a pyfunc model in job run</title>
      <link>https://community.databricks.com/t5/machine-learning/problem-loading-a-pyfunc-model-in-job-run/m-p/134220#M4346</link>
      <description>&lt;P&gt;Hey AmineM!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;DIV class="paragraph"&gt;If your MLflow model loads fine in a Databricks notebook but fails in a scheduled job on serverless compute with an error like:&lt;/DIV&gt;
&lt;DIV class="paragraph"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;PRE&gt;&lt;CODE&gt;TypeError: code() argument 13 must be str, not int&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="paragraph"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="paragraph"&gt;the root cause is almost always a mismatch between the Python version (or dependencies like &lt;CODE&gt;cloudpickle&lt;/CODE&gt;) used when the model was logged and the version used by your job cluster. This is especially common if you train your model on one Databricks Runtime (say, Python 3.8) and run your scheduled job on another (like Python 3.11), or across different serverless vs. interactive environments.&lt;/DIV&gt;
&lt;P&gt;&lt;STRONG&gt;How to fix it:&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Make sure your scheduled job runs on a compute/runtime with the &lt;EM&gt;same&lt;/EM&gt; Python and package versions as where you trained/logged the model.&lt;/LI&gt;
&lt;LI&gt;If you can't control the job compute's environment (sometimes the case on serverless), re-log the model &lt;EM&gt;from a job running on that same compute type&lt;/EM&gt; and use this new artifact for predictions.&lt;/LI&gt;
&lt;LI&gt;Optionally, check your model’s &lt;CODE&gt;requirements.txt&lt;/CODE&gt;/&lt;CODE&gt;conda.yaml&lt;/CODE&gt; for dependency mismatches (especially &lt;CODE&gt;cloudpickle&lt;/CODE&gt;).&lt;/LI&gt;
&lt;LI&gt;Using Model Serving works because it auto-aligns dependencies, but it does cost more. Best practice for batch is to avoid it if possible.&lt;/LI&gt;
&lt;/UL&gt;
&lt;DIV class="paragraph"&gt;This is a known serialization problem—matching environments is the robust solution! I hope this is helpful.&lt;/DIV&gt;
&lt;DIV class="paragraph"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="paragraph"&gt;Best,&lt;/DIV&gt;
&lt;DIV class="paragraph"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="paragraph"&gt;Sarah&lt;/DIV&gt;</description>
      <pubDate>Wed, 08 Oct 2025 13:12:24 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/problem-loading-a-pyfunc-model-in-job-run/m-p/134220#M4346</guid>
      <dc:creator>sarahbhord</dc:creator>
      <dc:date>2025-10-08T13:12:24Z</dc:date>
    </item>
  </channel>
</rss>

