{"record":{"id":"e0ae7057ed8bcc15","repo":"babalae/better-genshin-impact","slug":"prefix-list","errorCode":null,"errorMessage":"模型文件缺少prefix_list","messagePattern":"模型文件缺少prefix_list","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"BetterGenshinImpact/GameTask/GetGridIcons/GridIconsAccuracyTestTask.cs","lineNumber":62,"sourceCode":"        this.maxNumToTest = maxNumToTest;\n    }\n\n    /// <summary>\n    /// 加载图标识别模型\n    /// </summary>\n    /// <param name=\"prototypes\">原型向量</param>\n    /// <returns>推理会话</returns>\n    /// <exception cref=\"Exception\"></exception>\n    public static InferenceSession LoadModel(out Dictionary<string, float[]> prototypes)\n    {\n        #region 加载model\n        var session = new InferenceSession(Global.Absolute(@\"Assets\\Model\\Item\\gridIcon.onnx\"));\n\n        var metadata = session.ModelMetadata;\n\n        if (!metadata.CustomMetadataMap.TryGetValue(\"prefix_list\", out string? prefixListJson))\n        {\n            throw new Exception(\"模型文件缺少prefix_list\");\n        }\n        List<string> prefixList = System.Text.Json.JsonSerializer.Deserialize<List<string>>(prefixListJson) ?? throw new Exception();   // 不预测前缀\n        #endregion\n        #region 加载原型向量\n        var allLines = File.ReadLines(Global.Absolute(@\"Assets\\Model\\Item\\items.csv\")).Skip(1);    // 跳过首行列名\n        prototypes = new Dictionary<string, float[]>();\n        foreach (string line in allLines)\n        {\n            var columns = line.Split(\",\").ToArray();\n            var bytes = Convert.FromBase64String(columns[1]);\n            int totalFloats = bytes.Length / sizeof(float);\n            float[] flatData = new float[totalFloats];\n            Buffer.BlockCopy(bytes, 0, flatData, 0, bytes.Length);\n            prototypes.Add(columns[0], flatData);\n        }\n        #endregion\n        return session;\n    }","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/GameTask/GetGridIcons/GridIconsAccuracyTestTask.cs#L44-L80","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Restore the original gridIcon.onnx that ships with the project, ensuring it includes the 'prefix_list' metadata.","Use Netron or onnx runtime inspection tools to verify the model's custom metadata map contains 'prefix_list'.","If retraining, ensure the export script writes CustomMetadataMap entries (prefix_list and any others the inference code expects).","Guard the metadata access and provide a clear error listing available metadata keys."],"exampleFix":"// before\nif (!metadata.CustomMetadataMap.TryGetValue(\"prefix_list\", out string? prefixListJson))\n{\n    throw new Exception(\"模型文件缺少prefix_list\");\n}\n\n// after\nif (!metadata.CustomMetadataMap.TryGetValue(\"prefix_list\", out string? prefixListJson))\n{\n    var available = string.Join(\", \", metadata.CustomMetadataMap.Keys);\n    throw new InvalidOperationException(\n        $\"模型文件缺少prefix_list。可用元数据键：{available}\");\n}","handlingStrategy":"validation","validationCode":"var modelPath = Global.Absolute(@\"Assets\\Model\\Item\\gridIcon.onnx\");\nif (!File.Exists(modelPath)) { throw new FileNotFoundException(\"ONNX 模型文件不存在\", modelPath); }\n// After loading session:\nif (!session.ModelMetadata.CustomMetadataMap.ContainsKey(\"prefix_list\"))\n{ throw new InvalidOperationException(\"模型缺少 prefix_list 元数据，请使用正确的模型文件\"); }","typeGuard":null,"tryCatchPattern":"try { var session = GridIconsAccuracyTestTask.LoadModel(out var prototypes); }\ncatch (Exception ex) { logger.LogError(ex, \"模型加载失败，请检查 gridIcon.onnx\"); }","preventionTips":["Never replace the shipped ONNX model with an unverified one.","Validate model metadata keys before inference in a test.","Document required metadata keys in the model export pipeline."],"tags":["onnx","ml-model","metadata","configuration"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}