dotnet/machinelearning · error · ArgumentException

The length of inputs and outputs should be the same.

Error message

The length of inputs and outputs should be the same.

What it means

CausalLMDataset.Create(IEnumerable<MEAIChatMessage> inputs, outputs, chatTemplateBuilder, tokenizer) throws ArgumentException when the input and output collections have different lengths. The method zips inputs with outputs pairwise to build the fine-tuning prompts, so a length mismatch is meaningless and rejected up front.

Solutions

  1. Check inputs.Count() == outputs.Count() before calling Create and fix the data source.
  2. Materialize both collections to lists (ToList) and trim to the same length or drop unmatched pairs.
  3. Audit the fine-tuning file for records missing assistant responses and remove or repair them.
  4. If you must keep single-sided rows, build the dataset with the string-based or id-based Create overload after pairing manually.

Example fix

// before
dataset = CausalLMDataset.Create(chatInputs, chatOutputs, templateBuilder, tokenizer);
// after
if (chatInputs.Count() != chatOutputs.Count()) throw new InvalidDataException("rows missing assistant output");
dataset = CausalLMDataset.Create(chatInputs.ToList(), chatOutputs.ToList(), templateBuilder, tokenizer);
Defensive patterns

Strategy: validation

Validate before calling

if (inputs?.Count() != outputs?.Count()) throw new InvalidDataException($"inputs={inputs?.Count()} outputs={outputs?.Count()} must match");

Type guard

bool PairedRows<T>(IEnumerable<T> a, IEnumerable<T> b) => a != null && b != null && a.Count() == b.Count();

Try / catch

try { var ds = CausalLMDataset.Create(inputs, outputs, builder, tokenizer); }
catch (ArgumentException ex) when (ex.Message.Contains("inputs and outputs")) { logger.LogError("Fine-tuning rows are mispaired: {Msg}", ex.Message); throw; }

Prevention

When it happens

Trigger: Calling CausalLMDataset.Create with IEnumerable<MEAIChatMessage> inputs and outputs of unequal counts, e.g. a chat fine-tuning dataset where a prompt row has no matching assistant response.

Common situations: Loading a fine-tuning JSONL where some rows are missing the assistant turn; filtering one collection but not the other; LINQ lazy enumeration causing counts evaluated differently.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.ML.GenAI.Core/Trainer/CausalLMDataset.cs:34

public class CausalLMDataset : IEnumerable<CausalLMModelInput>
{
    private readonly List<CausalLMModelInput> _data;

    private CausalLMDataset(IEnumerable<CausalLMModelInput> data)
    {
        _data = new List<CausalLMModelInput>(data);
    }

    public static CausalLMDataset Create(IEnumerable<IEnumerable<ChatMessage>> inputs,
        IEnumerable<ChatMessage> outputs,
        IMEAIChatTemplateBuilder chatTemplateBuilder,
        Tokenizer tokenizer)
    {
        // the length of inputs and outputs should be the same
        if (inputs.Count() != outputs.Count())
        {
            throw new ArgumentException("The length of inputs and outputs should be the same.");
        }

        var enumerables = inputs.Zip(outputs, (input, output) =>
        {
            var inputPrompt = chatTemplateBuilder.BuildPrompt(input.ToList());
            var outputPrompt = chatTemplateBuilder.BuildPrompt(input.Concat([output]).ToList(), appendAssistantTag: false);
            var lengthToKeep = outputPrompt.Length - inputPrompt.Length;
            outputPrompt = outputPrompt.Substring(inputPrompt.Length, lengthToKeep);

            return (inputPrompt, outputPrompt);
        });

        return Create(enumerables.Select(x => x.inputPrompt), enumerables.Select(x => x.outputPrompt), tokenizer);
    }

    public static CausalLMDataset Create(IEnumerable<string> inputs, IEnumerable<string> outputs, Tokenizer tokenizer)
    {
        // the length of inputs and outputs should be the same

View on GitHub (pinned to 7b76e69cf9)