<?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: MLflow model serving: KeyError: 'python_function' in Machine Learning</title>
    <link>https://community.databricks.com/t5/machine-learning/mlflow-model-serving-keyerror-python-function/m-p/178194#M4768</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Hello&lt;BR /&gt;&lt;BR /&gt;The issue occurs because &lt;/SPAN&gt;&lt;SPAN&gt;TfidfVectorizer&lt;/SPAN&gt;&lt;SPAN&gt; provides a &lt;/SPAN&gt;&lt;SPAN&gt;transform()&lt;/SPAN&gt;&lt;SPAN&gt; method but does not implement &lt;/SPAN&gt;&lt;SPAN&gt;predict()&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;By default, MLflow adds the &lt;/SPAN&gt;&lt;SPAN&gt;python_function&lt;/SPAN&gt;&lt;SPAN&gt; flavor to scikit-learn models that implement &lt;/SPAN&gt;&lt;SPAN&gt;predict()&lt;/SPAN&gt;&lt;SPAN&gt;. This explains why your LogisticRegression model can be served successfully while the standalone TF-IDF vectorizer fails.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;Recommended solution&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Combine preprocessing and classification into a single scikit-learn Pipeline and log the complete pipeline using MLflow.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;import mlflow
import mlflow.sklearn
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression

pipeline = Pipeline([
    ("tfidf", TfidfVectorizer()),
    ("classifier", LogisticRegression())
])

pipeline.fit(train_val["text"], train_val["label"])

with mlflow.start_run():
    model_info = mlflow.sklearn.log_model(
        pipeline,
        "text_classifier",
        input_example=train_val["text"].head(3).tolist()
    )&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;You can verify the available model flavors before deployment:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;print(
    mlflow.models.get_model_info(
        model_info.model_uri
    ).flavors.keys()
)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;The expected result should include both &lt;/SPAN&gt;&lt;SPAN&gt;sklearn&lt;/SPAN&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;SPAN&gt;python_function&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;For example, the serving endpoint can receive raw text and return predictions without requiring a separate preprocessing endpoint.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If you specifically need to serve the vectorizer independently, consider wrapping it in a custom &lt;/SPAN&gt;&lt;SPAN&gt;mlflow.pyfunc.PythonModel&lt;/SPAN&gt;&lt;SPAN&gt; that implements &lt;/SPAN&gt;&lt;SPAN&gt;predict()&lt;/SPAN&gt;&lt;SPAN&gt; and calls &lt;/SPAN&gt;&lt;SPAN&gt;transform()&lt;/SPAN&gt;&lt;SPAN&gt; internally.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would avoid manually editing the MLmodel file because adding the flavor metadata alone does not implement the required prediction interface.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;Official references:&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://mlflow.org/docs/latest/api_reference/python_api/mlflow.sklearn.html" target="_blank" rel="noopener"&gt;&lt;SPAN&gt;https://mlflow.org/docs/latest/api_reference/python_api/mlflow.sklearn.html&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/mlflow/models" target="_blank" rel="noopener"&gt;&lt;SPAN&gt;https://docs.databricks.com/aws/en/mlflow/models&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/machine-learning/model-serving/model-serving-pre-deployment-validation" target="_blank" rel="noopener"&gt;&lt;SPAN&gt;https://docs.databricks.com/aws/en/machine-learning/model-serving/model-serving-pre-deployment-validation&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 30 Nov 2026 11:27:33 GMT</pubDate>
    <dc:creator>tajamulkhan</dc:creator>
    <dc:date>2026-11-30T11:27:33Z</dc:date>
    <item>
      <title>MLflow model serving: KeyError: 'python_function'</title>
      <link>https://community.databricks.com/t5/machine-learning/mlflow-model-serving-keyerror-python-function/m-p/33363#M1765</link>
      <description>&lt;P&gt;Hello, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am training a logistic regression on text with the help of an tf-idf vectorizer.&lt;/P&gt;&lt;P&gt;This is done with MLflow and sklearn in databricks.&lt;/P&gt;&lt;P&gt;The model itself is trained successfully in databricks and it is possible to accomplish predictions within the jupyter notebook on the databricks platform.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The MLflow code that creates the model: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;with mlflow.start_run(run_name='logistic_regression') as run:
  
    text_transformer = TfidfVectorizer(stop_words=['english'], ngram_range=(1, 2), lowercase=True, max_features=150000)
    
    lr = LogisticRegression(C=5e1, solver='lbfgs', multi_class='multinomial', random_state=17, n_jobs=4)
    
    text_transformer.fit(train_val['text'])
    mlflow.sklearn.log_model(text_transformer, "tfidf-model")
    
    X_train_text = text_transformer.transform(train_val['text'])
    X_test_text = text_transformer.transform(test['text'])
    
    skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=17)
    cv_results = cross_val_score(lr, X_train_text, train_val['label'], cv=skf, scoring='f1_micro')
    
    mlflow.log_param("F1_score", cv_results.mean())
    
    lr.fit(X_train_text, train_val['label'])
    mlflow.sklearn.log_model(lr, "lr-model")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In the models tab it is only possible to serve the logistic regression without an issue. &lt;/P&gt;&lt;P&gt;However, for serving the tfidf vectorizer there arises the following issue: &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Traceback (most recent call last):
  File "&amp;lt;string&amp;gt;", line 1, in &amp;lt;module&amp;gt;
KeyError: 'python_function'&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Inspecting the two models under experiments, it is noticeable, that the tfidf vectorizer does not contain the attributes for the key 'python_function'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;logistic regression:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;artifact_path: lr-model
databricks_runtime: 10.4.x-scala2.12
flavors:
  python_function:
    env: conda.yaml
    loader_module: mlflow.sklearn
    model_path: model.pkl
    python_version: 3.8.10
  sklearn:
    code: null
    pickled_model: model.pkl
    serialization_format: cloudpickle
    sklearn_version: 0.24.1
mlflow_version: 1.28.0
model_uuid: some number
run_id: some number
utc_time_created: 'some date'&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;tfidf:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;artifact_path: tfidf-model
databricks_runtime: 10.4.x-scala2.12
flavors:
  sklearn:
    code: null
    pickled_model: model.pkl
    serialization_format: cloudpickle
    sklearn_version: 0.24.1
mlflow_version: 1.28.0
model_uuid: some number
run_id: some number
utc_time_created: 'some date'&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;B&gt;Question&lt;/B&gt;: &lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Why is the tfidf model file structured differently / why does it lack python_function?&lt;/LI&gt;&lt;LI&gt;Is it possible to edit these model files manually, such that I can add the key python_function?&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks a lot for your help in advance, &lt;/P&gt;&lt;P&gt;best, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;matebreeze&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 15:20:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/mlflow-model-serving-keyerror-python-function/m-p/33363#M1765</guid>
      <dc:creator>matebreeze</dc:creator>
      <dc:date>2022-08-26T15:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: MLflow model serving: KeyError: 'python_function'</title>
      <link>https://community.databricks.com/t5/machine-learning/mlflow-model-serving-keyerror-python-function/m-p/178194#M4768</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hello&lt;BR /&gt;&lt;BR /&gt;The issue occurs because &lt;/SPAN&gt;&lt;SPAN&gt;TfidfVectorizer&lt;/SPAN&gt;&lt;SPAN&gt; provides a &lt;/SPAN&gt;&lt;SPAN&gt;transform()&lt;/SPAN&gt;&lt;SPAN&gt; method but does not implement &lt;/SPAN&gt;&lt;SPAN&gt;predict()&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;By default, MLflow adds the &lt;/SPAN&gt;&lt;SPAN&gt;python_function&lt;/SPAN&gt;&lt;SPAN&gt; flavor to scikit-learn models that implement &lt;/SPAN&gt;&lt;SPAN&gt;predict()&lt;/SPAN&gt;&lt;SPAN&gt;. This explains why your LogisticRegression model can be served successfully while the standalone TF-IDF vectorizer fails.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;Recommended solution&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Combine preprocessing and classification into a single scikit-learn Pipeline and log the complete pipeline using MLflow.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;import mlflow
import mlflow.sklearn
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression

pipeline = Pipeline([
    ("tfidf", TfidfVectorizer()),
    ("classifier", LogisticRegression())
])

pipeline.fit(train_val["text"], train_val["label"])

with mlflow.start_run():
    model_info = mlflow.sklearn.log_model(
        pipeline,
        "text_classifier",
        input_example=train_val["text"].head(3).tolist()
    )&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;You can verify the available model flavors before deployment:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;print(
    mlflow.models.get_model_info(
        model_info.model_uri
    ).flavors.keys()
)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;The expected result should include both &lt;/SPAN&gt;&lt;SPAN&gt;sklearn&lt;/SPAN&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;SPAN&gt;python_function&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;For example, the serving endpoint can receive raw text and return predictions without requiring a separate preprocessing endpoint.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If you specifically need to serve the vectorizer independently, consider wrapping it in a custom &lt;/SPAN&gt;&lt;SPAN&gt;mlflow.pyfunc.PythonModel&lt;/SPAN&gt;&lt;SPAN&gt; that implements &lt;/SPAN&gt;&lt;SPAN&gt;predict()&lt;/SPAN&gt;&lt;SPAN&gt; and calls &lt;/SPAN&gt;&lt;SPAN&gt;transform()&lt;/SPAN&gt;&lt;SPAN&gt; internally.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would avoid manually editing the MLmodel file because adding the flavor metadata alone does not implement the required prediction interface.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;Official references:&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://mlflow.org/docs/latest/api_reference/python_api/mlflow.sklearn.html" target="_blank" rel="noopener"&gt;&lt;SPAN&gt;https://mlflow.org/docs/latest/api_reference/python_api/mlflow.sklearn.html&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/mlflow/models" target="_blank" rel="noopener"&gt;&lt;SPAN&gt;https://docs.databricks.com/aws/en/mlflow/models&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.databricks.com/aws/en/machine-learning/model-serving/model-serving-pre-deployment-validation" target="_blank" rel="noopener"&gt;&lt;SPAN&gt;https://docs.databricks.com/aws/en/machine-learning/model-serving/model-serving-pre-deployment-validation&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Nov 2026 11:27:33 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/mlflow-model-serving-keyerror-python-function/m-p/178194#M4768</guid>
      <dc:creator>tajamulkhan</dc:creator>
      <dc:date>2026-11-30T11:27:33Z</dc:date>
    </item>
  </channel>
</rss>

