{"record":{"id":"67cfcdfa695166fc","repo":"dotnet/machinelearning","slug":"throw-new-argumentnullexception-nameof-vocabulary","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(vocabulary));","messagePattern":"throw new ArgumentNullException\\(nameof\\(vocabulary\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Tokenizers/Model/BpeOptions.cs","lineNumber":26,"sourceCode":"using System.Text.Json;\n\nnamespace Microsoft.ML.Tokenizers\n{\n    /// <summary>\n    /// Options for the BPE tokenizer.\n    /// </summary>\n    public sealed class BpeOptions\n    {\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"BpeOptions\"/> class.\n        /// </summary>\n        /// <param name=\"vocabulary\">The vocabulary to use.</param>\n        /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"vocabulary\"/> is null.</exception>\n        public BpeOptions(IEnumerable<KeyValuePair<string, int>> vocabulary)\n        {\n            if (vocabulary == null)\n            {\n                throw new ArgumentNullException(nameof(vocabulary));\n            }\n\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))","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Tokenizers/Model/BpeOptions.cs#L8-L44","documentation":"The BpeOptions constructor throws ArgumentNullException when the vocabulary parameter is null. BpeOptions requires an IEnumerable<KeyValuePair<string,int>> mapping tokens/merges to IDs, and a BPE tokenizer cannot operate without one. The docs explicitly document this exception.","triggerScenarios":"new BpeOptions(null) — typically when the vocabulary is loaded lazily and the load returned null, or a config-driven dictionary failed to populate before construction.","commonSituations":"BPE vocab JSON/merge files that failed to load silently; factory methods returning null vocabularies on missing files; DI scenarios where the vocab provider wasn't registered.","solutions":["Pass a non-null vocabulary dictionary of token-to-ID pairs.","Validate the vocab load step; make loaders throw on failure instead of returning null.","Use BpeTokenizer.Create(file paths) overloads that load the vocab for you.","Fail fast at startup if the vocabulary is missing rather than at tokenizer construction."],"exampleFix":"// before\nvar vocab = LoadVocab(); // may return null\nvar options = new BpeOptions(vocab);\n// after\nvar vocab = LoadVocab() ?? throw new InvalidOperationException(\"BPE vocabulary failed to load\");\nvar options = new BpeOptions(vocab);","handlingStrategy":"validation","validationCode":"if (vocabulary is null) throw new InvalidOperationException(\"BPE vocabulary must be loaded before constructing BpeOptions\");\nif (!vocabulary.Any()) throw new InvalidOperationException(\"BPE vocabulary must not be empty\");","typeGuard":"static bool HasVocabulary(IEnumerable<KeyValuePair<string, int>>? v) => v is not null && v.Any();","tryCatchPattern":"try { var opts = new BpeOptions(vocabulary); } catch (ArgumentNullException ex) when (ex.ParamName == \"vocabulary\") { /* load vocab from disk and retry */ }","preventionTips":["Make vocab loaders throw on failure instead of returning null.","Prefer BpeTokenizer.Create(...) overloads that load vocab/merges from files.","Validate vocab loading at startup with a count sanity check."],"tags":["csharp","argument-null","bpe","tokenizer"],"backgroundTag":"null-argument","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}