You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MLflow调用部署模型报TypeError: Only dict and DataFrame input types are supported如何解决

问题原因

你遇到的报错本质是MLflow官方内置的TensorFlow模型预测器,只接收字典(匹配TF模型输入张量名的键值对)或Pandas DataFrame类型的输入,你使用的两种请求格式被MLflow解析后都被转换为了numpy.ndarray类型,不符合类型校验规则。

解决方案

方案1:调整请求格式为TF模型兼容的字典结构(推荐,无需修改已部署模型)

首先确认你注册的TF模型的输入张量名称,可通过MLflow模型详情页的Schema查看,或本地加载模型后运行print(model.signatures["serving_default"].structured_input_signature)获取输入层名,假设你的输入张量名为input_1,修改请求如下:

curl http://127.0.0.1:1234/invocations -H "Content-Type: application/json" -d '{"inputs": {"input_1": [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 1, 4]]}}'

这种格式下MLflow会直接将输入以字典结构传给TF模型,不会自动转为numpy数组,可绕过类型校验。

方案2:重新注册模型时添加自定义输入适配逻辑

如果需要兼容原有instances/pandas-split请求格式,可在日志模型时使用mlflow.pyfunc封装一层输入转换逻辑,示例如下:

import mlflow.pyfunc
import tensorflow as tf

class TFInputAdapter(mlflow.pyfunc.PythonModel):
    def load_context(self, context):
        self.model = tf.keras.models.load_model(context.artifacts["tf_model_path"])
    
    def predict(self, context, model_input):
        # 自动适配numpy数组、DataFrame输入,转为模型需要的格式
        input_array = model_input.values if hasattr(model_input, "values") else model_input
        # 按模型要求调整结构,这里假设模型接收单输入张量
        return self.model({"input_1": input_array})

# 注册模型时使用该封装类
with mlflow.start_run():
    mlflow.pyfunc.log_model(
        artifact_path="model",
        python_model=TFInputAdapter(),
        artifacts={"tf_model_path": "./your_local_tf_model_path"},
        registered_model_name="nuovo_modello"
    )

重新部署Staging版本的模型后,原有请求格式即可正常使用。

方案3:启动服务时指定输入类型转换参数

如果使用的是MLflow 2.3+版本,启动模型服务时可添加--env-manager=local并设置环境变量MLFLOW_TF_KEEP_INPUT_AS_DF=1,避免MLflow自动将DataFrame输入转为numpy数组:

MLFLOW_TF_KEEP_INPUT_AS_DF=1 mlflow models serve -m "models:/nuovo_modello/Staging" -p 1234

该配置下pandas-split格式的请求会以DataFrame形式传入模型,符合类型校验要求。


内容的提问来源于stack exchange,提问作者Federico Scaltriti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 01:42:02