dotnet/machinelearning · error · ArgumentException

Only one of input_ids or inputs_embeds may be set

Error message

Only one of input_ids or inputs_embeds may be set

What it means

LlamaModel.forward requires exactly one of input_ids or input embeddings; passing both is ambiguous so it throws ArgumentException. The two representations are mutually exclusive ways to specify the input sequence.

Source

Thrown at src/Microsoft.ML.GenAI.LLaMA/Module/LlamaModel.cs:76

            this._cache = input.OverrideCache;
        }
        else if (!input.UseCache)
        {
            this._cache = null;
        }

        var outputAttentions = input.OutputAttentions;
        var outputHiddenStates = input.OutputHiddenStates;
        var attentionMask = input.AttentionMask;
        Device device;
        var inputIds = input.InputIds;
        var positionIds = input.PositionIds;
        var inputsEmbeds = input.InputEmbeddings;
        int batchSize;
        int seqLength;
        if (inputIds is not null && inputsEmbeds is not null)
        {
            throw new ArgumentException("Only one of input_ids or inputs_embeds may be set");
        }
        else if (inputIds is not null)
        {
            batchSize = inputIds.IntShape()[0];
            seqLength = inputIds.IntShape()[1];
            inputsEmbeds = this.embed_tokens.forward(inputIds);
            device = inputIds.device;
        }
        else if (inputsEmbeds is not null)
        {
            batchSize = inputsEmbeds.IntShape()[0];
            seqLength = inputsEmbeds.IntShape()[1];
            device = inputsEmbeds.device;
        }
        else
        {
            throw new ArgumentException("Either input_ids or inputs_embeds must be set");
        }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Populate only one of InputIds or InputEmbeddings on the input object.
  2. If you have embeddings, set InputIds to null; if you have tokens, leave InputEmbeddings null.
  3. Trace back to the code constructing the input and make the choice mutually exclusive at construction time.

Example fix

// before
var input = new LlamaModelInput { InputIds = ids, InputEmbeddings = embeds };
// after
var input = new LlamaModelInput { InputIds = ids }; // embeddings computed internally
Defensive patterns

Strategy: validation

Validate before calling

if (input.InputIds is not null && input.InputEmbeddings is not null) throw new ArgumentException("Set only one of InputIds or InputEmbeddings.");

Type guard

bool HasAmbiguousInput(LlamaModelInput i) => i.InputIds is not null && i.InputEmbeddings is not null;

Try / catch

try { output = model.forward(input); } catch (ArgumentException) { /* fix input construction: clear one of the two fields */ }

Prevention

When it happens

Trigger: Constructing a LlamaModelInput/CausalLMInput where both InputIds and InputEmbeddings are non-null and calling LlamaModel.forward.

Common situations: Pipeline or agent code that pre-computes embeddings but forgets to clear the token ids, or custom code wrapping the model that always sets both fields.

Related errors


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