{"record":{"id":"1c46b6ad4a94a33f","repo":"dotnet/machinelearning","slug":"could-not-find-the-vocabulary-file-vocabfile","errorCode":null,"errorMessage":"Could not find the vocabulary file '{vocabFile}'.","messagePattern":"Could not find the vocabulary file '(.+?)'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Tokenizers/Model/BpeOptions.cs","lineNumber":46,"sourceCode":"\n            Vocabulary = vocabulary;\n        }\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"BpeOptions\"/> class.\n        /// </summary>\n        /// <param name=\"vocabFile\">The JSON file path containing the dictionary of string keys and their ids.</param>\n        /// <param name=\"mergesFile\">The file path containing the tokens's pairs list.</param>\n        public BpeOptions(string vocabFile, string? mergesFile = null)\n        {\n            if (vocabFile is null)\n            {\n                throw new ArgumentNullException(nameof(vocabFile));\n            }\n\n            if (!File.Exists(vocabFile))\n            {\n                throw new ArgumentException($\"Could not find the vocabulary file '{vocabFile}'.\");\n            }\n\n            using Stream vocabStream = File.OpenRead(vocabFile);\n            Dictionary<string, int>? dictionary = JsonSerializer.Deserialize<Dictionary<string, int>>(vocabStream);\n\n            if (dictionary is null)\n            {\n                throw new InvalidOperationException($\"The content of the vocabulary file '{vocabFile}' is not valid.\");\n            }\n\n            Vocabulary = dictionary;\n\n            if (mergesFile is not null)\n            {\n                if (!File.Exists(mergesFile))\n                {\n                    throw new ArgumentException($\"Could not find the merges file '{mergesFile}'.\");\n                }","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Tokenizers/Model/BpeOptions.cs#L28-L64","documentation":"BpeOptions checks File.Exists(vocabFile) and throws ArgumentException when the supplied vocabulary file path does not exist on disk. The library requires the vocab JSON at construction because it immediately opens and deserializes it. This surfaces a bad path as a clear error instead of an obscure IOException from File.OpenRead.","triggerScenarios":"new BpeOptions(@\"models\\vocab.json\") where the file is absent, the working directory differs from the expected one, or the path has a typo or wrong casing/extension.","commonSituations":"Model files not copied to the output directory (missing CopyToOutputDirectory in csproj); relative path resolved against a different CWD when running under a test runner or service; downloading model assets was skipped.","solutions":["Verify the file exists at the exact path with File.Exists before constructing BpeOptions.","Use an absolute path or AppContext.BaseDirectory-relative path so the lookup does not depend on the current working directory.","Ensure the vocab.json asset is deployed/copied with your application output.","Confirm the file name and extension match what the model expects (e.g. vocab.json, not vocab.txt)."],"exampleFix":"// before\nvar options = new BpeOptions(\"vocab.json\");\n// after\nstring vocabPath = Path.Combine(AppContext.BaseDirectory, \"models\", \"vocab.json\");\nif (!File.Exists(vocabPath)) throw new FileNotFoundException(\"BPE vocab missing\", vocabPath);\nvar options = new BpeOptions(vocabPath);","handlingStrategy":"validation","validationCode":"if (!File.Exists(vocabPath)) throw new FileNotFoundException(\"BPE vocab file not found\", vocabPath);","typeGuard":null,"tryCatchPattern":"try { var options = new BpeOptions(vocabPath); } catch (ArgumentException ex) { logger.LogError(ex, \"Vocab file missing at {Path}\", vocabPath); }","preventionTips":["Set CopyToOutputDirectory=PreserveNewest for model assets in csproj","Build paths with Path.Combine(AppContext.BaseDirectory, ...) instead of relative paths","Run File.Exists assertions once at startup for all model files"],"tags":["csharp","file-path","tokenizer"],"backgroundTag":"file-not-found","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}