dotnet/machinelearning · error · ArgumentException

can't add or update result that already save to csv

Error message

can't add or update result that already save to csv

What it means

CsvTrialResultManager.AddOrUpdateTrialResult throws this ArgumentException when the TrialResult being added is already present in its _trialResultsHistory. The manager keeps a history of results already persisted to CSV and refuses duplicates to avoid double-logging a trial. It indicates the same TrialResult instance (or one comparing equal) was submitted twice.

Source

Thrown at src/Microsoft.ML.AutoML/AutoMLExperiment/ITrialResultManager.cs:63

            schemaBuilder.AddColumn("id", NumberDataViewType.Int32);
            schemaBuilder.AddColumn("loss", NumberDataViewType.Single);
            schemaBuilder.AddColumn("durationInMilliseconds", NumberDataViewType.Single);
            schemaBuilder.AddColumn("peakCpu", NumberDataViewType.Single);
            schemaBuilder.AddColumn("peakMemoryInMegaByte", NumberDataViewType.Single);
            schemaBuilder.AddColumn("parameter", new VectorDataViewType(NumberDataViewType.Double));
            _schema = schemaBuilder.ToSchema();

            // load from csv file.
            var trialResults = LoadFromCsvFile(filePath);
            _trialResultsHistory = new HashSet<TrialResult>(trialResults, new TrialResult());
            _newTrialResults = new HashSet<TrialResult>(new TrialResult());
        }

        public void AddOrUpdateTrialResult(TrialResult result)
        {
            if (_trialResultsHistory.Contains(result))
            {
                throw new ArgumentException("can't add or update result that already save to csv");
            }
            _newTrialResults.Remove(result);
            _newTrialResults.Add(result);
        }

        public IEnumerable<TrialResult> GetAllTrialResults()
        {
            return _trialResultsHistory.Concat(_newTrialResults);
        }

        /// <summary>
        /// save trial result to csv. This will not overwrite any existing records that already written in csv.
        /// </summary>
        public void Save()
        {
            // header (type)
            // | id (int) | loss (float) | durationInMilliseconds (float) | peakCpu (float) | peakMemoryInMegaByte (float) | parameter_i (float) |
            using (var fileStream = new FileStream(_filePath, FileMode.Append, FileAccess.Write))

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Ensure each trial produces a unique TrialResult (unique TrialSettings/TrialId) and is only passed to AddOrUpdateTrialResult once
  2. Check _trialResultsHistory (GetAllTrialResults) before calling to skip already-recorded results
  3. If updating is intended, implement the update path in the manager instead of re-adding; remove the stale entry first
  4. Fix trial retry logic so retried trials create new result objects rather than resubmitting the old one

Example fix

// before
manager.AddOrUpdateTrialResult(result);
// after
if (!manager.GetAllTrialResults().Any(r => r.TrialSettings.TrialId == result.TrialSettings.TrialId))
{
    manager.AddOrUpdateTrialResult(result);
}
Defensive patterns

Strategy: validation

Validate before calling

bool alreadyLogged = manager.GetAllTrialResults().Any(r => r == result || r.TrialSettings.TrialId == result.TrialSettings.TrialId);
if (alreadyLogged) return;

Try / catch

try { manager.AddOrUpdateTrialResult(result); }
catch (ArgumentException) { /* result already recorded — skip */ }

Prevention

When it happens

Trigger: Calling AddOrUpdateTrialResult with a TrialResult that was previously recorded in the trial results history; a trial runner re-reporting the same completed trial, or trial IDs being reused so the same result object flows through twice.

Common situations: Custom ITrialResultManager implementations or experiment runners that retry trials without generating a fresh TrialResult; replaying a trial log; id collisions causing the same result to be registered repeatedly.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


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