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

MLflow中ModelSignature.from_dict()调用抛出TypeError错误求助

问题解决方案

错误原因

从堆栈跟踪可以明确:Schema.from_json方法会将传入的JSON字符串解析后遍历每个元素,而你传入的inputs和outputs是单个JSON对象的字符串(比如{"messages": [...]}),解析后得到的是字典。遍历字典时拿到的是字符串类型的键(比如"messages"),后续代码尝试访问x["type"]自然会抛出TypeError: string indices must be integers——因为字符串不能用字典索引。

另外,ModelSignature.from_dict的参数结构要求:inputs/outputs需要是Schema对应的字典结构,或者是符合Schema格式的JSON数组字符串(而非单个对象的JSON字符串)。

正确实现方式

方式一:使用MLflow类型API构建签名(推荐)

直接用MLflow提供的Schema、ColSpec、DataType类来构造签名,类型定义更清晰:

from mlflow.models import ModelSignature
from mlflow.types import Schema, ColSpec, DataType

# 定义输入Schema:messages是数组,每个元素是包含content和role的结构体
input_schema = Schema([
    ColSpec(
        DataType.array,
        element_type=ColSpec(
            DataType.struct,
            fields={
                "content": ColSpec(DataType.string),
                "role": ColSpec(DataType.string)
            }
        ),
        name="messages"
    )
])

# 定义输出Schema
output_schema = Schema([
    ColSpec(DataType.string, name="question"),
    ColSpec(
        DataType.array,
        element_type=ColSpec(
            DataType.struct,
            fields={
                "content": ColSpec(DataType.string),
                "role": ColSpec(DataType.string)
            }
        ),
        name="history"
    ),
    ColSpec(
        DataType.array,
        element_type=ColSpec(
            DataType.struct,
            fields={
                "metadata": ColSpec(
                    DataType.struct,
                    fields={
                        "url": ColSpec(DataType.string),
                        "id": ColSpec(DataType.float)
                    }
                ),
                "page_content": ColSpec(DataType.string)
            }
        ),
        name="context"
    ),
    ColSpec(DataType.string, name="answer")
])

# 创建签名
model_signature = ModelSignature(inputs=input_schema, outputs=output_schema)

方式二:使用from_dict方法传入正确结构

如果坚持用from_dict,需要将inputs/outputs改成Schema要求的字典数组结构:

from mlflow.models import ModelSignature

signature_dict = {
    "inputs": [
        {
            "name": "messages",
            "type": "array",
            "element_type": {
                "type": "struct",
                "fields": [
                    {"name": "content", "type": "string"},
                    {"name": "role", "type": "string"}
                ]
            }
        }
    ],
    "outputs": [
        {"name": "question", "type": "string"},
        {
            "name": "history",
            "type": "array",
            "element_type": {
                "type": "struct",
                "fields": [
                    {"name": "content", "type": "string"},
                    {"name": "role", "type": "string"}
                ]
            }
        },
        {
            "name": "context",
            "type": "array",
            "element_type": {
                "type": "struct",
                "fields": [
                    {
                        "name": "metadata",
                        "type": "struct",
                        "fields": [
                            {"name": "url", "type": "string"},
                            {"name": "id", "type": "float"}
                        ]
                    },
                    {"name": "page_content", "type": "string"}
                ]
            }
        },
        {"name": "answer", "type": "string"}
    ],
    "params": []
}

model_signature = ModelSignature.from_dict(signature_dict)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 03:27:05