dotnet/machinelearning · error · ArgumentNullException

throw new ArgumentNullException(nameof(vocabFilePath));

Error message

throw new ArgumentNullException(nameof(vocabFilePath));

What it means

BertTokenizer.Create(string vocabFilePath, ...) throws ArgumentNullException when vocabFilePath is null or an empty string. The string is validated inline before opening the file, because File.OpenRead on a null/empty path would otherwise throw a less-clear exception. The path must point to a readable BERT vocabulary file from a trusted source.

Source

Thrown at src/Microsoft.ML.Tokenizers/Model/BertTokenizer.cs:665

            }

            return OperationStatus.Done;
        }

        /// <summary>
        /// Create a new instance of the <see cref="BertTokenizer"/> class.
        /// </summary>
        /// <param name="vocabFilePath">The path to the vocabulary file.</param>
        /// <param name="options">The options to use for the Bert tokenizer.</param>
        /// <returns>A new instance of the <see cref="BertTokenizer"/> class.</returns>
        /// <remarks>
        /// When creating the tokenizer, ensure that the vocabulary file is sourced from a trusted provider.
        /// </remarks>
        public static BertTokenizer Create(
                    string vocabFilePath,
                    BertOptions? options = null) =>
            Create(
                string.IsNullOrEmpty(vocabFilePath) ? throw new ArgumentNullException(nameof(vocabFilePath)) : File.OpenRead(vocabFilePath),
                options, disposeStream: true);

        /// <summary>
        /// Create a new instance of the <see cref="BertTokenizer"/> class.
        /// </summary>
        /// <param name="vocabStream">The stream containing the vocabulary file.</param>
        /// <param name="options">The options to use for the Bert tokenizer.</param>
        /// <returns>A new instance of the <see cref="BertTokenizer"/> class.</returns>
        /// <remarks>
        /// When creating the tokenizer, ensure that the vocabulary stream is sourced from a trusted provider.
        /// </remarks>
        public static BertTokenizer Create(
                    Stream vocabStream,
                    BertOptions? options = null) =>
            Create(vocabStream, options, disposeStream: false);

        /// <summary>
        /// Create a new instance of the <see cref="BertTokenizer"/> class asynchronously.

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Supply a valid, non-empty path to the vocab.txt file.
  2. Validate the path before calling: if (string.IsNullOrEmpty(path)) fail fast with a clear message.
  3. Fix the configuration source so the vocab path is populated (env var, config file, etc.).
  4. Check File.Exists(path) first to also catch wrong-path (not just empty) issues.

Example fix

// before
var tokenizer = BertTokenizer.Create(config.VocabPath);
// after
if (string.IsNullOrEmpty(config.VocabPath))
    throw new InvalidOperationException("VocabPath must be configured");
var tokenizer = BertTokenizer.Create(config.VocabPath);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(vocabFilePath)) throw new InvalidOperationException("Bert vocab file path must be configured and non-empty");
if (!File.Exists(vocabFilePath)) throw new FileNotFoundException("Bert vocab file not found", vocabFilePath);

Type guard

static bool IsValidVocabPath(string? path) => !string.IsNullOrWhiteSpace(path) && File.Exists(path);

Try / catch

try { var t = BertTokenizer.Create(vocabFilePath, options); } catch (ArgumentNullException ex) when (ex.ParamName == "vocabFilePath") { /* surface config error to user */ }

Prevention

When it happens

Trigger: BertTokenizer.Create(null) or BertTokenizer.Create("") — typically a config value for the vocab path that is unset, or a settings object whose path property defaulted to empty.

Common situations: Missing appsettings/config keys for model assets; deployment where the vocab file path env var is not set; typos in configuration property names leaving the path empty.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/98e3580f611a0549. Report an issue: GitHub.