babalae/better-genshin-impact · critical · Exception

模型文件缺少prefix_list

Error message

模型文件缺少prefix_list

What it means

Thrown by GridIconsAccuracyTestTask.LoadModel when the ONNX model file (gridIcon.onnx) does not contain a 'prefix_list' key in its custom metadata map. The code reads session.ModelMetadata.CustomMetadataMap and requires a 'prefix_list' entry to deserialize the list of prediction prefixes that the model will not output.

Source

Thrown at BetterGenshinImpact/GameTask/GetGridIcons/GridIconsAccuracyTestTask.cs:62

        this.maxNumToTest = maxNumToTest;
    }

    /// <summary>
    /// 加载图标识别模型
    /// </summary>
    /// <param name="prototypes">原型向量</param>
    /// <returns>推理会话</returns>
    /// <exception cref="Exception"></exception>
    public static InferenceSession LoadModel(out Dictionary<string, float[]> prototypes)
    {
        #region 加载model
        var session = new InferenceSession(Global.Absolute(@"Assets\Model\Item\gridIcon.onnx"));

        var metadata = session.ModelMetadata;

        if (!metadata.CustomMetadataMap.TryGetValue("prefix_list", out string? prefixListJson))
        {
            throw new Exception("模型文件缺少prefix_list");
        }
        List<string> prefixList = System.Text.Json.JsonSerializer.Deserialize<List<string>>(prefixListJson) ?? throw new Exception();   // 不预测前缀
        #endregion
        #region 加载原型向量
        var allLines = File.ReadLines(Global.Absolute(@"Assets\Model\Item\items.csv")).Skip(1);    // 跳过首行列名
        prototypes = new Dictionary<string, float[]>();
        foreach (string line in allLines)
        {
            var columns = line.Split(",").ToArray();
            var bytes = Convert.FromBase64String(columns[1]);
            int totalFloats = bytes.Length / sizeof(float);
            float[] flatData = new float[totalFloats];
            Buffer.BlockCopy(bytes, 0, flatData, 0, bytes.Length);
            prototypes.Add(columns[0], flatData);
        }
        #endregion
        return session;
    }

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Restore the original gridIcon.onnx that ships with the project, ensuring it includes the 'prefix_list' metadata.
  2. Use Netron or onnx runtime inspection tools to verify the model's custom metadata map contains 'prefix_list'.
  3. If retraining, ensure the export script writes CustomMetadataMap entries (prefix_list and any others the inference code expects).
  4. Guard the metadata access and provide a clear error listing available metadata keys.

Example fix

// before
if (!metadata.CustomMetadataMap.TryGetValue("prefix_list", out string? prefixListJson))
{
    throw new Exception("模型文件缺少prefix_list");
}

// after
if (!metadata.CustomMetadataMap.TryGetValue("prefix_list", out string? prefixListJson))
{
    var available = string.Join(", ", metadata.CustomMetadataMap.Keys);
    throw new InvalidOperationException(
        $"模型文件缺少prefix_list。可用元数据键:{available}");
}
Defensive patterns

Strategy: validation

Validate before calling

var modelPath = Global.Absolute(@"Assets\Model\Item\gridIcon.onnx");
if (!File.Exists(modelPath)) { throw new FileNotFoundException("ONNX 模型文件不存在", modelPath); }
// After loading session:
if (!session.ModelMetadata.CustomMetadataMap.ContainsKey("prefix_list"))
{ throw new InvalidOperationException("模型缺少 prefix_list 元数据,请使用正确的模型文件"); }

Try / catch

try { var session = GridIconsAccuracyTestTask.LoadModel(out var prototypes); }
catch (Exception ex) { logger.LogError(ex, "模型加载失败,请检查 gridIcon.onnx"); }

Prevention

When it happens

Trigger: Loading gridIcon.onnx whose ONNX metadata does not include a 'prefix_list' key. This occurs when the model was exported without the custom metadata, a different/incompatible model version was placed at Assets\Model\Item\gridIcon.onnx, or the file is corrupted.

Common situations: Replacing the ONNX model with a re-trained one that omits custom metadata tags; using a model exported by a different training pipeline; file corruption during download/copy.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/e0ae7057ed8bcc15. Report an issue: GitHub.